[3.11] GH-104405: Add missing PEP 523 checks (GH-104441)

This commit is contained in:
Brandt Bucher 2023-05-12 16:03:47 -07:00 committed by GitHub
parent 4ade7c3369
commit fbb6def08a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 17 deletions

View file

@ -1378,28 +1378,39 @@ def dummy():
class Test_Pep523API(unittest.TestCase): class Test_Pep523API(unittest.TestCase):
def do_test(self, func): def do_test(self, func, names):
calls = [] actual_calls = []
start = SUFFICIENT_TO_DEOPT_AND_SPECIALIZE start = SUFFICIENT_TO_DEOPT_AND_SPECIALIZE
count = start + SUFFICIENT_TO_DEOPT_AND_SPECIALIZE count = start + SUFFICIENT_TO_DEOPT_AND_SPECIALIZE
for i in range(count): try:
if i == start: for i in range(count):
_testinternalcapi.set_eval_frame_record(calls) if i == start:
func() _testinternalcapi.set_eval_frame_record(actual_calls)
_testinternalcapi.set_eval_frame_default() func()
self.assertEqual(len(calls), SUFFICIENT_TO_DEOPT_AND_SPECIALIZE) finally:
for name in calls: _testinternalcapi.set_eval_frame_default()
self.assertEqual(name, func.__name__) expected_calls = names * SUFFICIENT_TO_DEOPT_AND_SPECIALIZE
self.assertEqual(len(expected_calls), len(actual_calls))
for expected, actual in zip(expected_calls, actual_calls, strict=True):
self.assertEqual(expected, actual)
def test_pep523_with_specialization_simple(self): def test_inlined_binary_subscr(self):
def func1(): class C:
pass def __getitem__(self, other):
self.do_test(func1) return None
def func():
C()[42]
names = ["func", "__getitem__"]
self.do_test(func, names)
def test_pep523_with_specialization_with_default(self): def test_inlined_call(self):
def func2(x=None): def inner(x=42):
pass pass
self.do_test(func2) def func():
inner()
inner(42)
names = ["func", "inner", "inner"]
self.do_test(func, names)
if __name__ == "__main__": if __name__ == "__main__":

View file

@ -0,0 +1,2 @@
Fix an issue where some :term:`bytecode` instructions could ignore
:pep:`523` when "inlining" calls.

View file

@ -2233,6 +2233,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
} }
TARGET(BINARY_SUBSCR_GETITEM) { TARGET(BINARY_SUBSCR_GETITEM) {
DEOPT_IF(tstate->interp->eval_frame, BINARY_SUBSCR);
PyObject *sub = TOP(); PyObject *sub = TOP();
PyObject *container = SECOND(); PyObject *container = SECOND();
_PyBinarySubscrCache *cache = (_PyBinarySubscrCache *)next_instr; _PyBinarySubscrCache *cache = (_PyBinarySubscrCache *)next_instr;

View file

@ -1238,6 +1238,10 @@ _Py_Specialize_BinarySubscr(
SPECIALIZATION_FAIL(BINARY_SUBSCR, SPEC_FAIL_OUT_OF_VERSIONS); SPECIALIZATION_FAIL(BINARY_SUBSCR, SPEC_FAIL_OUT_OF_VERSIONS);
goto fail; goto fail;
} }
if (_PyInterpreterState_GET()->eval_frame) {
SPECIALIZATION_FAIL(BINARY_SUBSCR, SPEC_FAIL_OTHER);
goto fail;
}
cache->func_version = version; cache->func_version = version;
((PyHeapTypeObject *)container_type)->_spec_cache.getitem = descriptor; ((PyHeapTypeObject *)container_type)->_spec_cache.getitem = descriptor;
_Py_SET_OPCODE(*instr, BINARY_SUBSCR_GETITEM); _Py_SET_OPCODE(*instr, BINARY_SUBSCR_GETITEM);