Skip to content

Commit b803214

Browse files
committed
add test
1 parent c0ad56c commit b803214

3 files changed

Lines changed: 22 additions & 2 deletions

File tree

Lib/test/test_capi/test_opt.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2774,6 +2774,24 @@ class C:
27742774
self.assertLessEqual(count_ops(ex, "_POP_TOP"), 2)
27752775
self.assertIn("_POP_TOP_NOP", uops)
27762776

2777+
def test_load_attr_module(self):
2778+
import platform
2779+
# Use default argument to capture module as local variable
2780+
def testfunc(n, mod=platform):
2781+
x = 0
2782+
for _ in range(n):
2783+
# Access module attribute multiple times
2784+
x = mod.python_version
2785+
return x
2786+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
2787+
self.assertIsNotNone(ex)
2788+
uops = get_opnames(ex)
2789+
2790+
self.assertIn("_LOAD_ATTR_MODULE", uops)
2791+
# _POP_TOP_MODULE should be optimized to _POP_TOP_NOP for borrowed refs
2792+
self.assertIn("_POP_TOP_NOP", uops)
2793+
self.assertLessEqual(count_ops(ex, "_POP_TOP"), 2)
2794+
27772795
def test_int_add_op_refcount_elimination(self):
27782796
def testfunc(n):
27792797
c = 1

Python/optimizer_bytecodes.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,8 @@ dummy_func(void) {
638638
}
639639

640640
op(_POP_TOP_MODULE, (value --)) {
641-
if (PyJitRef_IsBorrowed(value)) {
641+
if (PyJitRef_IsBorrowed(value) ||
642+
sym_is_immortal(PyJitRef_Unwrap(value))) {
642643
REPLACE_OP(this_instr, _POP_TOP_NOP, 0, 0);
643644
}
644645
}

Python/optimizer_cases.c.h

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)