cinap_lenrekadd hg and python4bb9e2215 years agoHistory
2620 lines · 61.8 KB · C
RawDownload
"No argument:  the names in the current scope.\n""Module object:  the module attributes.\n""Type or class object:  its attributes, and recursively the attributes of\n""    its bases.\n""Otherwise:  its attributes, its class's attributes, and recursively the\n""    attributes of its class's base classes."); static PyObject *builtin_divmod(PyObject *self, PyObject *args){	PyObject *v, *w; 	if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))		return NULL;	return PyNumber_Divmod(v, w);} PyDoc_STRVAR(divmod_doc,"divmod(x, y) -> (div, mod)\n\\n\Return the tuple ((x-x%y)/y, x%y).  Invariant: div*y + mod == x.");  static PyObject *builtin_eval(PyObject *self, PyObject *args){	PyObject *cmd, *result, *tmp = NULL;	PyObject *globals = Py_None, *locals = Py_None;	char *str;	PyCompilerFlags cf; 	if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals))		return NULL;	if (locals != Py_None && !PyMapping_Check(locals)) {		PyErr_SetString(PyExc_TypeError, "locals must be a mapping");		return NULL;	}	if (globals != Py_None && !PyDict_Check(globals)) {		PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?			"globals must be a real dict; try eval(expr, {}, mapping)"			: "globals must be a dict");		return NULL;	}	if (globals == Py_None) {		globals = PyEval_GetGlobals();		if (locals == Py_None)			locals = PyEval_GetLocals();	}	else if (locals == Py_None)		locals = globals; 	if (globals == NULL || locals == NULL) {		PyErr_SetString(PyExc_TypeError, 			"eval must be given globals and locals "			"when called without a frame");		return NULL;	} 	if (PyDict_GetItemString(globals, "__builtins__") == NULL) {		if (PyDict_SetItemString(globals, "__builtins__",					 PyEval_GetBuiltins()) != 0)			return NULL;	} 	if (PyCode_Check(cmd)) {		if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {			PyErr_SetString(PyExc_TypeError,		"code object passed to eval() may not contain free variables");			return NULL;		}		return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);	} 	if (!PyString_Check(cmd) &&	    !PyUnicode_Check(cmd)) {		PyErr_SetString(PyExc_TypeError,			   "eval() arg 1 must be a string or code object");		return NULL;	}	cf.cf_flags = 0; #ifdef Py_USING_UNICODE	if (PyUnicode_Check(cmd)) {		tmp = PyUnicode_AsUTF8String(cmd);		if (tmp == NULL)			return NULL;		cmd = tmp;		cf.cf_flags |= PyCF_SOURCE_IS_UTF8;	}#endif	if (PyString_AsStringAndSize(cmd, &str, NULL)) {		Py_XDECREF(tmp);		return NULL;	}	while (*str == ' ' || *str == '\t')		str++; 	(void)PyEval_MergeCompilerFlags(&cf);	result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);	Py_XDECREF(tmp);	return result;} PyDoc_STRVAR(eval_doc,"eval(source[, globals[, locals]]) -> value\n\\n\Evaluate the source in the context of globals and locals.\n\The source may be a string representing a Python expression\n\or a code object as returned by compile().\n\The globals must be a dictionary and locals can be any mapping,\n\defaulting to the current globals and locals.\n\If only globals is given, locals defaults to it.\n");  static PyObject *builtin_execfile(PyObject *self, PyObject *args){	char *filename;	PyObject *globals = Py_None, *locals = Py_None;	PyObject *res;	FILE* fp = NULL;	PyCompilerFlags cf;	int exists; 	if (!PyArg_ParseTuple(args, "s|O!O:execfile",			&filename,			&PyDict_Type, &globals,			&locals))		return NULL;	if (locals != Py_None && !PyMapping_Check(locals)) {		PyErr_SetString(PyExc_TypeError, "locals must be a mapping");		return NULL;	}	if (globals == Py_None) {		globals = PyEval_GetGlobals();		if (locals == Py_None)			locals = PyEval_GetLocals();	}	else if (locals == Py_None)		locals = globals;	if (PyDict_GetItemString(globals, "__builtins__") == NULL) {		if (PyDict_SetItemString(globals, "__builtins__",					 PyEval_GetBuiltins()) != 0)			return NULL;	} 	exists = 0;	/* Test for existence or directory. */#if defined(PLAN9)	{		Dir *d; 		if ((d = dirstat(filename))!=nil) {			if(d->mode & DMDIR)				werrstr("is a directory");			else				exists = 1;			free(d);		}	}#elif defined(RISCOS)	if (object_exists(filename)) {		if (isdir(filename))			errno = EISDIR;		else			exists = 1;	}#else	/* standard Posix */	{		struct stat s;		if (stat(filename, &s) == 0) {			if (S_ISDIR(s.st_mode))#				if defined(PYOS_OS2) && defined(PYCC_VACPP)					errno = EOS2ERR;#				else					errno = EISDIR;#				endif			else				exists = 1;		}	}#endif         if (exists) {		Py_BEGIN_ALLOW_THREADS		fp = fopen(filename, "r" PY_STDIOTEXTMODE);		Py_END_ALLOW_THREADS 		if (fp == NULL) {			exists = 0;		}        } 	if (!exists) {		PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);		return NULL;	}	cf.cf_flags = 0;	if (PyEval_MergeCompilerFlags(&cf))		res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,				   locals, 1, &cf);	else		res = PyRun_FileEx(fp, filename, Py_file_input, globals,				   locals, 1);	return res;} PyDoc_STRVAR(execfile_doc,"execfile(filename[, globals[, locals]])\n\\n\Read and execute a Python script from a file.\n\The globals and locals are dictionaries, defaulting to the current\n\globals and locals.  If only globals is given, locals defaults to it.");  static PyObject *builtin_getattr(PyObject *self, PyObject *args){	PyObject *v, *result, *dflt = NULL;	PyObject *name; 	if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))		return NULL;#ifdef Py_USING_UNICODE	if (PyUnicode_Check(name)) {		name = _PyUnicode_AsDefaultEncodedString(name, NULL);		if (name == NULL)			return NULL;	}#endif 	if (!PyString_Check(name)) {		PyErr_SetString(PyExc_TypeError,				"getattr(): attribute name must be string");		return NULL;	}	result = PyObject_GetAttr(v, name);	if (result == NULL && dflt != NULL &&	    PyErr_ExceptionMatches(PyExc_AttributeError))	{		PyErr_Clear();		Py_INCREF(dflt);		result = dflt;	}	return result;} PyDoc_STRVAR(getattr_doc,"getattr(object, name[, default]) -> value\n\\n\Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\When a default argument is given, it is returned when the attribute doesn't\n\exist; without it, an exception is raised in that case.");  static PyObject *builtin_globals(PyObject *self){	PyObject *d; 	d = PyEval_GetGlobals();	Py_XINCREF(d);	return d;} PyDoc_STRVAR(globals_doc,"globals() -> dictionary\n\\n\Return the dictionary containing the current scope's global variables.");  static PyObject *builtin_hasattr(PyObject *self, PyObject *args){	PyObject *v;	PyObject *name; 	if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))		return NULL;#ifdef Py_USING_UNICODE	if (PyUnicode_Check(name)) {		name = _PyUnicode_AsDefaultEncodedString(name, NULL);		if (name == NULL)			return NULL;	}#endif 	if (!PyString_Check(name)) {		PyErr_SetString(PyExc_TypeError,				"hasattr(): attribute name must be string");		return NULL;	}	v = PyObject_GetAttr(v, name);	if (v == NULL) {		PyErr_Clear();		Py_INCREF(Py_False);		return Py_False;	}	Py_DECREF(v);	Py_INCREF(Py_True);	return Py_True;} PyDoc_STRVAR(hasattr_doc,"hasattr(object, name) -> bool\n\\n\Return whether the object has an attribute with the given name.\n\(This is done by calling getattr(object, name) and catching exceptions.)");  static PyObject *builtin_id(PyObject *self, PyObject *v){	return PyLong_FromVoidPtr(v);} PyDoc_STRVAR(id_doc,"id(object) -> integer\n\\n\Return the identity of an object.  This is guaranteed to be unique among\n\simultaneously existing objects.  (Hint: it's the object's memory address.)");  static PyObject *builtin_map(PyObject *self, PyObject *args){	typedef struct {		PyObject *it;	/* the iterator object */		int saw_StopIteration;  /* bool:  did the iterator end? */	} sequence; 	PyObject *func, *result;	sequence *seqs = NULL, *sqp;	Py_ssize_t n, len;	register int i, j; 	n = PyTuple_Size(args);	if (n < 2) {		PyErr_SetString(PyExc_TypeError,				"map() requires at least two args");		return NULL;	} 	func = PyTuple_GetItem(args, 0);	n--; 	if (func == Py_None && n == 1) {		/* map(None, S) is the same as list(S). */		return PySequence_List(PyTuple_GetItem(args, 1));	} 	/* Get space for sequence descriptors.  Must NULL out the iterator	 * pointers so that jumping to Fail_2 later doesn't see trash.	 */	if ((seqs = PyMem_NEW(sequence, n)) == NULL) {		PyErr_NoMemory();		return NULL;	}	for (i = 0; i < n; ++i) {		seqs[i].it = (PyObject*)NULL;		seqs[i].saw_StopIteration = 0;	} 	/* Do a first pass to obtain iterators for the arguments, and set len	 * to the largest of their lengths.	 */	len = 0;	for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {		PyObject *curseq;		Py_ssize_t curlen; 		/* Get iterator. */		curseq = PyTuple_GetItem(args, i+1);		sqp->it = PyObject_GetIter(curseq);		if (sqp->it == NULL) {			static char errmsg[] =			    "argument %d to map() must support iteration";			char errbuf[sizeof(errmsg) + 25];			PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);			PyErr_SetString(PyExc_TypeError, errbuf);			goto Fail_2;		} 		/* Update len. */		curlen = _PyObject_LengthHint(curseq);		if (curlen < 0) {			if (!PyErr_ExceptionMatches(PyExc_TypeError)  &&			    !PyErr_ExceptionMatches(PyExc_AttributeError)) {				goto Fail_2;			}			PyErr_Clear();			curlen = 8;  /* arbitrary */		}		if (curlen > len)			len = curlen;	} 	/* Get space for the result list. */	if ((result = (PyObject *) PyList_New(len)) == NULL)		goto Fail_2; 	/* Iterate over the sequences until all have stopped. */	for (i = 0; ; ++i) {		PyObject *alist, *item=NULL, *value;		int numactive = 0; 		if (func == Py_None && n == 1)			alist = NULL;		else if ((alist = PyTuple_New(n)) == NULL)			goto Fail_1; 		for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {			if (sqp->saw_StopIteration) {				Py_INCREF(Py_None);				item = Py_None;			}			else {				item = PyIter_Next(sqp->it);				if (item)					++numactive;				else {					if (PyErr_Occurred()) {						Py_XDECREF(alist);						goto Fail_1;					}					Py_INCREF(Py_None);					item = Py_None;					sqp->saw_StopIteration = 1;				}			}			if (alist)				PyTuple_SET_ITEM(alist, j, item);			else				break;		} 		if (!alist)			alist = item; 		if (numactive == 0) {			Py_DECREF(alist);			break;		} 		if (func == Py_None)			value = alist;		else {			value = PyEval_CallObject(func, alist);			Py_DECREF(alist);			if (value == NULL)				goto Fail_1;		}		if (i >= len) {			int status = PyList_Append(result, value);			Py_DECREF(value);			if (status < 0)				goto Fail_1;		}		else if (PyList_SetItem(result, i, value) < 0)		 	goto Fail_1;	} 	if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)		goto Fail_1; 	goto Succeed; Fail_1:	Py_DECREF(result);Fail_2:	result = NULL;Succeed:	assert(seqs);	for (i = 0; i < n; ++i)		Py_XDECREF(seqs[i].it);	PyMem_DEL(seqs);	return result;} PyDoc_STRVAR(map_doc,"map(function, sequence[, sequence, ...]) -> list\n\\n\Return a list of the results of applying the function to the items of\n\the argument sequence(s).  If more than one sequence is given, the\n\function is called with an argument list consisting of the corresponding\n\item of each sequence, substituting None for missing values when not all\n\sequences have the same length.  If the function is None, return a list of\n\the items of the sequence (or a list of tuples if more than one sequence).");  static PyObject *builtin_setattr(PyObject *self, PyObject *args){	PyObject *v;	PyObject *name;	PyObject *value; 	if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))		return NULL;	if (PyObject_SetAttr(v, name, value) != 0)

Lines 501–1000 of 2620. Previous lines