mirror of
https://github.com/python/cpython.git
synced 2024-11-27 11:55:13 +08:00
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.
This commit is contained in:
parent
626a8d034c
commit
7cc56eb524
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user