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

brandtbucher webhook-mailer at python.org
Fri May 12 19:03:58 EDT 2023


https://github.com/python/cpython/commit/fbb6def08a7ce7d21653e15ccbc4017b4eb2e795
commit: fbb6def08a7ce7d21653e15ccbc4017b4eb2e795
branch: 3.11
author: Brandt Bucher <brandtbucher at microsoft.com>
committer: brandtbucher <brandtbucher at gmail.com>
date: 2023-05-12T23:03:47Z
summary:

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

files:
A Misc/NEWS.d/next/Core and Builtins/2023-05-12-00-19-02.gh-issue-104405.tXV5fn.rst
M Lib/test/test_capi/test_misc.py
M Python/ceval.c
M Python/specialize.c

diff --git a/Lib/test/test_capi/test_misc.py b/Lib/test/test_capi/test_misc.py
index 67150a8d24cb..3e36fbde8c66 100644
--- a/Lib/test/test_capi/test_misc.py
+++ b/Lib/test/test_capi/test_misc.py
@@ -1378,28 +1378,39 @@ def dummy():
 
 class Test_Pep523API(unittest.TestCase):
 
-    def do_test(self, func):
-        calls = []
+    def do_test(self, func, names):
+        actual_calls = []
         start = SUFFICIENT_TO_DEOPT_AND_SPECIALIZE
         count = start + SUFFICIENT_TO_DEOPT_AND_SPECIALIZE
-        for i in range(count):
-            if i == start:
-                _testinternalcapi.set_eval_frame_record(calls)
-            func()
-        _testinternalcapi.set_eval_frame_default()
-        self.assertEqual(len(calls), SUFFICIENT_TO_DEOPT_AND_SPECIALIZE)
-        for name in calls:
-            self.assertEqual(name, func.__name__)
-
-    def test_pep523_with_specialization_simple(self):
-        def func1():
-            pass
-        self.do_test(func1)
+        try:
+            for i in range(count):
+                if i == start:
+                    _testinternalcapi.set_eval_frame_record(actual_calls)
+                func()
+        finally:
+            _testinternalcapi.set_eval_frame_default()
+        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_inlined_binary_subscr(self):
+        class C:
+            def __getitem__(self, other):
+                return None
+        def func():
+            C()[42]
+        names = ["func", "__getitem__"]
+        self.do_test(func, names)
 
-    def test_pep523_with_specialization_with_default(self):
-        def func2(x=None):
+    def test_inlined_call(self):
+        def inner(x=42):
             pass
-        self.do_test(func2)
+        def func():
+            inner()
+            inner(42)
+        names = ["func", "inner", "inner"]
+        self.do_test(func, names)
 
 
 if __name__ == "__main__":
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-05-12-00-19-02.gh-issue-104405.tXV5fn.rst b/Misc/NEWS.d/next/Core and Builtins/2023-05-12-00-19-02.gh-issue-104405.tXV5fn.rst
new file mode 100644
index 000000000000..06ec5d7b0f0c
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2023-05-12-00-19-02.gh-issue-104405.tXV5fn.rst	
@@ -0,0 +1,2 @@
+Fix an issue where some :term:`bytecode` instructions could ignore
+:pep:`523` when "inlining" calls.
diff --git a/Python/ceval.c b/Python/ceval.c
index 72f9c8375d07..47df35319709 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -2233,6 +2233,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
         }
 
         TARGET(BINARY_SUBSCR_GETITEM) {
+            DEOPT_IF(tstate->interp->eval_frame, BINARY_SUBSCR);
             PyObject *sub = TOP();
             PyObject *container = SECOND();
             _PyBinarySubscrCache *cache = (_PyBinarySubscrCache *)next_instr;
diff --git a/Python/specialize.c b/Python/specialize.c
index 08ce2f5caa6b..9d182fd31b08 100644
--- a/Python/specialize.c
+++ b/Python/specialize.c
@@ -1238,6 +1238,10 @@ _Py_Specialize_BinarySubscr(
             SPECIALIZATION_FAIL(BINARY_SUBSCR, SPEC_FAIL_OUT_OF_VERSIONS);
             goto fail;
         }
+        if (_PyInterpreterState_GET()->eval_frame) {
+            SPECIALIZATION_FAIL(BINARY_SUBSCR, SPEC_FAIL_OTHER);
+            goto fail;
+        }
         cache->func_version = version;
         ((PyHeapTypeObject *)container_type)->_spec_cache.getitem = descriptor;
         _Py_SET_OPCODE(*instr, BINARY_SUBSCR_GETITEM);



More information about the Python-checkins mailing list