https://github.com/python/cpython/commit/019238ab18348710f7c6a7f835ee1d371e91fcc0
commit: 019238ab18348710f7c6a7f835ee1d371e91fcc0
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: hugovk <[email protected]>
date: 2025-08-11T17:07:19+03:00
summary:

[3.14] gh-137200: support frame lineno setter with `BRANCH_LEFT` and 
`BRANCH_RIGHT` events (GH-137229) (#137280)

Co-authored-by: Xuanteng Huang <[email protected]>

files:
M Lib/test/test_monitoring.py
M Objects/frameobject.c

diff --git a/Lib/test/test_monitoring.py b/Lib/test/test_monitoring.py
index 263e4e6f394155..28895d16c5707c 100644
--- a/Lib/test/test_monitoring.py
+++ b/Lib/test/test_monitoring.py
@@ -3,6 +3,7 @@
 import collections
 import dis
 import functools
+import inspect
 import math
 import operator
 import sys
@@ -1709,6 +1710,27 @@ def func(v=1):
             ('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):
 
diff --git a/Objects/frameobject.c b/Objects/frameobject.c
index 76b52efccf804f..944e98e062d19c 100644
--- a/Objects/frameobject.c
+++ b/Objects/frameobject.c
@@ -1671,6 +1671,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 */

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to