Skip to content

Commit 0959666

Browse files
committed
Change BRANCH_NOT_TAKEN event to appear to originate at same offset as BRANCH_TAKEN event.
1 parent 1b65c46 commit 0959666

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

Lib/test/test_monitoring.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,7 +1519,7 @@ def func():
15191519
('line', 'func', 5),
15201520
('line', 'meth', 1),
15211521
('jump', 'func', 5, '[offset=120]'),
1522-
('branch', 'func', '[offset=128]', '[offset=130]'),
1522+
('branch', 'func', '[offset=124]', '[offset=130]'),
15231523
('line', 'get_events', 11)])
15241524

15251525
self.check_events(func, recorders = FLOW_AND_LINE_RECORDERS, expected = [
@@ -1534,7 +1534,7 @@ def func():
15341534
('line', 'meth', 1),
15351535
('return', 'meth', None),
15361536
('jump', 'func', 5, '[offset=120]'),
1537-
('branch', 'func', '[offset=128]', '[offset=130]'),
1537+
('branch', 'func', '[offset=124]', '[offset=130]'),
15381538
('return', 'func', None),
15391539
('line', 'get_events', 11)])
15401540

Python/instrumentation.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,10 @@ call_instrumentation_vector(
10821082
/* Offset visible to user should be the offset in bytes, as that is the
10831083
* convention for APIs involving code offsets. */
10841084
int bytes_offset = offset * (int)sizeof(_Py_CODEUNIT);
1085+
if (event == PY_MONITORING_EVENT_BRANCH_NOT_TAKEN) {
1086+
assert(EVENT_FOR_OPCODE[_Py_GetBaseOpcode(code, offset-2)] == PY_MONITORING_EVENT_BRANCH_TAKEN);
1087+
bytes_offset -= 4;
1088+
}
10851089
PyObject *offset_obj = PyLong_FromLong(bytes_offset);
10861090
if (offset_obj == NULL) {
10871091
return -1;
@@ -2804,10 +2808,7 @@ branch_handler(
28042808
if (self->taken) {
28052809
offset += 2;
28062810
}
2807-
else {
2808-
offset -= 2;
2809-
}
2810-
if (offset < 0 || offset >= Py_SIZE(code)) {
2811+
if (offset >= Py_SIZE(code)) {
28112812
return res;
28122813
}
28132814
int other_event = self->taken ?

0 commit comments

Comments
 (0)