https://github.com/python/cpython/commit/15c9f2491d235ca1bebfbfbdefdd8d6ac3869298
commit: 15c9f2491d235ca1bebfbfbdefdd8d6ac3869298
branch: main
author: Sergey B Kirpichev <[email protected]>
committer: vstinner <[email protected]>
date: 2026-02-02T12:34:02+01:00
summary:

gh-115231: Fill __module__ for built-in staticmethods (#115232)

Co-authored-by: Nikita Sobolev <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2024-02-10-05-42-26.gh-issue-115231.6T7dzi.rst
M Lib/test/test_funcattrs.py
M Objects/typeobject.c

diff --git a/Lib/test/test_funcattrs.py b/Lib/test/test_funcattrs.py
index 375f456dfde834..fe14e7cb342c9e 100644
--- a/Lib/test/test_funcattrs.py
+++ b/Lib/test/test_funcattrs.py
@@ -459,6 +459,24 @@ class BuiltinFunctionPropertiesTest(unittest.TestCase):
     # XXX Not sure where this should really go since I can't find a
     # test module specifically for builtin_function_or_method.
 
+    def test_builtin__module__(self):
+        import math
+
+        # builtin function:
+        self.assertEqual(len.__module__, 'builtins')
+        self.assertEqual(math.sin.__module__, 'math')
+
+        # instance method:
+        self.assertRaises(AttributeError, getattr, int.to_bytes, '__module__')
+        self.assertEqual(int.to_bytes.__objclass__.__module__, 'builtins')
+
+        # builtin classmethod:
+        self.assertEqual(int.from_bytes.__module__, None)
+        self.assertEqual(int.from_bytes.__self__.__module__, 'builtins')
+
+        # builtin staticmethod:
+        self.assertEqual(bytes.maketrans.__module__, 'builtins')
+
     def test_builtin__qualname__(self):
         import time
 
diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2024-02-10-05-42-26.gh-issue-115231.6T7dzi.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2024-02-10-05-42-26.gh-issue-115231.6T7dzi.rst
new file mode 100644
index 00000000000000..0e41bc963bebec
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2024-02-10-05-42-26.gh-issue-115231.6T7dzi.rst
@@ -0,0 +1,2 @@
+Setup ``__module__`` attribute for built-in static methods.  Patch by Sergey
+B Kirpichev.
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 54263fd603ed73..2905b9ba56241e 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -8361,7 +8361,15 @@ type_add_method(PyTypeObject *type, PyMethodDef *meth)
         descr = PyDescr_NewClassMethod(type, meth);
     }
     else if (meth->ml_flags & METH_STATIC) {
-        PyObject *cfunc = PyCFunction_NewEx(meth, (PyObject*)type, NULL);
+        PyObject *mod = type_module(type);
+        if (mod == NULL) {
+            if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
+                return -1;
+            }
+            PyErr_Clear();
+        }
+        PyObject *cfunc = PyCFunction_NewEx(meth, (PyObject*)type, mod);
+        Py_XDECREF(mod);
         if (cfunc == NULL) {
             return -1;
         }

_______________________________________________
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