Skip to content

Commit

Permalink
fill __module__ attribute for built-in classmethods too
Browse files Browse the repository at this point in the history
  • Loading branch information
skirpichev committed Feb 11, 2024
1 parent 1f854ee commit c4dec10
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Lib/test/test_funcattrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,9 @@ def test_builtin__module__(self):
self.assertEqual(decimal.Decimal.exp.__objclass__.__module__, 'decimal')

# builtin classmethod:
self.assertEqual(int.from_bytes.__module__, None)
self.assertEqual(int.from_bytes.__module__, 'builtins')
self.assertEqual(int.from_bytes.__self__.__module__, 'builtins')
self.assertEqual(decimal.Decimal.from_float.__module__, None)
self.assertEqual(decimal.Decimal.from_float.__module__, 'decimal')
self.assertEqual(decimal.Decimal.from_float.__self__.__module__, 'decimal')

# builtin staticmethod:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Setup ``__module__`` attribute for built-in static methods. Patch by Sergey
B Kirpichev.
Setup ``__module__`` attribute for built-in class/static methods. Patch by
Sergey B Kirpichev.
7 changes: 6 additions & 1 deletion Objects/descrobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ classmethod_get(PyObject *self, PyObject *obj, PyObject *type)
if (descr->d_method->ml_flags & METH_METHOD) {
cls = descr->d_common.d_type;
}
return PyCMethod_New(descr->d_method, type, NULL, cls);
PyObject *mod;
PyObject_GetOptionalAttr((PyObject*)type, &_Py_ID(__module__), &mod);
PyErr_Clear();
PyObject *result = PyCMethod_New(descr->d_method, type, mod, cls);
Py_XDECREF(mod);
return result;
}

static PyObject *
Expand Down

0 comments on commit c4dec10

Please sign in to comment.