From 7cc56eb524dd83e3ba9d76accec3b0fe4010a48a Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Fri, 12 Sep 1997 20:04:46 +0000 Subject: [PATCH] When creating a class, set its __module__ attribute to the module whose name is in the current globals' __name__ variable. If __name__ is not set, ignore this. --- Objects/classobject.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Objects/classobject.c b/Objects/classobject.c index 91307f834c5..1cbbcf75c2a 100644 --- a/Objects/classobject.c +++ b/Objects/classobject.c @@ -47,16 +47,36 @@ PyClass_New(bases, dict, name) { PyClassObject *op, *dummy; static PyObject *getattrstr, *setattrstr, *delattrstr; - static PyObject *docstr; + static PyObject *docstr, *modstr, *namestr; if (docstr == NULL) { docstr= PyString_InternFromString("__doc__"); if (docstr == NULL) return NULL; } + if (modstr == NULL) { + modstr= PyString_InternFromString("__module__"); + if (modstr == NULL) + return NULL; + } + if (namestr == NULL) { + namestr= PyString_InternFromString("__name__"); + if (namestr == NULL) + return NULL; + } if (PyDict_GetItem(dict, docstr) == NULL) { if (PyDict_SetItem(dict, docstr, Py_None) < 0) return NULL; } + if (PyDict_GetItem(dict, modstr) == NULL) { + PyObject *globals = PyEval_GetGlobals(); + if (globals != NULL) { + PyObject *name = PyDict_GetItem(globals, namestr); + if (name != NULL) { + if (PyDict_SetItem(dict, modstr, name) < 0) + return NULL; + } + } + } if (bases == NULL) { bases = PyTuple_New(0); if (bases == NULL) @@ -114,6 +134,7 @@ class_lookup(cp, name, pclass) } n = PyTuple_Size(cp->cl_bases); for (i = 0; i < n; i++) { + /* XXX What if one of the bases is not a class? */ PyObject *v = class_lookup( (PyClassObject *) PyTuple_GetItem(cp->cl_bases, i), name, pclass);