https://github.com/python/cpython/commit/fa52f289a36f50d6d10e57d485e5a4f58261222b commit: fa52f289a36f50d6d10e57d485e5a4f58261222b branch: main author: neonene <53406459+neon...@users.noreply.github.com> committer: encukou <encu...@gmail.com> date: 2025-05-01T14:32:57+02:00 summary:
gh-133166: Fix missing error emission of PyType_GetModuleByDef (GH-133240) files: A Misc/NEWS.d/next/C_API/2025-05-01-01-02-11.gh-issue-133166.Ly9Ae2.rst M Lib/test/test_capi/test_type.py M Modules/_testcapi/heaptype.c M Objects/typeobject.c diff --git a/Lib/test/test_capi/test_type.py b/Lib/test/test_capi/test_type.py index 7e5d013d737ab0..3c9974c7387388 100644 --- a/Lib/test/test_capi/test_type.py +++ b/Lib/test/test_capi/test_type.py @@ -179,6 +179,22 @@ class Z(C1, A2): pass _testcapi.pytype_getbasebytoken( 'not a type', id(self), True, False) + def test_get_module_by_def(self): + heaptype = _testcapi.create_type_with_token('_testcapi.H', 0) + mod = _testcapi.pytype_getmodulebydef(heaptype) + self.assertIs(mod, _testcapi) + + class H1(heaptype): pass + mod = _testcapi.pytype_getmodulebydef(H1) + self.assertIs(mod, _testcapi) + + with self.assertRaises(TypeError): + _testcapi.pytype_getmodulebydef(int) + + class H2(int): pass + with self.assertRaises(TypeError): + _testcapi.pytype_getmodulebydef(H2) + def test_freeze(self): # test PyType_Freeze() type_freeze = _testcapi.type_freeze diff --git a/Misc/NEWS.d/next/C_API/2025-05-01-01-02-11.gh-issue-133166.Ly9Ae2.rst b/Misc/NEWS.d/next/C_API/2025-05-01-01-02-11.gh-issue-133166.Ly9Ae2.rst new file mode 100644 index 00000000000000..976b823b441067 --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2025-05-01-01-02-11.gh-issue-133166.Ly9Ae2.rst @@ -0,0 +1,2 @@ +Fix regression where :c:func:`PyType_GetModuleByDef` returns NULL without +setting :exc:`TypeError` when a static type is passed. diff --git a/Modules/_testcapi/heaptype.c b/Modules/_testcapi/heaptype.c index fd061f2d23a47c..257e0256655976 100644 --- a/Modules/_testcapi/heaptype.c +++ b/Modules/_testcapi/heaptype.c @@ -521,6 +521,13 @@ pytype_getbasebytoken(PyObject *self, PyObject *args) return NULL; } +static PyObject * +pytype_getmodulebydef(PyObject *self, PyObject *type) +{ + PyObject *mod = PyType_GetModuleByDef((PyTypeObject *)type, _testcapimodule); + return Py_XNewRef(mod); +} + static PyMethodDef TestMethods[] = { {"pytype_fromspec_meta", pytype_fromspec_meta, METH_O}, @@ -538,6 +545,7 @@ static PyMethodDef TestMethods[] = { {"create_type_with_token", create_type_with_token, METH_VARARGS}, {"get_tp_token", get_tp_token, METH_O}, {"pytype_getbasebytoken", pytype_getbasebytoken, METH_VARARGS}, + {"pytype_getmodulebydef", pytype_getmodulebydef, METH_O}, {NULL}, }; diff --git a/Objects/typeobject.c b/Objects/typeobject.c index ff68311281ce6f..a7ab69fef4c721 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -5399,7 +5399,7 @@ PyType_GetModuleByDef(PyTypeObject *type, PyModuleDef *def) if (!_PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE)) { // type_ready_mro() ensures that no heap type is // contained in a static type MRO. - return NULL; + goto error; } else { PyHeapTypeObject *ht = (PyHeapTypeObject*)type; @@ -5439,13 +5439,15 @@ PyType_GetModuleByDef(PyTypeObject *type, PyModuleDef *def) } END_TYPE_LOCK(); - if (res == NULL) { - PyErr_Format( - PyExc_TypeError, - "PyType_GetModuleByDef: No superclass of '%s' has the given module", - type->tp_name); + if (res != NULL) { + return res; } - return res; +error: + PyErr_Format( + PyExc_TypeError, + "PyType_GetModuleByDef: No superclass of '%s' has the given module", + type->tp_name); + return NULL; } _______________________________________________ Python-checkins mailing list -- python-checkins@python.org To unsubscribe send an email to python-checkins-le...@python.org https://mail.python.org/mailman3/lists/python-checkins.python.org/ Member address: arch...@mail-archive.com