Skip to content

Commit

Permalink
gh-115836: Don't use hardcoded line numbers in test_monitoring (#115837)
Browse files Browse the repository at this point in the history
  • Loading branch information
swtaarrs authored Feb 23, 2024
1 parent b481018 commit a494a3d
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions Lib/test/test_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def g1():
TEST_TOOL2 = 3
TEST_TOOL3 = 4

def nth_line(func, offset):
return func.__code__.co_firstlineno + offset

class MonitoringBasicTest(unittest.TestCase):

def test_has_objects(self):
Expand Down Expand Up @@ -529,8 +532,8 @@ def test_lines_single(self):
f1()
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, 16, start+8])
start = nth_line(LineMonitoringTest.test_lines_single, 0)
self.assertEqual(events, [start+7, nth_line(f1, 1), start+8])
finally:
sys.monitoring.set_events(TEST_TOOL, 0)
sys.monitoring.register_callback(TEST_TOOL, E.LINE, None)
Expand All @@ -547,8 +550,13 @@ def test_lines_loop(self):
floop()
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, 23, 24, 23, 24, 23, start+8])
start = nth_line(LineMonitoringTest.test_lines_loop, 0)
floop_1 = nth_line(floop, 1)
floop_2 = nth_line(floop, 2)
self.assertEqual(
events,
[start+7, floop_1, floop_2, floop_1, floop_2, floop_1, start+8]
)
finally:
sys.monitoring.set_events(TEST_TOOL, 0)
sys.monitoring.register_callback(TEST_TOOL, E.LINE, None)
Expand All @@ -569,8 +577,8 @@ def test_lines_two(self):
sys.monitoring.set_events(TEST_TOOL, 0); sys.monitoring.set_events(TEST_TOOL2, 0)
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, 16, start+11]
start = nth_line(LineMonitoringTest.test_lines_two, 0)
expected = [start+10, nth_line(f1, 1), start+11]
self.assertEqual(events, expected)
self.assertEqual(events2, expected)
finally:
Expand Down

0 comments on commit a494a3d

Please sign in to comment.