-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
gh-115231: fill __module__ for built-in class/staticmethods #115232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
9b31877
be1c7ef
1f854ee
c4dec10
1e25d84
63b05a0
777bc8d
d1650bf
d0f5bb0
e20339b
4ba1288
543a5b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Setup ``__module__`` attribute for built-in class/static methods. Patch by | ||
Sergey B Kirpichev. |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -130,7 +130,11 @@ 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_GetAttr((PyObject*)type, &_Py_ID(__module__)); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will add an overhead for use of class methods. We should try to find other solution. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
You meant for adding static/class methods?
Unlikely e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PR updated to use type_module(). For classmethods, I doubt there is another solution. Here is a micro-benchmark:
@serhiy-storchaka, does the rest (static methods) looks sane for you? If so, I'll create a separate pr just for that case. import pyperf
runner = pyperf.Runner()
n = 'from_bytes'
runner.bench_func('int.from_bytes', getattr, int, n) |
||||||||
PyErr_Clear(); | ||||||||
PyObject *result = PyCMethod_New(descr->d_method, type, mod, cls); | ||||||||
Py_XDECREF(mod); | ||||||||
return result; | ||||||||
} | ||||||||
|
||||||||
static PyObject * | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6653,7 +6653,10 @@ 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__)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might theoretically return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, thanks. Probably we could just clear the error indicator on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also take a look at There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is better to use |
||
PyErr_Clear(); | ||
PyObject *cfunc = PyCFunction_NewEx(meth, (PyObject*)type, mod); | ||
Py_XDECREF(mod); | ||
if (cfunc == NULL) { | ||
return -1; | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.