Skip to content

Commit d18f73a

Browse files
authored
gh-137200: support frame lineno setter with BRANCH_LEFT and BRANCH_RIGHT events (GH-137229)
1 parent 438cbd8 commit d18f73a

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
@@ -1685,6 +1685,8 @@ frame_lineno_set_impl(PyFrameObject *self, PyObject *value)
16851685
case PY_MONITORING_EVENT_PY_RESUME:
16861686
case PY_MONITORING_EVENT_JUMP:
16871687
case PY_MONITORING_EVENT_BRANCH:
1688+
case PY_MONITORING_EVENT_BRANCH_LEFT:
1689+
case PY_MONITORING_EVENT_BRANCH_RIGHT:
16881690
case PY_MONITORING_EVENT_LINE:
16891691
case PY_MONITORING_EVENT_PY_YIELD:
16901692
/* Setting f_lineno is allowed for the above events */

0 commit comments

Comments
 (0)