Skip to content

Commit 9de351f

Browse files
committed
ensure that our assumptions about the compiler's behavior hold
1 parent 8efd7c6 commit 9de351f

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

Lib/test/test_monitoring.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Test suite for the sys.monitoring."""
22

33
import collections
4+
import dis
45
import functools
56
import operator
67
import sys
@@ -507,7 +508,7 @@ def test_lines_single(self):
507508
sys.monitoring.set_events(TEST_TOOL, 0)
508509
sys.monitoring.register_callback(TEST_TOOL, E.LINE, None)
509510
start = LineMonitoringTest.test_lines_single.__code__.co_firstlineno
510-
self.assertEqual(events, [start+7, 15, start+8])
511+
self.assertEqual(events, [start+7, 16, start+8])
511512
finally:
512513
sys.monitoring.set_events(TEST_TOOL, 0)
513514
sys.monitoring.register_callback(TEST_TOOL, E.LINE, None)
@@ -525,7 +526,7 @@ def test_lines_loop(self):
525526
sys.monitoring.set_events(TEST_TOOL, 0)
526527
sys.monitoring.register_callback(TEST_TOOL, E.LINE, None)
527528
start = LineMonitoringTest.test_lines_loop.__code__.co_firstlineno
528-
self.assertEqual(events, [start+7, 22, 23, 22, 23, 22, start+8])
529+
self.assertEqual(events, [start+7, 23, 24, 23, 24, 23, start+8])
529530
finally:
530531
sys.monitoring.set_events(TEST_TOOL, 0)
531532
sys.monitoring.register_callback(TEST_TOOL, E.LINE, None)
@@ -547,7 +548,7 @@ def test_lines_two(self):
547548
sys.monitoring.register_callback(TEST_TOOL, E.LINE, None)
548549
sys.monitoring.register_callback(TEST_TOOL2, E.LINE, None)
549550
start = LineMonitoringTest.test_lines_two.__code__.co_firstlineno
550-
expected = [start+10, 15, start+11]
551+
expected = [start+10, 16, start+11]
551552
self.assertEqual(events, expected)
552553
self.assertEqual(events2, expected)
553554
finally:
@@ -1190,10 +1191,22 @@ def _exec(self, codestr, optimized=False):
11901191
# un-optimized `LOAD_GLOBAL super; CALL; LOAD_ATTR` form.
11911192
assignment = "x = 1" if optimized else "super = super"
11921193
codestr = f"{assignment}\n{textwrap.dedent(codestr)}"
1194+
co = compile(codestr, "<string>", "exec")
1195+
# validate that we really do have a LOAD_SUPER_ATTR, only when optimized
1196+
self.assertEqual(self._has_load_super_attr(co), optimized)
11931197
d = {}
1194-
exec(codestr, d, d)
1198+
exec(co, d, d)
11951199
return d
11961200

1201+
def _has_load_super_attr(self, co):
1202+
has = any(instr.opname == "LOAD_SUPER_ATTR" for instr in dis.get_instructions(co))
1203+
if not has:
1204+
has = any(
1205+
isinstance(c, types.CodeType) and self._has_load_super_attr(c)
1206+
for c in co.co_consts
1207+
)
1208+
return has
1209+
11971210
def _super_method_call(self, optimized=False):
11981211
codestr = """
11991212
class A:

0 commit comments

Comments
 (0)