bpo-40703: Let PyType_FromSpec() set "type.__module__" only if it is not set yet. (GH-20273)

This commit is contained in:
scoder 2020-06-10 18:09:01 +02:00 committed by GitHub
parent ec88e1bca8
commit 24b8bad6d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 15 deletions

View File

@ -0,0 +1,2 @@
The PyType_FromSpec*() functions no longer overwrite the type's "__module__" attribute
if it is set via "Py_tp_members" or "Py_tp_getset".

View File

@ -3067,23 +3067,28 @@ PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)
} }
/* Set type.__module__ */ /* Set type.__module__ */
s = strrchr(spec->name, '.'); if (_PyDict_GetItemIdWithError(type->tp_dict, &PyId___module__) == NULL) {
if (s != NULL) { if (PyErr_Occurred()) {
int err;
modname = PyUnicode_FromStringAndSize(
spec->name, (Py_ssize_t)(s - spec->name));
if (modname == NULL) {
goto fail; goto fail;
} }
err = _PyDict_SetItemId(type->tp_dict, &PyId___module__, modname); s = strrchr(spec->name, '.');
Py_DECREF(modname); if (s != NULL) {
if (err != 0) int err;
goto fail; modname = PyUnicode_FromStringAndSize(
} else { spec->name, (Py_ssize_t)(s - spec->name));
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, if (modname == NULL) {
"builtin type %.200s has no __module__ attribute", goto fail;
spec->name)) }
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; return (PyObject*)res;