mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
bpo-40703: Let PyType_FromSpec() set "type.__module__" only if it is not set yet. (GH-20273) (GH-20782)
(cherry picked from commit 24b8bad6d3
)
This commit is contained in:
parent
3b97d1becb
commit
9419158a3e
2 changed files with 22 additions and 15 deletions
|
@ -3061,23 +3061,28 @@ PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)
|
|||
}
|
||||
|
||||
/* Set type.__module__ */
|
||||
s = strrchr(spec->name, '.');
|
||||
if (s != NULL) {
|
||||
int err;
|
||||
modname = PyUnicode_FromStringAndSize(
|
||||
spec->name, (Py_ssize_t)(s - spec->name));
|
||||
if (modname == NULL) {
|
||||
if (_PyDict_GetItemIdWithError(type->tp_dict, &PyId___module__) == NULL) {
|
||||
if (PyErr_Occurred()) {
|
||||
goto fail;
|
||||
}
|
||||
err = _PyDict_SetItemId(type->tp_dict, &PyId___module__, modname);
|
||||
Py_DECREF(modname);
|
||||
if (err != 0)
|
||||
goto fail;
|
||||
} else {
|
||||
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
|
||||
"builtin type %.200s has no __module__ attribute",
|
||||
spec->name))
|
||||
goto fail;
|
||||
s = strrchr(spec->name, '.');
|
||||
if (s != NULL) {
|
||||
int err;
|
||||
modname = PyUnicode_FromStringAndSize(
|
||||
spec->name, (Py_ssize_t)(s - spec->name));
|
||||
if (modname == NULL) {
|
||||
goto fail;
|
||||
}
|
||||
err = _PyDict_SetItemId(type->tp_dict, &PyId___module__, modname);
|
||||
Py_DECREF(modname);
|
||||
if (err != 0)
|
||||
goto fail;
|
||||
} else {
|
||||
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
|
||||
"builtin type %.200s has no __module__ attribute",
|
||||
spec->name))
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
|
||||
return (PyObject*)res;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue