GH-122821: Simplify compilation of while statements to ensure consistency of offsets for sys.monitoring (GH-122934)

This commit is contained in:
Mark Shannon 2024-08-13 10:25:44 +01:00 committed by GitHub
parent 0e207f3e7a
commit fe23f8ed97
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 122 additions and 87 deletions

View file

@ -1446,9 +1446,27 @@ class BranchRecorder(JumpRecorder):
class JumpOffsetRecorder:
event_type = E.JUMP
name = "jump"
def __init__(self, events, offsets=False):
self.events = events
def __call__(self, code, from_, to):
self.events.append((self.name, code.co_name, from_, to))
class BranchOffsetRecorder(JumpOffsetRecorder):
event_type = E.BRANCH
name = "branch"
JUMP_AND_BRANCH_RECORDERS = JumpRecorder, BranchRecorder
JUMP_BRANCH_AND_LINE_RECORDERS = JumpRecorder, BranchRecorder, LineRecorder
FLOW_AND_LINE_RECORDERS = JumpRecorder, BranchRecorder, LineRecorder, ExceptionRecorder, ReturnRecorder
BRANCH_OFFSET_RECORDERS = BranchOffsetRecorder,
class TestBranchAndJumpEvents(CheckEvents):
maxDiff = None
@ -1538,6 +1556,24 @@ class TestBranchAndJumpEvents(CheckEvents):
('return', 'func', None),
('line', 'get_events', 11)])
def test_while_offset_consistency(self):
def foo(n=0):
while n<4:
pass
n += 1
return None
in_loop = ('branch', 'foo', 10, 14)
exit_loop = ('branch', 'foo', 10, 30)
self.check_events(foo, recorders = BRANCH_OFFSET_RECORDERS, expected = [
in_loop,
in_loop,
in_loop,
in_loop,
exit_loop])
class TestLoadSuperAttr(CheckEvents):
RECORDERS = CallRecorder, LineRecorder, CRaiseRecorder, CReturnRecorder