mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
Nudge getting __module__ and __name__ for new-style classes so that
the results of *setting* __name__ are not so surprising. If people can suggest more tests, that'd be grand, or is what's there sufficient?
This commit is contained in:
parent
deaba57009
commit
ade8c8b2c3
2 changed files with 33 additions and 18 deletions
|
@ -3603,7 +3603,15 @@ def mutable_names():
|
|||
class C(object):
|
||||
pass
|
||||
|
||||
C.__name__ = 'C'
|
||||
# C.__module__ could be 'test_descr' or '__main__'
|
||||
mod = C.__module__
|
||||
|
||||
C.__name__ = 'D'
|
||||
vereq((C.__module__, C.__name__), (mod, 'D'))
|
||||
|
||||
C.__name__ = 'D.E'
|
||||
vereq((C.__module__, C.__name__), (mod, 'D.E'))
|
||||
|
||||
|
||||
def test_main():
|
||||
do_this_first()
|
||||
|
|
|
@ -41,12 +41,20 @@ type_name(PyTypeObject *type, void *context)
|
|||
{
|
||||
char *s;
|
||||
|
||||
s = strrchr(type->tp_name, '.');
|
||||
if (s == NULL)
|
||||
s = type->tp_name;
|
||||
else
|
||||
s++;
|
||||
return PyString_FromString(s);
|
||||
if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
|
||||
etype* et = (etype*)type;
|
||||
|
||||
Py_INCREF(et->name);
|
||||
return et->name;
|
||||
}
|
||||
else {
|
||||
s = strrchr(type->tp_name, '.');
|
||||
if (s == NULL)
|
||||
s = type->tp_name;
|
||||
else
|
||||
s++;
|
||||
return PyString_FromString(s);
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
|
@ -95,19 +103,18 @@ type_module(PyTypeObject *type, void *context)
|
|||
PyObject *mod;
|
||||
char *s;
|
||||
|
||||
s = strrchr(type->tp_name, '.');
|
||||
if (s != NULL)
|
||||
return PyString_FromStringAndSize(type->tp_name,
|
||||
(int)(s - type->tp_name));
|
||||
if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
|
||||
return PyString_FromString("__builtin__");
|
||||
mod = PyDict_GetItemString(type->tp_dict, "__module__");
|
||||
if (mod != NULL) {
|
||||
Py_INCREF(mod);
|
||||
if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
|
||||
mod = PyDict_GetItemString(type->tp_dict, "__module__");
|
||||
Py_XINCREF(mod);
|
||||
return mod;
|
||||
}
|
||||
PyErr_SetString(PyExc_AttributeError, "__module__");
|
||||
return NULL;
|
||||
else {
|
||||
s = strrchr(type->tp_name, '.');
|
||||
if (s != NULL)
|
||||
return PyString_FromStringAndSize(
|
||||
type->tp_name, (int)(s - type->tp_name));
|
||||
return PyString_FromString("__builtin__");
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue