Skip to content

Commit

Permalink
ensure that our assumptions about the compiler's behavior hold
Browse files Browse the repository at this point in the history
  • Loading branch information
carljm committed May 12, 2023
1 parent 8efd7c6 commit 9de351f
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions Lib/test/test_monitoring.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Test suite for the sys.monitoring."""

import collections
import dis
import functools
import operator
import sys
Expand Down Expand Up @@ -507,7 +508,7 @@ def test_lines_single(self):
sys.monitoring.set_events(TEST_TOOL, 0)
sys.monitoring.register_callback(TEST_TOOL, E.LINE, None)
start = LineMonitoringTest.test_lines_single.__code__.co_firstlineno
self.assertEqual(events, [start+7, 15, start+8])
self.assertEqual(events, [start+7, 16, start+8])
finally:
sys.monitoring.set_events(TEST_TOOL, 0)
sys.monitoring.register_callback(TEST_TOOL, E.LINE, None)
Expand All @@ -525,7 +526,7 @@ def test_lines_loop(self):
sys.monitoring.set_events(TEST_TOOL, 0)
sys.monitoring.register_callback(TEST_TOOL, E.LINE, None)
start = LineMonitoringTest.test_lines_loop.__code__.co_firstlineno
self.assertEqual(events, [start+7, 22, 23, 22, 23, 22, start+8])
self.assertEqual(events, [start+7, 23, 24, 23, 24, 23, start+8])
finally:
sys.monitoring.set_events(TEST_TOOL, 0)
sys.monitoring.register_callback(TEST_TOOL, E.LINE, None)
Expand All @@ -547,7 +548,7 @@ def test_lines_two(self):
sys.monitoring.register_callback(TEST_TOOL, E.LINE, None)
sys.monitoring.register_callback(TEST_TOOL2, E.LINE, None)
start = LineMonitoringTest.test_lines_two.__code__.co_firstlineno
expected = [start+10, 15, start+11]
expected = [start+10, 16, start+11]
self.assertEqual(events, expected)
self.assertEqual(events2, expected)
finally:
Expand Down Expand Up @@ -1190,10 +1191,22 @@ def _exec(self, codestr, optimized=False):
# un-optimized `LOAD_GLOBAL super; CALL; LOAD_ATTR` form.
assignment = "x = 1" if optimized else "super = super"
codestr = f"{assignment}\n{textwrap.dedent(codestr)}"
co = compile(codestr, "<string>", "exec")
# validate that we really do have a LOAD_SUPER_ATTR, only when optimized
self.assertEqual(self._has_load_super_attr(co), optimized)
d = {}
exec(codestr, d, d)
exec(co, d, d)
return d

def _has_load_super_attr(self, co):
has = any(instr.opname == "LOAD_SUPER_ATTR" for instr in dis.get_instructions(co))
if not has:
has = any(
isinstance(c, types.CodeType) and self._has_load_super_attr(c)
for c in co.co_consts
)
return has

def _super_method_call(self, optimized=False):
codestr = """
class A:
Expand Down

0 comments on commit 9de351f

Please sign in to comment.