Skip to content
Open
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
20 changes: 20 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3300,6 +3300,26 @@ def testfunc(n):
self.assertIn("_CALL_BUILTIN_CLASS", uops)
self.assertNotIn("_GUARD_CALLABLE_BUILTIN_CLASS", uops)

def test_call_builtin_class_custom_metaclass(self):
_testcapi = import_helper.import_module("_testcapi")

def testfunc(cls, n):
for _ in range(n):
result = cls()
return result

Meta = _testcapi.make_vectorcall_class(type)
C = type.__new__(Meta, "C", (), {})
Meta.set_vectorcall(C, type)

testfunc(list, TIER2_THRESHOLD)
ex = get_first_executor(testfunc)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertIn("_GUARD_CALLABLE_BUILTIN_CLASS", uops)
self.assertIn("_CALL_BUILTIN_CLASS", uops)
self.assertEqual(testfunc(C, 16), "tp_call")

def test_call_builtin_o(self):
def testfunc(n):
x = 0
Expand Down
36 changes: 36 additions & 0 deletions Lib/test/test_opcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,42 @@ def __init__(self, arg):


class TestCallCache(TestBase):
@requires_jit_disabled
@requires_specialization
def test_call_builtin_class_custom_metaclass(self):
_testcapi = import_module("_testcapi")

def call(cls):
return cls()

Meta = _testcapi.make_vectorcall_class(type)
C = type.__new__(Meta, "C", (), {})
Meta.set_vectorcall(C, type)

for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
call(list)
self.assert_specialized(call, "CALL_BUILTIN_CLASS")
Meta.__call__ = lambda cls: "custom"
self.assertEqual(call(C), "custom")

@requires_jit_disabled
@requires_specialization
def test_call_immutable_class_custom_metaclass(self):
_testcapi = import_module("_testcapi")
_testlimitedcapi = import_module("_testlimitedcapi")

def call(cls):
return cls()

Meta = _testcapi.make_vectorcall_class(type)
C = type.__new__(Meta, "C", (), {})
Meta.set_vectorcall(C, type)
_testlimitedcapi.type_freeze(C)

for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
self.assertEqual(call(C), "tp_call")
self.assert_specialized(call, "CALL_NON_PY_GENERAL")

def test_too_many_defaults_0(self):
def f():
pass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix an issue where the vectorcall optimization for class calls could bypass a custom metaclass's ``__call__`` method.
2 changes: 1 addition & 1 deletion Modules/_testinternalcapi/test_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -4794,7 +4794,7 @@ dummy_func(

op(_GUARD_CALLABLE_BUILTIN_CLASS, (callable, unused, unused[oparg] -- callable, unused, unused[oparg])) {
PyObject *callable_o = PyStackRef_AsPyObjectBorrow(callable);
EXIT_IF(!PyType_Check(callable_o));
EXIT_IF(!Py_IS_TYPE(callable_o, &PyType_Type));
PyTypeObject *tp = (PyTypeObject *)callable_o;
EXIT_IF(tp->tp_vectorcall == NULL);
}
Expand Down
2 changes: 1 addition & 1 deletion Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,9 @@ specialize_class_call(PyObject *callable, _Py_CODEUNIT *instr, int nargs)
{
assert(PyType_Check(callable));
PyTypeObject *tp = _PyType_CAST(callable);
if (Py_TYPE(tp) != &PyType_Type) {
goto generic;
}
if (tp->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) {
int oparg = instr->op.arg;
if (nargs == 1 && oparg == 1) {
Expand All @@ -1681,9 +1684,6 @@ specialize_class_call(PyObject *callable, _Py_CODEUNIT *instr, int nargs)
}
goto generic;
}
if (Py_TYPE(tp) != &PyType_Type) {
goto generic;
}
if (tp->tp_new == PyBaseObject_Type.tp_new) {
unsigned int tp_version = 0;
PyObject *init = get_init_for_simple_managed_python_class(tp, &tp_version);
Expand Down
Loading