Skip to content

GH-114806. Don't specialize calls to classes with metaclasses. #114870

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

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 16 additions & 0 deletions Lib/test/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,22 @@ def add_one_level():
with self.assertRaises(RecursionError):
add_one_level()

def testMetaclassCallOptimization(self):
calls = 0

class TypeMetaclass(type):
def __call__(cls, *args, **kwargs):
nonlocal calls
calls += 1
return type.__call__(cls, *args, **kwargs)

class Type(metaclass=TypeMetaclass):
def __init__(self, obj):
self._obj = obj

for i in range(100):
Type(i)
self.assertEqual(calls, 100)

if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
No longer specialize calls to classes, if those classes have metaclasses.
Fixes bug where the ``__call__`` method of the metaclass was not being
called.
5 changes: 5 additions & 0 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ _PyCode_Quicken(PyCodeObject *code)
#define SPEC_FAIL_CALL_METHOD_WRAPPER 28
#define SPEC_FAIL_CALL_OPERATOR_WRAPPER 29
#define SPEC_FAIL_CALL_INIT_NOT_SIMPLE 30
#define SPEC_FAIL_CALL_METACLASS 31

/* COMPARE_OP */
#define SPEC_FAIL_COMPARE_OP_DIFFERENT_TYPES 12
Expand Down Expand Up @@ -1757,6 +1758,10 @@ specialize_class_call(PyObject *callable, _Py_CODEUNIT *instr, int nargs)
SPEC_FAIL_CALL_STR : SPEC_FAIL_CALL_CLASS_NO_VECTORCALL);
return -1;
}
if (Py_TYPE(tp) != &PyType_Type) {
SPECIALIZATION_FAIL(CALL, SPEC_FAIL_CALL_METACLASS);
return -1;
}
if (tp->tp_new == PyBaseObject_Type.tp_new) {
PyFunctionObject *init = get_init_for_simple_managed_python_class(tp);
if (type_get_version(tp, CALL) == 0) {
Expand Down