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
75 changes: 74 additions & 1 deletion Lib/test/test_opcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,43 @@ def assert_no_opcode(self, f, opname):
opnames = {instruction.opname for instruction in instructions}
self.assertNotIn(opname, opnames)

def adaptive_counters(self, f):
"""Map each specialized instruction in f to its adaptive counter."""
counters = {}
for instruction in dis.get_instructions(f, adaptive=True):
if instruction.opname == instruction.baseopname:
continue
if instruction.baseopname in ("RESUME", "JUMP_BACKWARD"):
continue
cache = {name: data for name, _, data in instruction.cache_info}
if "counter" in cache:
counters[instruction.offset] = (instruction.opname,
cache["counter"])
return counters

def assert_specialization_stable(self, f, *args, calls=10):
"""Assert that no specialized instruction in f misses its guard.

A guard miss advances the instruction's adaptive counter. f should
have a fresh code object (see reset_code()), otherwise leftover
specializations from earlier runs can show up as misses.
"""
before = self.adaptive_counters(f)
self.assertTrue(before, f"{f.__qualname__} has no specialized "
"instructions")
for _ in range(calls):
f(*args)
after = self.adaptive_counters(f)
# Ignore instructions that only specialize during these calls.
moved = []
for off, (op, counter) in before.items():
if after.get(off) != (op, counter):
now = after[off][1] if off in after else "unspecialized"
moved.append(f"{op} at offset {off}: counter {counter} -> {now}")
self.assertEqual(moved, [],
f"specialized instructions in {f.__qualname__} "
f"missed their guard during {calls} calls")


class TestLoadSuperAttrCache(unittest.TestCase):
def test_descriptor_not_double_executed_on_spec_fail(self):
Expand Down Expand Up @@ -2156,6 +2193,7 @@ def list_append(l):
for _ in r:
l.append(1)

reset_code(list_append)
list_append([])
self.assert_specialized(list_append, "CALL_LIST_APPEND")
self.assert_no_opcode(list_append, "CALL_METHOD_DESCRIPTOR_O")
Expand All @@ -2166,10 +2204,45 @@ def my_list_append(l):
l.append(1)

class MyList(list): pass
my_list_append(MyList())
my_list = MyList()
reset_code(my_list_append)
my_list_append(my_list)
self.assert_specialized(my_list_append, "CALL_METHOD_DESCRIPTOR_O")
self.assert_no_opcode(my_list_append, "CALL_LIST_APPEND")
self.assert_no_opcode(my_list_append, "CALL")
self.assert_specialization_stable(my_list_append, my_list)

@cpython_only
@requires_specialization
@requires_jit_disabled
def test_call_method_descriptor_subclass_instance(self):
# C methods inherited from a built-in type must not miss
# their guard on subclass instances. One case per guard, plus unbound.
class MyStr(str): pass
Point = collections.namedtuple("Point", "x y")
counts = collections.defaultdict(int, {1: 2})

def noargs(s): return s.upper()
def o(p): return p.count(1)
def fast(d): return d.get(1)
def fast_with_keywords(s): return s.split()
def unbound(d): return dict.get(d, 1)

cases = [
(noargs, MyStr("abc"), "CALL_METHOD_DESCRIPTOR_NOARGS"),
(o, Point(1, 2), "CALL_METHOD_DESCRIPTOR_O"),
(fast, counts, "CALL_METHOD_DESCRIPTOR_FAST"),
(fast_with_keywords, MyStr("a b"),
"CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS"),
(unbound, counts, "CALL_METHOD_DESCRIPTOR_FAST"),
]
for f, obj, opname in cases:
with self.subTest(f=f.__name__, obj=type(obj).__name__):
reset_code(f)
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
f(obj)
self.assert_specialized(f, opname)
self.assert_specialization_stable(f, obj)

@cpython_only
@requires_specialization
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fix specialization of calls to C methods inherited from built-in types when
the receiver is an instance of a subclass, such as
:class:`~collections.defaultdict` or a :func:`~collections.namedtuple`. The
specialized ``CALL_METHOD_DESCRIPTOR_*`` instructions previously failed their
guard on every such call and fell back to the generic call path.
8 changes: 4 additions & 4 deletions Modules/_testinternalcapi/test_cases.c.h

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

8 changes: 4 additions & 4 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -5060,7 +5060,7 @@ dummy_func(
EXIT_IF(total_args != 2);
PyObject *self = PyStackRef_AsPyObjectBorrow(
PyStackRef_IsNull(self_or_null) ? args[0] : self_or_null);
EXIT_IF(!Py_IS_TYPE(self, method->d_common.d_type));
EXIT_IF(!PyObject_TypeCheck(self, method->d_common.d_type));
}

op(_CALL_METHOD_DESCRIPTOR_O, (callable, self_or_null, args[oparg] -- res, c, s, a)) {
Expand Down Expand Up @@ -5137,7 +5137,7 @@ dummy_func(
}
EXIT_IF(total_args == 0);
PyObject *self = PyStackRef_AsPyObjectBorrow(arguments[0]);
EXIT_IF(!Py_IS_TYPE(self, method->d_common.d_type));
EXIT_IF(!PyObject_TypeCheck(self, method->d_common.d_type));
}

op(_CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS, (callable, self_or_null, args[oparg] -- callable, self_or_null, args[oparg])) {
Expand Down Expand Up @@ -5211,7 +5211,7 @@ dummy_func(
EXIT_IF(total_args != 1);
PyObject *self = PyStackRef_AsPyObjectBorrow(
PyStackRef_IsNull(self_or_null) ? args[0] : self_or_null);
EXIT_IF(!Py_IS_TYPE(self, method->d_common.d_type));
EXIT_IF(!PyObject_TypeCheck(self, method->d_common.d_type));
}

op(_CALL_METHOD_DESCRIPTOR_NOARGS, (callable, self_or_null, args[oparg] -- res, c, s)) {
Expand Down Expand Up @@ -5281,7 +5281,7 @@ dummy_func(
EXIT_IF(total_args == 0);
PyObject *self = PyStackRef_AsPyObjectBorrow(
PyStackRef_IsNull(self_or_null) ? args[0] : self_or_null);
EXIT_IF(!Py_IS_TYPE(self, method->d_common.d_type));
EXIT_IF(!PyObject_TypeCheck(self, method->d_common.d_type));
}

op(_CALL_METHOD_DESCRIPTOR_FAST, (callable, self_or_null, args[oparg] -- callable, self_or_null, args[oparg])) {
Expand Down
8 changes: 4 additions & 4 deletions Python/executor_cases.c.h

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

8 changes: 4 additions & 4 deletions Python/generated_cases.c.h

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

Loading