]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-137200: support frame lineno setter with `BRANCH_LEFT` and `BRANCH_RIGHT` events...
authorXuanteng Huang <44627253+xuantengh@users.noreply.github.com>
Thu, 31 Jul 2025 13:22:22 +0000 (21:22 +0800)
committerGitHub <noreply@github.com>
Thu, 31 Jul 2025 13:22:22 +0000 (14:22 +0100)
Lib/test/test_monitoring.py
Objects/frameobject.c

index a932ac80117d271801120cbe22788292b72ffc6a..4122f786a9afb094f1dd50232abf4e9a22c7d9d1 100644 (file)
@@ -3,6 +3,7 @@
 import collections
 import dis
 import functools
+import inspect
 import math
 import operator
 import sys
@@ -1709,6 +1710,27 @@ class TestBranchAndJumpEvents(CheckEvents):
             ('branch right', 'func', 6, 8),
             ('branch right', 'func', 2, 10)])
 
+    def test_callback_set_frame_lineno(self):
+        def func(s: str) -> int:
+            if s.startswith("t"):
+                return 1
+            else:
+                return 0
+
+        def callback(code, from_, to):
+            # try set frame.f_lineno
+            frame = inspect.currentframe()
+            while frame and frame.f_code is not code:
+                frame = frame.f_back
+
+            self.assertIsNotNone(frame)
+            frame.f_lineno = frame.f_lineno + 1 # run next instruction
+
+        sys.monitoring.set_local_events(TEST_TOOL, func.__code__, E.BRANCH_LEFT)
+        sys.monitoring.register_callback(TEST_TOOL, E.BRANCH_LEFT, callback)
+
+        self.assertEqual(func("true"), 1)
+
 
 class TestBranchConsistency(MonitoringTestBase, unittest.TestCase):
 
index 97de1e06efe1b20dec01e1537174638cf99d1f98..72c0ab0666e927f5e9f5ff824f73b6e84cd60f9b 100644 (file)
@@ -1685,6 +1685,8 @@ frame_lineno_set_impl(PyFrameObject *self, PyObject *value)
         case PY_MONITORING_EVENT_PY_RESUME:
         case PY_MONITORING_EVENT_JUMP:
         case PY_MONITORING_EVENT_BRANCH:
+        case PY_MONITORING_EVENT_BRANCH_LEFT:
+        case PY_MONITORING_EVENT_BRANCH_RIGHT:
         case PY_MONITORING_EVENT_LINE:
         case PY_MONITORING_EVENT_PY_YIELD:
             /* Setting f_lineno is allowed for the above events */