]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-131798: Add `_CHECK_IS_NOT_PY_CALLABLE` to the JIT optimizer (GH-148434)
authorWulian233 <1055917385@qq.com>
Sun, 12 Apr 2026 13:07:19 +0000 (21:07 +0800)
committerGitHub <noreply@github.com>
Sun, 12 Apr 2026 13:07:19 +0000 (21:07 +0800)
Lib/test/test_capi/test_opt.py
Python/optimizer_bytecodes.c
Python/optimizer_cases.c.h

index f11413cc6254225987eebb611d876977cb6b6d77..2678a620763b4b39ab829709f722d343b51058ee 100644 (file)
@@ -2755,6 +2755,21 @@ class TestUopsOptimization(unittest.TestCase):
         self.assertNotIn("_GUARD_TOS_INT", uops)
         self.assertIn("_POP_TOP_NOP", uops)
 
+    def test_check_is_not_py_callable(self):
+        def testfunc(n):
+            total = 0
+            f = len
+            xs = (1, 2, 3)
+            for _ in range(n):
+                total += f(xs)
+            return total
+
+        res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
+        self.assertEqual(res, 3 * TIER2_THRESHOLD)
+        self.assertIsNotNone(ex)
+        uops = get_opnames(ex)
+        self.assertNotIn("_CHECK_IS_NOT_PY_CALLABLE", uops)
+
     def test_call_len_string(self):
         def testfunc(n):
             for _ in range(n):
index 39dc4877af8884e84934a7c9b8e085aa63ee9232..6e5af4793419f23ffa31e6622b731bb1739a39e7 100644 (file)
@@ -1217,6 +1217,13 @@ dummy_func(void) {
         (void)framesize;
     }
 
+    op(_CHECK_IS_NOT_PY_CALLABLE, (callable, unused, unused[oparg] -- callable, unused, unused[oparg])) {
+        PyTypeObject *type = sym_get_type(callable);
+        if (type && type != &PyFunction_Type && type != &PyMethod_Type) {
+            ADD_OP(_NOP, 0, 0);
+        }
+    }
+
     op(_PUSH_FRAME, (new_frame -- )) {
         SYNC_SP();
         if (!CURRENT_FRAME_IS_INIT_SHIM()) {
index 746653906874b5caac398eabae15f7cfb3e26479..d90ad285a11f3362cf40769410383d1b680aab53 100644 (file)
         }
 
         case _CHECK_IS_NOT_PY_CALLABLE: {
+            JitOptRef callable;
+            callable = stack_pointer[-2 - oparg];
+            PyTypeObject *type = sym_get_type(callable);
+            if (type && type != &PyFunction_Type && type != &PyMethod_Type) {
+                ADD_OP(_NOP, 0, 0);
+            }
             break;
         }