Skip to content

Commit 019238a

Browse files
[3.14] gh-137200: support frame lineno setter with BRANCH_LEFT and BRANCH_RIGHT events (GH-137229) (#137280)
Co-authored-by: Xuanteng Huang <44627253+xuantengh@users.noreply.github.com>
1 parent 1bf7462 commit 019238a

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

Lib/test/test_monitoring.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import collections
44
import dis
55
import functools
6+
import inspect
67
import math
78
import operator
89
import sys
@@ -1709,6 +1710,27 @@ def func(v=1):
17091710
('branch right', 'func', 6, 8),
17101711
('branch right', 'func', 2, 10)])
17111712

1713+
def test_callback_set_frame_lineno(self):
1714+
def func(s: str) -> int:
1715+
if s.startswith("t"):
1716+
return 1
1717+
else:
1718+
return 0
1719+
1720+
def callback(code, from_, to):
1721+
# try set frame.f_lineno
1722+
frame = inspect.currentframe()
1723+
while frame and frame.f_code is not code:
1724+
frame = frame.f_back
1725+
1726+
self.assertIsNotNone(frame)
1727+
frame.f_lineno = frame.f_lineno + 1 # run next instruction
1728+
1729+
sys.monitoring.set_local_events(TEST_TOOL, func.__code__, E.BRANCH_LEFT)
1730+
sys.monitoring.register_callback(TEST_TOOL, E.BRANCH_LEFT, callback)
1731+
1732+
self.assertEqual(func("true"), 1)
1733+
17121734

17131735
class TestBranchConsistency(MonitoringTestBase, unittest.TestCase):
17141736

Objects/frameobject.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,6 +1671,8 @@ frame_lineno_set_impl(PyFrameObject *self, PyObject *value)
16711671
case PY_MONITORING_EVENT_PY_RESUME:
16721672
case PY_MONITORING_EVENT_JUMP:
16731673
case PY_MONITORING_EVENT_BRANCH:
1674+
case PY_MONITORING_EVENT_BRANCH_LEFT:
1675+
case PY_MONITORING_EVENT_BRANCH_RIGHT:
16741676
case PY_MONITORING_EVENT_LINE:
16751677
case PY_MONITORING_EVENT_PY_YIELD:
16761678
/* Setting f_lineno is allowed for the above events */

0 commit comments

Comments
 (0)