cinap_lenrekadd hg and python4bb9e2215 years agoHistory
888 lines · 20.9 KB · C
RawDownload
 /* Function object implementation */ #include "Python.h"#include "code.h"#include "eval.h"#include "structmember.h" PyObject *PyFunction_New(PyObject *code, PyObject *globals){	PyFunctionObject *op = PyObject_GC_New(PyFunctionObject,					    &PyFunction_Type);	static PyObject *__name__ = 0;	if (op != NULL) {		PyObject *doc;		PyObject *consts;		PyObject *module;		op->func_weakreflist = NULL;		Py_INCREF(code);		op->func_code = code;		Py_INCREF(globals);		op->func_globals = globals;		op->func_name = ((PyCodeObject *)code)->co_name;		Py_INCREF(op->func_name);		op->func_defaults = NULL; /* No default arguments */		op->func_closure = NULL;		consts = ((PyCodeObject *)code)->co_consts;		if (PyTuple_Size(consts) >= 1) {			doc = PyTuple_GetItem(consts, 0);			if (!PyString_Check(doc) && !PyUnicode_Check(doc))				doc = Py_None;		}		else			doc = Py_None;		Py_INCREF(doc);		op->func_doc = doc;		op->func_dict = NULL;		op->func_module = NULL; 		/* __module__: If module name is in globals, use it.		   Otherwise, use None.		*/		if (!__name__) {			__name__ = PyString_InternFromString("__name__");			if (!__name__) {				Py_DECREF(op);				return NULL;			}		}		module = PyDict_GetItem(globals, __name__);		if (module) {		    Py_INCREF(module);		    op->func_module = module;		}	}	else		return NULL;	_PyObject_GC_TRACK(op);	return (PyObject *)op;} PyObject *PyFunction_GetCode(PyObject *op){	if (!PyFunction_Check(op)) {		PyErr_BadInternalCall();		return NULL;	}	return ((PyFunctionObject *) op) -> func_code;} PyObject *PyFunction_GetGlobals(PyObject *op){	if (!PyFunction_Check(op)) {		PyErr_BadInternalCall();		return NULL;	}	return ((PyFunctionObject *) op) -> func_globals;} PyObject *PyFunction_GetModule(PyObject *op){	if (!PyFunction_Check(op)) {		PyErr_BadInternalCall();		return NULL;	}	return ((PyFunctionObject *) op) -> func_module;} PyObject *PyFunction_GetDefaults(PyObject *op){	if (!PyFunction_Check(op)) {		PyErr_BadInternalCall();		return NULL;	}	return ((PyFunctionObject *) op) -> func_defaults;} intPyFunction_SetDefaults(PyObject *op, PyObject *defaults){	if (!PyFunction_Check(op)) {		PyErr_BadInternalCall();		return -1;	}	if (defaults == Py_None)		defaults = NULL;	else if (defaults && PyTuple_Check(defaults)) {		Py_INCREF(defaults);	}	else {		PyErr_SetString(PyExc_SystemError, "non-tuple default args");		return -1;	}	Py_XDECREF(((PyFunctionObject *) op) -> func_defaults);	((PyFunctionObject *) op) -> func_defaults = defaults;	return 0;} PyObject *PyFunction_GetClosure(PyObject *op){	if (!PyFunction_Check(op)) {		PyErr_BadInternalCall();		return NULL;	}	return ((PyFunctionObject *) op) -> func_closure;} intPyFunction_SetClosure(PyObject *op, PyObject *closure){	if (!PyFunction_Check(op)) {		PyErr_BadInternalCall();		return -1;	}	if (closure == Py_None)		closure = NULL;	else if (PyTuple_Check(closure)) {		Py_INCREF(closure);	}	else {		PyErr_Format(PyExc_SystemError, 			     "expected tuple for closure, got '%.100s'",			     closure->ob_type->tp_name);		return -1;	}	Py_XDECREF(((PyFunctionObject *) op) -> func_closure);	((PyFunctionObject *) op) -> func_closure = closure;	return 0;} /* Methods */ #define OFF(x) offsetof(PyFunctionObject, x) static PyMemberDef func_memberlist[] = {        {"func_closure",  T_OBJECT,     OFF(func_closure),	 RESTRICTED|READONLY},        {"func_doc",      T_OBJECT,     OFF(func_doc), WRITE_RESTRICTED},        {"__doc__",       T_OBJECT,     OFF(func_doc), WRITE_RESTRICTED},        {"func_globals",  T_OBJECT,     OFF(func_globals),	 RESTRICTED|READONLY},        {"__module__",    T_OBJECT,     OFF(func_module), WRITE_RESTRICTED},        {NULL}  /* Sentinel */}; static intrestricted(void){	if (!PyEval_GetRestricted())		return 0;	PyErr_SetString(PyExc_RuntimeError,		"function attributes not accessible in restricted mode");	return 1;} static PyObject *func_get_dict(PyFunctionObject *op){	if (restricted())		return NULL;	if (op->func_dict == NULL) {		op->func_dict = PyDict_New();		if (op->func_dict == NULL)			return NULL;	}	Py_INCREF(op->func_dict);	return op->func_dict;} static intfunc_set_dict(PyFunctionObject *op, PyObject *value){	PyObject *tmp; 	if (restricted())		return -1;	/* It is illegal to del f.func_dict */	if (value == NULL) {		PyErr_SetString(PyExc_TypeError,				"function's dictionary may not be deleted");		return -1;	}	/* Can only set func_dict to a dictionary */	if (!PyDict_Check(value)) {		PyErr_SetString(PyExc_TypeError,				"setting function's dictionary to a non-dict");		return -1;	}	tmp = op->func_dict;	Py_INCREF(value);	op->func_dict = value;	Py_XDECREF(tmp);	return 0;} static PyObject *func_get_code(PyFunctionObject *op){	if (restricted())		return NULL;	Py_INCREF(op->func_code);	return op->func_code;} static intfunc_set_code(PyFunctionObject *op, PyObject *value){	PyObject *tmp;	Py_ssize_t nfree, nclosure; 	if (restricted())		return -1;	/* Not legal to del f.func_code or to set it to anything	 * other than a code object. */	if (value == NULL || !PyCode_Check(value)) {		PyErr_SetString(PyExc_TypeError,				"func_code must be set to a code object");		return -1;	}	nfree = PyCode_GetNumFree((PyCodeObject *)value);	nclosure = (op->func_closure == NULL ? 0 :		    PyTuple_GET_SIZE(op->func_closure));	if (nclosure != nfree) {		PyErr_Format(PyExc_ValueError,			     "%s() requires a code object with %zd free vars,"			     " not %zd",			     PyString_AsString(op->func_name),			     nclosure, nfree);		return -1;	}	tmp = op->func_code;	Py_INCREF(value);	op->func_code = value;	Py_DECREF(tmp);	return 0;} static PyObject *func_get_name(PyFunctionObject *op){	Py_INCREF(op->func_name);	return op->func_name;} static intfunc_set_name(PyFunctionObject *op, PyObject *value){	PyObject *tmp; 	if (restricted())		return -1;	/* Not legal to del f.func_name or to set it to anything	 * other than a string object. */	if (value == NULL || !PyString_Check(value)) {		PyErr_SetString(PyExc_TypeError,				"func_name must be set to a string object");		return -1;	}	tmp = op->func_name;	Py_INCREF(value);	op->func_name = value;	Py_DECREF(tmp);	return 0;} static PyObject *func_get_defaults(PyFunctionObject *op){	if (restricted())		return NULL;	if (op->func_defaults == NULL) {		Py_INCREF(Py_None);		return Py_None;	}	Py_INCREF(op->func_defaults);	return op->func_defaults;} static intfunc_set_defaults(PyFunctionObject *op, PyObject *value){	PyObject *tmp; 	if (restricted())		return -1;	/* Legal to del f.func_defaults.	 * Can only set func_defaults to NULL or a tuple. */	if (value == Py_None)		value = NULL;	if (value != NULL && !PyTuple_Check(value)) {		PyErr_SetString(PyExc_TypeError,				"func_defaults must be set to a tuple object");		return -1;	}	tmp = op->func_defaults;	Py_XINCREF(value);	op->func_defaults = value;	Py_XDECREF(tmp);	return 0;} static PyGetSetDef func_getsetlist[] = {        {"func_code", (getter)func_get_code, (setter)func_set_code},        {"func_defaults", (getter)func_get_defaults,	 (setter)func_set_defaults},	{"func_dict", (getter)func_get_dict, (setter)func_set_dict},	{"__dict__", (getter)func_get_dict, (setter)func_set_dict},	{"func_name", (getter)func_get_name, (setter)func_set_name},	{"__name__", (getter)func_get_name, (setter)func_set_name},	{NULL} /* Sentinel */}; PyDoc_STRVAR(func_doc,"function(code, globals[, name[, argdefs[, closure]]])\n\\n\Create a function object from a code object and a dictionary.\n\The optional name string overrides the name from the code object.\n\The optional argdefs tuple specifies the default argument values.\n\The optional closure tuple supplies the bindings for free variables."); /* func_new() maintains the following invariants for closures.  The   closure must correspond to the free variables of the code object.      if len(code.co_freevars) == 0:            closure = NULL   else:           len(closure) == len(code.co_freevars)   for every elt in closure, type(elt) == cell*/ static PyObject *func_new(PyTypeObject* type, PyObject* args, PyObject* kw){	PyCodeObject *code;	PyObject *globals;	PyObject *name = Py_None;	PyObject *defaults = Py_None;	PyObject *closure = Py_None;	PyFunctionObject *newfunc;	Py_ssize_t nfree, nclosure;	static char *kwlist[] = {"code", "globals", "name",				 "argdefs", "closure", 0}; 	if (!PyArg_ParseTupleAndKeywords(args, kw, "O!O!|OOO:function",			      kwlist,			      &PyCode_Type, &code,			      &PyDict_Type, &globals,			      &name, &defaults, &closure))		return NULL;	if (name != Py_None && !PyString_Check(name)) {		PyErr_SetString(PyExc_TypeError,				"arg 3 (name) must be None or string");		return NULL;	}	if (defaults != Py_None && !PyTuple_Check(defaults)) {		PyErr_SetString(PyExc_TypeError,				"arg 4 (defaults) must be None or tuple");		return NULL;	}	nfree = PyTuple_GET_SIZE(code->co_freevars);	if (!PyTuple_Check(closure)) {		if (nfree && closure == Py_None) {			PyErr_SetString(PyExc_TypeError,					"arg 5 (closure) must be tuple");			return NULL;		}		else if (closure != Py_None) {			PyErr_SetString(PyExc_TypeError,				"arg 5 (closure) must be None or tuple");			return NULL;		}	} 	/* check that the closure is well-formed */	nclosure = closure == Py_None ? 0 : PyTuple_GET_SIZE(closure);	if (nfree != nclosure)		return PyErr_Format(PyExc_ValueError,				    "%s requires closure of length %zd, not %zd",				    PyString_AS_STRING(code->co_name),				    nfree, nclosure);	if (nclosure) {		Py_ssize_t i;		for (i = 0; i < nclosure; i++) {			PyObject *o = PyTuple_GET_ITEM(closure, i);			if (!PyCell_Check(o)) {				return PyErr_Format(PyExc_TypeError,				    "arg 5 (closure) expected cell, found %s",						    o->ob_type->tp_name);			}		}	}		newfunc = (PyFunctionObject *)PyFunction_New((PyObject *)code, 						     globals);	if (newfunc == NULL)		return NULL;		if (name != Py_None) {		Py_INCREF(name);		Py_DECREF(newfunc->func_name);		newfunc->func_name = name;	}	if (defaults != Py_None) {		Py_INCREF(defaults);		newfunc->func_defaults  = defaults;	}	if (closure != Py_None) {		Py_INCREF(closure);		newfunc->func_closure = closure;	} 	return (PyObject *)newfunc;} static voidfunc_dealloc(PyFunctionObject *op){	_PyObject_GC_UNTRACK(op);	if (op->func_weakreflist != NULL)		PyObject_ClearWeakRefs((PyObject *) op);	Py_DECREF(op->func_code);	Py_DECREF(op->func_globals);	Py_XDECREF(op->func_module);	Py_DECREF(op->func_name);	Py_XDECREF(op->func_defaults);	Py_XDECREF(op->func_doc);	Py_XDECREF(op->func_dict);	Py_XDECREF(op->func_closure);	PyObject_GC_Del(op);} static PyObject*func_repr(PyFunctionObject *op){	return PyString_FromFormat("<function %s at %p>",				   PyString_AsString(op->func_name),				   op);} static intfunc_traverse(PyFunctionObject *f, visitproc visit, void *arg){	Py_VISIT(f->func_code);	Py_VISIT(f->func_globals);	Py_VISIT(f->func_module);	Py_VISIT(f->func_defaults);	Py_VISIT(f->func_doc);	Py_VISIT(f->func_name);	Py_VISIT(f->func_dict);	Py_VISIT(f->func_closure);	return 0;} static PyObject *function_call(PyObject *func, PyObject *arg, PyObject *kw){	PyObject *result;	PyObject *argdefs;	PyObject **d, **k;	Py_ssize_t nk, nd; 	argdefs = PyFunction_GET_DEFAULTS(func);	if (argdefs != NULL && PyTuple_Check(argdefs)) {		d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);		nd = PyTuple_Size(argdefs);	}	else {		d = NULL;		nd = 0;	} 	if (kw != NULL && PyDict_Check(kw)) {		Py_ssize_t pos, i;		nk = PyDict_Size(kw);

Lines 1–500 of 888.