]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-148907: fix performance regression in `PyType_GetModuleByDef` on free-threading...
authorKumar Aditya <kumaraditya@python.org>
Thu, 23 Apr 2026 11:12:57 +0000 (16:42 +0530)
committerGitHub <noreply@github.com>
Thu, 23 Apr 2026 11:12:57 +0000 (16:42 +0530)
Objects/typeobject.c

index 08b95cfbc6ce5904745239df0e1e7a1f377654e2..fb3c7101410683561c5f317161c3b89cbcdaf26a 100644 (file)
@@ -5878,7 +5878,13 @@ PyType_GetModuleByToken_DuringGC(PyTypeObject *type, const void *token)
 PyObject *
 PyType_GetModuleByToken(PyTypeObject *type, const void *token)
 {
-    PyObject *mod = PyType_GetModuleByToken_DuringGC(type, token);
+    return Py_XNewRef(PyType_GetModuleByDef(type, (PyModuleDef *)token));
+}
+
+PyObject *
+PyType_GetModuleByDef(PyTypeObject *type, PyModuleDef *def)
+{
+    PyObject *mod = PyType_GetModuleByToken_DuringGC(type, def);
     if (!mod) {
         PyErr_Format(
             PyExc_TypeError,
@@ -5886,14 +5892,6 @@ PyType_GetModuleByToken(PyTypeObject *type, const void *token)
             type->tp_name);
         return NULL;
     }
-    return Py_NewRef(mod);
-}
-
-PyObject *
-PyType_GetModuleByDef(PyTypeObject *type, PyModuleDef *def)
-{
-    PyObject *mod = PyType_GetModuleByToken(type, def);
-    Py_XDECREF(mod);  // return borrowed ref
     return mod;
 }