Skip to content

Commit ff758ab

Browse files
committed
pythongh-115231: fill __module__ for built-in class/staticmethods
1 parent 3dc4125 commit ff758ab

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

Lib/test/test_funcattrs.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,29 @@ class BuiltinFunctionPropertiesTest(unittest.TestCase):
453453
# XXX Not sure where this should really go since I can't find a
454454
# test module specifically for builtin_function_or_method.
455455

456+
def test_builtin__module__(self):
457+
import decimal
458+
import math
459+
460+
# builtin function:
461+
self.assertEqual(len.__module__, 'builtins')
462+
self.assertEqual(math.sin.__module__, 'math')
463+
464+
# instance method:
465+
self.assertRaises(AttributeError, getattr, int.to_bytes, '__module__')
466+
self.assertRaises(AttributeError, getattr, decimal.Decimal.exp, '__module__')
467+
self.assertEqual(int.to_bytes.__objclass__.__module__, 'builtins')
468+
self.assertEqual(decimal.Decimal.exp.__objclass__.__module__, 'decimal')
469+
470+
# builtin classmethod:
471+
self.assertEqual(int.from_bytes.__module__, 'builtins')
472+
self.assertEqual(int.from_bytes.__self__.__module__, 'builtins')
473+
self.assertEqual(decimal.Decimal.from_float.__module__, 'decimal')
474+
self.assertEqual(decimal.Decimal.from_float.__self__.__module__, 'decimal')
475+
476+
# builtin staticmethod:
477+
self.assertEqual(bytes.maketrans.__module__, 'builtins')
478+
456479
def test_builtin__qualname__(self):
457480
import time
458481

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Setup ``__module__`` attribute for built-in class/static methods. Patch by
2+
Sergey B Kirpichev.

Objects/descrobject.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,11 @@ classmethod_get(PyObject *self, PyObject *obj, PyObject *type)
130130
if (descr->d_method->ml_flags & METH_METHOD) {
131131
cls = descr->d_common.d_type;
132132
}
133-
return PyCMethod_New(descr->d_method, type, NULL, cls);
133+
PyObject *mod = PyObject_GetAttr((PyObject*)type, &_Py_ID(__module__));
134+
PyErr_Clear();
135+
PyObject *result = PyCMethod_New(descr->d_method, type, mod, cls);
136+
Py_XDECREF(mod);
137+
return result;
134138
}
135139

136140
static PyObject *

Objects/typeobject.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6886,7 +6886,10 @@ type_add_method(PyTypeObject *type, PyMethodDef *meth)
68866886
descr = PyDescr_NewClassMethod(type, meth);
68876887
}
68886888
else if (meth->ml_flags & METH_STATIC) {
6889-
PyObject *cfunc = PyCFunction_NewEx(meth, (PyObject*)type, NULL);
6889+
PyObject *mod = PyObject_GetAttr((PyObject*)type, &_Py_ID(__module__));
6890+
PyErr_Clear();
6891+
PyObject *cfunc = PyCFunction_NewEx(meth, (PyObject*)type, mod);
6892+
Py_XDECREF(mod);
68906893
if (cfunc == NULL) {
68916894
return -1;
68926895
}

0 commit comments

Comments
 (0)