]> granicus.if.org Git - python/commitdiff
When creating a class, set its __module__ attribute to the module
authorGuido van Rossum <guido@python.org>
Fri, 12 Sep 1997 20:04:46 +0000 (20:04 +0000)
committerGuido van Rossum <guido@python.org>
Fri, 12 Sep 1997 20:04:46 +0000 (20:04 +0000)
whose name is in the current globals' __name__ variable.  If __name__
is not set, ignore this.

Objects/classobject.c

index 91307f834c5d00366d8b4285ee944f950d3fc131..1cbbcf75c2a64170b7ab77b5e6e68b8fcdc3a533 100644 (file)
@@ -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);