Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Lib/test/test_funcattrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,29 @@ 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 decimal
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.assertRaises(AttributeError, getattr, decimal.Decimal.exp, '__module__')
self.assertEqual(int.to_bytes.__objclass__.__module__, 'builtins')
self.assertEqual(decimal.Decimal.exp.__objclass__.__module__, 'decimal')

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

# builtin staticmethod:
self.assertEqual(bytes.maketrans.__module__, 'builtins')

def test_builtin__qualname__(self):
import time

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Setup ``__module__`` attribute for built-in static methods. Patch by Sergey
B Kirpichev.
4 changes: 3 additions & 1 deletion Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -6653,7 +6653,9 @@ 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 = PyObject_GetAttr((PyObject*)type, &_Py_ID(__module__));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might theoretically return NULL and raise AttributeError.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, thanks. Probably we could just clear the error indicator on mod == NULL.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also take a look at GetOptionalAttr

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want ignore here not just AttributeError's. I did similar fix for class methods and got a failure in the test.test_types.UnionTests.test_or_type_operator_with_bad_module().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to use type_module() for consistency with the rest of this file. It will not give a surprise if Exceptions other than AttributeError should not be ignored.

PyObject *cfunc = PyCFunction_NewEx(meth, (PyObject*)type, mod);
Py_XDECREF(mod);
if (cfunc == NULL) {
return -1;
}
Expand Down