summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorBrett Simmers <swtaarrs@users.noreply.github.com>2024-02-23 03:14:17 (GMT)
committerGitHub <noreply@github.com>2024-02-23 03:14:17 (GMT)
commita494a3dd8e0e74861972698c37b14a4087a4688c (patch)
tree38ee4cfeaf0e75809638f2f69c78af9206590883 /Lib
parentb48101864c724a7eab41a6878a836f38e54e04fb (diff)
downloadcpython-a494a3dd8e0e74861972698c37b14a4087a4688c.zip
cpython-a494a3dd8e0e74861972698c37b14a4087a4688c.tar.gz
cpython-a494a3dd8e0e74861972698c37b14a4087a4688c.tar.bz2
gh-115836: Don't use hardcoded line numbers in test_monitoring (#115837)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_monitoring.py20
1 files changed, 14 insertions, 6 deletions
diff --git a/Lib/test/test_monitoring.py b/Lib/test/test_monitoring.py
index 58aa4bc..2fd8220 100644
--- a/Lib/test/test_monitoring.py
+++ b/Lib/test/test_monitoring.py
@@ -35,6 +35,9 @@ TEST_TOOL = 2
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):
@@ -529,8 +532,8 @@ class LineMonitoringTest(MonitoringTestBase, unittest.TestCase):
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)
@@ -547,8 +550,13 @@ class LineMonitoringTest(MonitoringTestBase, unittest.TestCase):
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)
@@ -569,8 +577,8 @@ class LineMonitoringTest(MonitoringTestBase, unittest.TestCase):
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: