https://github.com/python/cpython/commit/29917d51ab41df9a00f1bb35fa9d3e1392ac48e8
commit: 29917d51ab41df9a00f1bb35fa9d3e1392ac48e8
branch: main
author: Kumar Aditya <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2026-04-23T16:42:57+05:30
summary:

gh-148907: fix performance regression in `PyType_GetModuleByDef` on 
free-threading (#148908)

files:
M Objects/typeobject.c

diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 08b95cfbc6ce59..fb3c7101410683 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -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;
 }
 

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to