cinap_lenrekadd hg and python4bb9e2215 years agoHistory
4351 lines · 104.0 KB · C
RawDownload
static intimport_all_from(PyObject *locals, PyObject *v){	PyObject *all = PyObject_GetAttrString(v, "__all__");	PyObject *dict, *name, *value;	int skip_leading_underscores = 0;	int pos, err; 	if (all == NULL) {		if (!PyErr_ExceptionMatches(PyExc_AttributeError))			return -1; /* Unexpected error */		PyErr_Clear();		dict = PyObject_GetAttrString(v, "__dict__");		if (dict == NULL) {			if (!PyErr_ExceptionMatches(PyExc_AttributeError))				return -1;			PyErr_SetString(PyExc_ImportError,			"from-import-* object has no __dict__ and no __all__");			return -1;		}		all = PyMapping_Keys(dict);		Py_DECREF(dict);		if (all == NULL)			return -1;		skip_leading_underscores = 1;	} 	for (pos = 0, err = 0; ; pos++) {		name = PySequence_GetItem(all, pos);		if (name == NULL) {			if (!PyErr_ExceptionMatches(PyExc_IndexError))				err = -1;			else				PyErr_Clear();			break;		}		if (skip_leading_underscores &&		    PyString_Check(name) &&		    PyString_AS_STRING(name)[0] == '_')		{			Py_DECREF(name);			continue;		}		value = PyObject_GetAttr(v, name);		if (value == NULL)			err = -1;		else if (PyDict_CheckExact(locals))			err = PyDict_SetItem(locals, name, value);		else			err = PyObject_SetItem(locals, name, value);		Py_DECREF(name);		Py_XDECREF(value);		if (err != 0)			break;	}	Py_DECREF(all);	return err;} static PyObject *build_class(PyObject *methods, PyObject *bases, PyObject *name){	PyObject *metaclass = NULL, *result, *base; 	if (PyDict_Check(methods))		metaclass = PyDict_GetItemString(methods, "__metaclass__");	if (metaclass != NULL)		Py_INCREF(metaclass);	else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {		base = PyTuple_GET_ITEM(bases, 0);		metaclass = PyObject_GetAttrString(base, "__class__");		if (metaclass == NULL) {			PyErr_Clear();			metaclass = (PyObject *)base->ob_type;			Py_INCREF(metaclass);		}	}	else {		PyObject *g = PyEval_GetGlobals();		if (g != NULL && PyDict_Check(g))			metaclass = PyDict_GetItemString(g, "__metaclass__");		if (metaclass == NULL)			metaclass = (PyObject *) &PyClass_Type;		Py_INCREF(metaclass);	}	result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods, NULL);	Py_DECREF(metaclass);	if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {		/* A type error here likely means that the user passed		   in a base that was not a class (such the random module		   instead of the random.random type).  Help them out with		   by augmenting the error message with more information.*/ 		PyObject *ptype, *pvalue, *ptraceback; 		PyErr_Fetch(&ptype, &pvalue, &ptraceback);		if (PyString_Check(pvalue)) {			PyObject *newmsg;			newmsg = PyString_FromFormat(				"Error when calling the metaclass bases\n    %s",				PyString_AS_STRING(pvalue));			if (newmsg != NULL) {				Py_DECREF(pvalue);				pvalue = newmsg;			}		}		PyErr_Restore(ptype, pvalue, ptraceback);	}	return result;} static intexec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,	       PyObject *locals){	int n;	PyObject *v;	int plain = 0; 	if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&	    ((n = PyTuple_Size(prog)) == 2 || n == 3)) {		/* Backward compatibility hack */		globals = PyTuple_GetItem(prog, 1);		if (n == 3)			locals = PyTuple_GetItem(prog, 2);		prog = PyTuple_GetItem(prog, 0);	}	if (globals == Py_None) {		globals = PyEval_GetGlobals();		if (locals == Py_None) {			locals = PyEval_GetLocals();			plain = 1;		}		if (!globals || !locals) {			PyErr_SetString(PyExc_SystemError,					"globals and locals cannot be NULL");			return -1;		}	}	else if (locals == Py_None)		locals = globals;	if (!PyString_Check(prog) &&	    !PyUnicode_Check(prog) &&	    !PyCode_Check(prog) &&	    !PyFile_Check(prog)) {		PyErr_SetString(PyExc_TypeError,			"exec: arg 1 must be a string, file, or code object");		return -1;	}	if (!PyDict_Check(globals)) {		PyErr_SetString(PyExc_TypeError,		    "exec: arg 2 must be a dictionary or None");		return -1;	}	if (!PyMapping_Check(locals)) {		PyErr_SetString(PyExc_TypeError,		    "exec: arg 3 must be a mapping or None");		return -1;	}	if (PyDict_GetItemString(globals, "__builtins__") == NULL)		PyDict_SetItemString(globals, "__builtins__", f->f_builtins);	if (PyCode_Check(prog)) {		if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {			PyErr_SetString(PyExc_TypeError,		"code object passed to exec may not contain free variables");			return -1;		}		v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);	}	else if (PyFile_Check(prog)) {		FILE *fp = PyFile_AsFile(prog);		char *name = PyString_AsString(PyFile_Name(prog));		PyCompilerFlags cf;		if (name == NULL)			return -1;		cf.cf_flags = 0;		if (PyEval_MergeCompilerFlags(&cf))			v = PyRun_FileFlags(fp, name, Py_file_input, globals,					    locals, &cf);		else			v = PyRun_File(fp, name, Py_file_input, globals,				       locals);	}	else {		PyObject *tmp = NULL;		char *str;		PyCompilerFlags cf;		cf.cf_flags = 0;#ifdef Py_USING_UNICODE		if (PyUnicode_Check(prog)) {			tmp = PyUnicode_AsUTF8String(prog);			if (tmp == NULL)				return -1;			prog = tmp;			cf.cf_flags |= PyCF_SOURCE_IS_UTF8;		}#endif		if (PyString_AsStringAndSize(prog, &str, NULL))			return -1;		if (PyEval_MergeCompilerFlags(&cf))			v = PyRun_StringFlags(str, Py_file_input, globals,					      locals, &cf);		else			v = PyRun_String(str, Py_file_input, globals, locals);		Py_XDECREF(tmp);	}	if (plain)		PyFrame_LocalsToFast(f, 0);	if (v == NULL)		return -1;	Py_DECREF(v);	return 0;} static voidformat_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj){	char *obj_str; 	if (!obj)		return; 	obj_str = PyString_AsString(obj);	if (!obj_str)		return; 	PyErr_Format(exc, format_str, obj_str);} static PyObject *string_concatenate(PyObject *v, PyObject *w,		   PyFrameObject *f, unsigned char *next_instr){	/* This function implements 'variable += expr' when both arguments	   are strings. */	Py_ssize_t v_len = PyString_GET_SIZE(v);	Py_ssize_t w_len = PyString_GET_SIZE(w);	Py_ssize_t new_len = v_len + w_len;	if (new_len < 0) {		PyErr_SetString(PyExc_OverflowError,				"strings are too large to concat");		return NULL;	} 	if (v->ob_refcnt == 2) {		/* In the common case, there are 2 references to the value		 * stored in 'variable' when the += is performed: one on the		 * value stack (in 'v') and one still stored in the 'variable'.		 * We try to delete the variable now to reduce the refcnt to 1.		 */		switch (*next_instr) {		case STORE_FAST:		{			int oparg = PEEKARG();			PyObject **fastlocals = f->f_localsplus;			if (GETLOCAL(oparg) == v)				SETLOCAL(oparg, NULL);			break;		}		case STORE_DEREF:		{			PyObject **freevars = f->f_localsplus + f->f_code->co_nlocals;			PyObject *c = freevars[PEEKARG()];			if (PyCell_GET(c) == v)				PyCell_Set(c, NULL);			break;		}		case STORE_NAME:		{			PyObject *names = f->f_code->co_names;			PyObject *name = GETITEM(names, PEEKARG());			PyObject *locals = f->f_locals;			if (PyDict_CheckExact(locals) &&			    PyDict_GetItem(locals, name) == v) {				if (PyDict_DelItem(locals, name) != 0) {					PyErr_Clear();				}			}			break;		}		}	} 	if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {		/* Now we own the last reference to 'v', so we can resize it		 * in-place.		 */		if (_PyString_Resize(&v, new_len) != 0) {			/* XXX if _PyString_Resize() fails, 'v' has been			 * deallocated so it cannot be put back into 'variable'.			 * The MemoryError is raised when there is no value in			 * 'variable', which might (very remotely) be a cause			 * of incompatibilities.			 */			return NULL;		}		/* copy 'w' into the newly allocated area of 'v' */		memcpy(PyString_AS_STRING(v) + v_len,		       PyString_AS_STRING(w), w_len);		return v;	}	else {		/* When in-place resizing is not an option. */		PyString_Concat(&v, w);		return v;	}} #ifdef DYNAMIC_EXECUTION_PROFILE static PyObject *getarray(long a[256]){	int i;	PyObject *l = PyList_New(256);	if (l == NULL) return NULL;	for (i = 0; i < 256; i++) {		PyObject *x = PyInt_FromLong(a[i]);		if (x == NULL) {			Py_DECREF(l);			return NULL;		}		PyList_SetItem(l, i, x);	}	for (i = 0; i < 256; i++)		a[i] = 0;	return l;} PyObject *_Py_GetDXProfile(PyObject *self, PyObject *args){#ifndef DXPAIRS	return getarray(dxp);#else	int i;	PyObject *l = PyList_New(257);	if (l == NULL) return NULL;	for (i = 0; i < 257; i++) {		PyObject *x = getarray(dxpairs[i]);		if (x == NULL) {			Py_DECREF(l);			return NULL;		}		PyList_SetItem(l, i, x);	}	return l;#endif} #endif

Lines 4001–4351 of 4351. Previous lines