Skip to content

Commit 03d2f03

Browse files
authored
gh-131798: Add _CHECK_IS_NOT_PY_CALLABLE to the JIT optimizer (GH-148434)
1 parent 30c698a commit 03d2f03

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2755,6 +2755,21 @@ def testfunc(n):
27552755
self.assertNotIn("_GUARD_TOS_INT", uops)
27562756
self.assertIn("_POP_TOP_NOP", uops)
27572757

2758+
def test_check_is_not_py_callable(self):
2759+
def testfunc(n):
2760+
total = 0
2761+
f = len
2762+
xs = (1, 2, 3)
2763+
for _ in range(n):
2764+
total += f(xs)
2765+
return total
2766+
2767+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
2768+
self.assertEqual(res, 3 * TIER2_THRESHOLD)
2769+
self.assertIsNotNone(ex)
2770+
uops = get_opnames(ex)
2771+
self.assertNotIn("_CHECK_IS_NOT_PY_CALLABLE", uops)
2772+
27582773
def test_call_len_string(self):
27592774
def testfunc(n):
27602775
for _ in range(n):

Python/optimizer_bytecodes.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,13 @@ dummy_func(void) {
12171217
(void)framesize;
12181218
}
12191219

1220+
op(_CHECK_IS_NOT_PY_CALLABLE, (callable, unused, unused[oparg] -- callable, unused, unused[oparg])) {
1221+
PyTypeObject *type = sym_get_type(callable);
1222+
if (type && type != &PyFunction_Type && type != &PyMethod_Type) {
1223+
ADD_OP(_NOP, 0, 0);
1224+
}
1225+
}
1226+
12201227
op(_PUSH_FRAME, (new_frame -- )) {
12211228
SYNC_SP();
12221229
if (!CURRENT_FRAME_IS_INIT_SHIM()) {

Python/optimizer_cases.c.h

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)