cinap_lenrekadd hg and python4bb9e2215 years agoHistory
4351 lines · 104.0 KB · C
RawDownload
   This *consumes* a reference count to each of its arguments. */static enum why_codedo_raise(PyObject *type, PyObject *value, PyObject *tb){	if (type == NULL) {		/* Reraise */		PyThreadState *tstate = PyThreadState_GET();		type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;		value = tstate->exc_value;		tb = tstate->exc_traceback;		Py_XINCREF(type);		Py_XINCREF(value);		Py_XINCREF(tb);	} 	/* We support the following forms of raise:	   raise <class>, <classinstance>	   raise <class>, <argument tuple>	   raise <class>, None	   raise <class>, <argument>	   raise <classinstance>, None	   raise <string>, <object>	   raise <string>, None 	   An omitted second argument is the same as None. 	   In addition, raise <tuple>, <anything> is the same as	   raising the tuple's first item (and it better have one!);	   this rule is applied recursively. 	   Finally, an optional third argument can be supplied, which	   gives the traceback to be substituted (useful when	   re-raising an exception after examining it).  */ 	/* First, check the traceback argument, replacing None with	   NULL. */	if (tb == Py_None) {		Py_DECREF(tb);		tb = NULL;	}	else if (tb != NULL && !PyTraceBack_Check(tb)) {		PyErr_SetString(PyExc_TypeError,			   "raise: arg 3 must be a traceback or None");		goto raise_error;	} 	/* Next, replace a missing value with None */	if (value == NULL) {		value = Py_None;		Py_INCREF(value);	} 	/* Next, repeatedly, replace a tuple exception with its first item */	while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {		PyObject *tmp = type;		type = PyTuple_GET_ITEM(type, 0);		Py_INCREF(type);		Py_DECREF(tmp);	} 	if (PyString_CheckExact(type)) {		/* Raising builtin string is deprecated but still allowed --		 * do nothing.  Raising an instance of a new-style str		 * subclass is right out. */		if (PyErr_Warn(PyExc_DeprecationWarning,			   "raising a string exception is deprecated"))			goto raise_error;	}	else if (PyExceptionClass_Check(type))		PyErr_NormalizeException(&type, &value, &tb); 	else if (PyExceptionInstance_Check(type)) {		/* Raising an instance.  The value should be a dummy. */		if (value != Py_None) {			PyErr_SetString(PyExc_TypeError,			  "instance exception may not have a separate value");			goto raise_error;		}		else {			/* Normalize to raise <class>, <instance> */			Py_DECREF(value);			value = type;			type = PyExceptionInstance_Class(type);			Py_INCREF(type);		}	}	else {		/* Not something you can raise.  You get an exception		   anyway, just not what you specified :-) */		PyErr_Format(PyExc_TypeError,			     "exceptions must be classes, instances, or "			     "strings (deprecated), not %s",			     type->ob_type->tp_name);		goto raise_error;	}	PyErr_Restore(type, value, tb);	if (tb == NULL)		return WHY_EXCEPTION;	else		return WHY_RERAISE; raise_error:	Py_XDECREF(value);	Py_XDECREF(type);	Py_XDECREF(tb);	return WHY_EXCEPTION;} /* Iterate v argcnt times and store the results on the stack (via decreasing   sp).  Return 1 for success, 0 if error. */ static intunpack_iterable(PyObject *v, int argcnt, PyObject **sp){	int i = 0;	PyObject *it;  /* iter(v) */	PyObject *w; 	assert(v != NULL); 	it = PyObject_GetIter(v);	if (it == NULL)		goto Error; 	for (; i < argcnt; i++) {		w = PyIter_Next(it);		if (w == NULL) {			/* Iterator done, via error or exhaustion. */			if (!PyErr_Occurred()) {				PyErr_Format(PyExc_ValueError,					"need more than %d value%s to unpack",					i, i == 1 ? "" : "s");			}			goto Error;		}		*--sp = w;	} 	/* We better have exhausted the iterator now. */	w = PyIter_Next(it);	if (w == NULL) {		if (PyErr_Occurred())			goto Error;		Py_DECREF(it);		return 1;	}	Py_DECREF(w);	PyErr_SetString(PyExc_ValueError, "too many values to unpack");	/* fall through */Error:	for (; i > 0; i--, sp++)		Py_DECREF(*sp);	Py_XDECREF(it);	return 0;}  #ifdef LLTRACEstatic intprtrace(PyObject *v, char *str){	printf("%s ", str);	if (PyObject_Print(v, stdout, 0) != 0)		PyErr_Clear(); /* Don't know what else to do */	printf("\n");	return 1;}#endif static voidcall_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f){	PyObject *type, *value, *traceback, *arg;	int err;	PyErr_Fetch(&type, &value, &traceback);	if (value == NULL) {		value = Py_None;		Py_INCREF(value);	}	arg = PyTuple_Pack(3, type, value, traceback);	if (arg == NULL) {		PyErr_Restore(type, value, traceback);		return;	}	err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);	Py_DECREF(arg);	if (err == 0)		PyErr_Restore(type, value, traceback);	else {		Py_XDECREF(type);		Py_XDECREF(value);		Py_XDECREF(traceback);	}} static voidcall_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,		     int what, PyObject *arg){	PyObject *type, *value, *traceback;	int err;	PyErr_Fetch(&type, &value, &traceback);	err = call_trace(func, obj, frame, what, arg);	if (err == 0)		PyErr_Restore(type, value, traceback);	else {		Py_XDECREF(type);		Py_XDECREF(value);		Py_XDECREF(traceback);	}} static intcall_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,	   int what, PyObject *arg){	register PyThreadState *tstate = frame->f_tstate;	int result;	if (tstate->tracing)		return 0;	tstate->tracing++;	tstate->use_tracing = 0;	result = func(obj, frame, what, arg);	tstate->use_tracing = ((tstate->c_tracefunc != NULL)			       || (tstate->c_profilefunc != NULL));	tstate->tracing--;	return result;} PyObject *_PyEval_CallTracing(PyObject *func, PyObject *args){	PyFrameObject *frame = PyEval_GetFrame();	PyThreadState *tstate = frame->f_tstate;	int save_tracing = tstate->tracing;	int save_use_tracing = tstate->use_tracing;	PyObject *result; 	tstate->tracing = 0;	tstate->use_tracing = ((tstate->c_tracefunc != NULL)			       || (tstate->c_profilefunc != NULL));	result = PyObject_Call(func, args, NULL);	tstate->tracing = save_tracing;	tstate->use_tracing = save_use_tracing;	return result;} static intmaybe_call_line_trace(Py_tracefunc func, PyObject *obj,		      PyFrameObject *frame, int *instr_lb, int *instr_ub,		      int *instr_prev){	int result = 0;         /* If the last instruction executed isn't in the current           instruction window, reset the window.  If the last           instruction happens to fall at the start of a line or if it           represents a jump backwards, call the trace function.        */	if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {                int line;                PyAddrPair bounds;                 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,                                              &bounds);                if (line >= 0) {			frame->f_lineno = line;			result = call_trace(func, obj, frame,					    PyTrace_LINE, Py_None);                }                *instr_lb = bounds.ap_lower;                *instr_ub = bounds.ap_upper;	}	else if (frame->f_lasti <= *instr_prev) {		result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);	}	*instr_prev = frame->f_lasti;	return result;} voidPyEval_SetProfile(Py_tracefunc func, PyObject *arg){	PyThreadState *tstate = PyThreadState_GET();	PyObject *temp = tstate->c_profileobj;	Py_XINCREF(arg);	tstate->c_profilefunc = NULL;	tstate->c_profileobj = NULL;	/* Must make sure that tracing is not ignored if 'temp' is freed */	tstate->use_tracing = tstate->c_tracefunc != NULL;	Py_XDECREF(temp);	tstate->c_profilefunc = func;	tstate->c_profileobj = arg;	/* Flag that tracing or profiling is turned on */	tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);} voidPyEval_SetTrace(Py_tracefunc func, PyObject *arg){	PyThreadState *tstate = PyThreadState_GET();	PyObject *temp = tstate->c_traceobj;	Py_XINCREF(arg);	tstate->c_tracefunc = NULL;	tstate->c_traceobj = NULL;	/* Must make sure that profiling is not ignored if 'temp' is freed */	tstate->use_tracing = tstate->c_profilefunc != NULL;	Py_XDECREF(temp);	tstate->c_tracefunc = func;	tstate->c_traceobj = arg;	/* Flag that tracing or profiling is turned on */	tstate->use_tracing = ((func != NULL)			       || (tstate->c_profilefunc != NULL));} PyObject *PyEval_GetBuiltins(void){	PyFrameObject *current_frame = PyEval_GetFrame();	if (current_frame == NULL)		return PyThreadState_GET()->interp->builtins;	else		return current_frame->f_builtins;} PyObject *PyEval_GetLocals(void){	PyFrameObject *current_frame = PyEval_GetFrame();	if (current_frame == NULL)		return NULL;	PyFrame_FastToLocals(current_frame);	return current_frame->f_locals;} PyObject *PyEval_GetGlobals(void){	PyFrameObject *current_frame = PyEval_GetFrame();	if (current_frame == NULL)		return NULL;	else		return current_frame->f_globals;} PyFrameObject *PyEval_GetFrame(void){	PyThreadState *tstate = PyThreadState_GET();	return _PyThreadState_GetFrame(tstate);} intPyEval_GetRestricted(void){	PyFrameObject *current_frame = PyEval_GetFrame();	return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);} intPyEval_MergeCompilerFlags(PyCompilerFlags *cf){	PyFrameObject *current_frame = PyEval_GetFrame();	int result = cf->cf_flags != 0; 	if (current_frame != NULL) {		const int codeflags = current_frame->f_code->co_flags;		const int compilerflags = codeflags & PyCF_MASK;		if (compilerflags) {			result = 1;			cf->cf_flags |= compilerflags;		}#if 0 /* future keyword */		if (codeflags & CO_GENERATOR_ALLOWED) {			result = 1;			cf->cf_flags |= CO_GENERATOR_ALLOWED;		}#endif	}	return result;} intPy_FlushLine(void){	PyObject *f = PySys_GetObject("stdout");	if (f == NULL)		return 0;	if (!PyFile_SoftSpace(f, 0))		return 0;	return PyFile_WriteString("\n", f);}  /* External interface to call any callable object.   The arg must be a tuple or NULL. */ #undef PyEval_CallObject/* for backward compatibility: export this interface */ PyObject *PyEval_CallObject(PyObject *func, PyObject *arg){	return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);}#define PyEval_CallObject(func,arg) \        PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL) PyObject *PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw){	PyObject *result; 	if (arg == NULL) {		arg = PyTuple_New(0);		if (arg == NULL)			return NULL;	}	else if (!PyTuple_Check(arg)) {		PyErr_SetString(PyExc_TypeError,				"argument list must be a tuple");		return NULL;	}	else		Py_INCREF(arg); 	if (kw != NULL && !PyDict_Check(kw)) {		PyErr_SetString(PyExc_TypeError,				"keyword list must be a dictionary");		Py_DECREF(arg);		return NULL;	} 	result = PyObject_Call(func, arg, kw);	Py_DECREF(arg);	return result;} const char *PyEval_GetFuncName(PyObject *func){	if (PyMethod_Check(func))		return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));	else if (PyFunction_Check(func))		return PyString_AsString(((PyFunctionObject*)func)->func_name);	else if (PyCFunction_Check(func))		return ((PyCFunctionObject*)func)->m_ml->ml_name;	else if (PyClass_Check(func))		return PyString_AsString(((PyClassObject*)func)->cl_name);	else if (PyInstance_Check(func)) {		return PyString_AsString(			((PyInstanceObject*)func)->in_class->cl_name);	} else {		return func->ob_type->tp_name;	}} const char *PyEval_GetFuncDesc(PyObject *func){	if (PyMethod_Check(func))		return "()";	else if (PyFunction_Check(func))		return "()";	else if (PyCFunction_Check(func))		return "()";	else if (PyClass_Check(func))		return " constructor";	else if (PyInstance_Check(func)) {		return " instance";	} else {		return " object";	}} static voiderr_args(PyObject *func, int flags, int nargs){	if (flags & METH_NOARGS)		PyErr_Format(PyExc_TypeError,			     "%.200s() takes no arguments (%d given)",			     ((PyCFunctionObject *)func)->m_ml->ml_name,			     nargs);	else		PyErr_Format(PyExc_TypeError,			     "%.200s() takes exactly one argument (%d given)",			     ((PyCFunctionObject *)func)->m_ml->ml_name,			     nargs);} #define C_TRACE(x, call) \if (tstate->use_tracing && tstate->c_profilefunc) { \	if (call_trace(tstate->c_profilefunc, \		tstate->c_profileobj, \		tstate->frame, PyTrace_C_CALL, \		func)) { \		x = NULL; \	} \	else { \		x = call; \		if (tstate->c_profilefunc != NULL) { \

Lines 3001–3500 of 4351. Previous lines