Skip to content

Commit a08d714

Browse files
committed
gh-162600: Don't specialize calls of abstract classes to CALL_ALLOC_AND_ENTER_INIT
1 parent 9d6fa5d commit a08d714

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

‎Lib/test/test_opcache.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,42 @@ def instantiate():
562562
with self.assertRaises(TypeError):
563563
instantiate()
564564

565+
@requires_jit_disabled
566+
@requires_specialization
567+
def test_abstract_class_is_not_instantiated(self):
568+
class Abstract:
569+
def __init__(self):
570+
pass
571+
Abstract.__abstractmethods__ = frozenset({"f"})
572+
573+
@reset_code
574+
def instantiate():
575+
return Abstract()
576+
577+
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
578+
with self.assertRaises(TypeError):
579+
instantiate()
580+
self.assert_no_opcode(instantiate, "CALL_ALLOC_AND_ENTER_INIT")
581+
582+
# A class made abstract after its call was specialized.
583+
class Later:
584+
def __init__(self):
585+
pass
586+
587+
@reset_code
588+
def instantiate_later():
589+
return Later()
590+
591+
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
592+
instantiate_later()
593+
self.assert_specialized(instantiate_later, "CALL_ALLOC_AND_ENTER_INIT")
594+
Later.__abstractmethods__ = frozenset({"f"})
595+
# Run past the cooldown, so that the call is specialized again.
596+
for _ in range(_testinternalcapi.SPECIALIZATION_COOLDOWN):
597+
with self.assertRaises(TypeError):
598+
instantiate_later()
599+
self.assert_no_opcode(instantiate_later, "CALL_ALLOC_AND_ENTER_INIT")
600+
565601
def test_recursion_check_for_general_calls(self):
566602
def test(default=None):
567603
return test()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a class with ``__abstractmethods__`` assigned directly, without
2+
:class:`abc.ABCMeta`, being instantiated instead of raising :exc:`TypeError`
3+
when the same call is executed more than once.

‎Python/specialize.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,8 +1692,13 @@ specialize_class_call(PyObject *callable, _Py_CODEUNIT *instr, int nargs)
16921692
Py_XDECREF(init);
16931693
return -1;
16941694
}
1695-
if (init != NULL && _PyType_CacheInitForSpecialization(
1696-
(PyHeapTypeObject *)tp, init, tp_version)) {
1695+
/* CALL_ALLOC_AND_ENTER_INIT skips object_new()'s abstract check.
1696+
Check after the lookup so a concurrent change invalidates
1697+
tp_version. */
1698+
if (init != NULL && !(tp->tp_flags & Py_TPFLAGS_IS_ABSTRACT) &&
1699+
_PyType_CacheInitForSpecialization(
1700+
(PyHeapTypeObject *)tp, init, tp_version))
1701+
{
16971702
_PyCallCache *cache = (_PyCallCache *)(instr + 1);
16981703
write_u32(cache->func_version, tp_version);
16991704
specialize(instr, CALL_ALLOC_AND_ENTER_INIT);

0 commit comments

Comments
 (0)