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
21 changes: 21 additions & 0 deletions Lib/test/test_opcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2008,6 +2008,27 @@ class MyList(list): pass
self.assert_no_opcode(my_list_append, "CALL_LIST_APPEND")
self.assert_no_opcode(my_list_append, "CALL")

@cpython_only
@requires_specialization
def test_load_and_call_classmethod(self):

r = range(_testinternalcapi.SPECIALIZATION_THRESHOLD)

class C:
@classmethod
def val(self):
return 1

def class_method_call():
for _ in r:
C.val()

class_method_call()
# gh-148608: To improve specialization, the classmethod should've become unbound.
self.assert_specialized(class_method_call, "CALL_PY_EXACT_ARGS")
self.assert_no_opcode(class_method_call, "CALL_BOUND_METHOD_EXACT_ARGS")
self.assert_no_opcode(class_method_call, "CALL")

@cpython_only
@requires_specialization
def test_load_attr_module_with_getattr(self):
Expand Down
9 changes: 9 additions & 0 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -1773,6 +1773,15 @@ _PyObject_GetMethodStackRef(PyThreadState *ts, _PyStackRef *self,
PyObject *res = PyObject_GetAttr(obj, name);
PyStackRef_CLEAR(*self);
if (res != NULL) {
// gh-148608: If it's a method, unbind it right now to improve
// specialization later on.
if (Py_IS_TYPE(res, &PyMethod_Type)) {
PyMethodObject *as_meth = ((PyMethodObject *)res);
*method = PyStackRef_FromPyObjectNew(as_meth->im_func);
*self = PyStackRef_FromPyObjectNew(as_meth->im_self);
Py_DECREF(res);
return 1;
}
*method = PyStackRef_FromPyObjectSteal(res);
return 0;
}
Expand Down
Loading