diff options
author | Jeremy Hylton <jeremy@alum.mit.edu> | 2003-01-31 18:33:18 (GMT) |
---|---|---|
committer | Jeremy Hylton <jeremy@alum.mit.edu> | 2003-01-31 18:33:18 (GMT) |
commit | 4f0dcc9a9a4629607d4fff79912e8d0c86cd3914 (patch) | |
tree | bba33e033f6a51fec1c01a79a5f6e931f65e44f2 /Objects/funcobject.c | |
parent | 8f24cdc0d5e30e2f924ed2e8a71400fa87a70983 (diff) | |
download | cpython-4f0dcc9a9a4629607d4fff79912e8d0c86cd3914.zip cpython-4f0dcc9a9a4629607d4fff79912e8d0c86cd3914.tar.gz cpython-4f0dcc9a9a4629607d4fff79912e8d0c86cd3914.tar.bz2 |
Provide __module__ attributes for functions defined in C and Python.
__module__ is the string name of the module the function was defined
in, just like __module__ of classes. In some cases, particularly for
C functions, the __module__ may be None.
Change PyCFunction_New() from a function to a macro, but keep an
unused copy of the function around so that we don't change the binary
API.
Change pickle's save_global() to use whichmodule() if __module__ is
None, but add the __module__ logic to whichmodule() since it might be
used outside of pickle.
Diffstat (limited to 'Objects/funcobject.c')
-rw-r--r-- | Objects/funcobject.c | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/Objects/funcobject.c b/Objects/funcobject.c index 6154d99..b162fdf 100644 --- a/Objects/funcobject.c +++ b/Objects/funcobject.c @@ -14,6 +14,7 @@ PyFunction_New(PyObject *code, PyObject *globals) if (op != NULL) { PyObject *doc; PyObject *consts; + PyObject *module; op->func_weakreflist = NULL; Py_INCREF(code); op->func_code = code; @@ -34,6 +35,16 @@ PyFunction_New(PyObject *code, PyObject *globals) 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. + */ + module = PyDict_GetItemString(globals, "__name__"); + if (module) { + Py_INCREF(module); + op->func_module = module; + } } else return NULL; @@ -62,6 +73,16 @@ PyFunction_GetGlobals(PyObject *op) } 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)) { @@ -138,6 +159,7 @@ static PyMemberDef func_memberlist[] = { RESTRICTED|READONLY}, {"func_name", T_OBJECT, OFF(func_name), READONLY}, {"__name__", T_OBJECT, OFF(func_name), READONLY}, + {"__module__", T_OBJECT, OFF(func_module), READONLY}, {NULL} /* Sentinel */ }; @@ -373,6 +395,7 @@ func_dealloc(PyFunctionObject *op) 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); @@ -405,6 +428,11 @@ func_traverse(PyFunctionObject *f, visitproc visit, void *arg) if (err) return err; } + if (f->func_module) { + err = visit(f->func_module, arg); + if (err) + return err; + } if (f->func_defaults) { err = visit(f->func_defaults, arg); if (err) |