mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
gh-109094: remove unnecessary updates of frame->prev_instr in instrumentation functions (#109076)
This commit is contained in:
parent
403ab1306a
commit
96396962ce
2 changed files with 11 additions and 6 deletions
|
@ -317,6 +317,13 @@ generator_example.events = ([(0, 'call'),
|
||||||
[(5, 'line'), (5, 'return')])
|
[(5, 'line'), (5, 'return')])
|
||||||
|
|
||||||
|
|
||||||
|
def lineno_matches_lasti(frame):
|
||||||
|
last_line = None
|
||||||
|
for start, end, line in frame.f_code.co_lines():
|
||||||
|
if start <= frame.f_lasti < end:
|
||||||
|
last_line = line
|
||||||
|
return last_line == frame.f_lineno
|
||||||
|
|
||||||
class Tracer:
|
class Tracer:
|
||||||
def __init__(self, trace_line_events=None, trace_opcode_events=None):
|
def __init__(self, trace_line_events=None, trace_opcode_events=None):
|
||||||
self.trace_line_events = trace_line_events
|
self.trace_line_events = trace_line_events
|
||||||
|
@ -330,6 +337,7 @@ class Tracer:
|
||||||
frame.f_trace_opcodes = self.trace_opcode_events
|
frame.f_trace_opcodes = self.trace_opcode_events
|
||||||
|
|
||||||
def trace(self, frame, event, arg):
|
def trace(self, frame, event, arg):
|
||||||
|
assert lineno_matches_lasti(frame)
|
||||||
self._reconfigure_frame(frame)
|
self._reconfigure_frame(frame)
|
||||||
self.events.append((frame.f_lineno, event))
|
self.events.append((frame.f_lineno, event))
|
||||||
return self.trace
|
return self.trace
|
||||||
|
@ -1890,6 +1898,7 @@ class JumpTracer:
|
||||||
def trace(self, frame, event, arg):
|
def trace(self, frame, event, arg):
|
||||||
if self.done:
|
if self.done:
|
||||||
return
|
return
|
||||||
|
assert lineno_matches_lasti(frame)
|
||||||
# frame.f_code.co_firstlineno is the first line of the decorator when
|
# frame.f_code.co_firstlineno is the first line of the decorator when
|
||||||
# 'function' is decorated and the decorator may be written using
|
# 'function' is decorated and the decorator may be written using
|
||||||
# multiple physical lines when it is too long. Use the first line
|
# multiple physical lines when it is too long. Use the first line
|
||||||
|
|
|
@ -1057,8 +1057,6 @@ _Py_call_instrumentation_jump(
|
||||||
assert(event == PY_MONITORING_EVENT_JUMP ||
|
assert(event == PY_MONITORING_EVENT_JUMP ||
|
||||||
event == PY_MONITORING_EVENT_BRANCH);
|
event == PY_MONITORING_EVENT_BRANCH);
|
||||||
assert(frame->prev_instr == instr);
|
assert(frame->prev_instr == instr);
|
||||||
/* Event should occur after the jump */
|
|
||||||
frame->prev_instr = target;
|
|
||||||
PyCodeObject *code = _PyFrame_GetCode(frame);
|
PyCodeObject *code = _PyFrame_GetCode(frame);
|
||||||
int to = (int)(target - _PyCode_CODE(code));
|
int to = (int)(target - _PyCode_CODE(code));
|
||||||
PyObject *to_obj = PyLong_FromLong(to * (int)sizeof(_Py_CODEUNIT));
|
PyObject *to_obj = PyLong_FromLong(to * (int)sizeof(_Py_CODEUNIT));
|
||||||
|
@ -1071,12 +1069,10 @@ _Py_call_instrumentation_jump(
|
||||||
if (err) {
|
if (err) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (frame->prev_instr != target) {
|
if (frame->prev_instr != instr) {
|
||||||
/* The callback has caused a jump (by setting the line number) */
|
/* The callback has caused a jump (by setting the line number) */
|
||||||
return frame->prev_instr;
|
return frame->prev_instr;
|
||||||
}
|
}
|
||||||
/* Reset prev_instr for INSTRUMENTED_LINE */
|
|
||||||
frame->prev_instr = instr;
|
|
||||||
return target;
|
return target;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1125,7 +1121,7 @@ _Py_Instrumentation_GetLine(PyCodeObject *code, int index)
|
||||||
int
|
int
|
||||||
_Py_call_instrumentation_line(PyThreadState *tstate, _PyInterpreterFrame* frame, _Py_CODEUNIT *instr, _Py_CODEUNIT *prev)
|
_Py_call_instrumentation_line(PyThreadState *tstate, _PyInterpreterFrame* frame, _Py_CODEUNIT *instr, _Py_CODEUNIT *prev)
|
||||||
{
|
{
|
||||||
frame->prev_instr = instr;
|
assert(frame->prev_instr == instr);
|
||||||
PyCodeObject *code = _PyFrame_GetCode(frame);
|
PyCodeObject *code = _PyFrame_GetCode(frame);
|
||||||
assert(is_version_up_to_date(code, tstate->interp));
|
assert(is_version_up_to_date(code, tstate->interp));
|
||||||
assert(instrumentation_cross_checks(tstate->interp, code));
|
assert(instrumentation_cross_checks(tstate->interp, code));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue