Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8f51f54

Browse files
committed
Rationalize the events passed to the profiler (no changes for the tracer).
The profiler does not need to know anything about the exception state, so we no longer call it when an exception is raised. We do, however, make sure we *always* call the profiler when we exit a frame. This ensures that timing events are more easily isolated by a profiler and finally clauses that do a lot of work don't have their time mis-allocated. When an exception is propogated out of the frame, the C callback for the profiler now receives a PyTrace_RETURN event with an arg of NULL; the Python-level profile hook function will see a 'return' event with an arg of None. This means that from Python it is impossible for the profiler to determine if the frame exited with an exception or if it returned None, but this doesn't matter for profiling. A C-based profiler could tell the difference, but this doesn't seem important. ceval.c:eval_frame(): Simplify the code in two places so that the profiler is called for every exit from a frame and not for exceptions. sysmodule.c:profile_trampoline(): Make sure we don't expose Python code to NULL; use None instead.
1 parent c4b09b4 commit 8f51f54

2 files changed

Lines changed: 6 additions & 10 deletions

File tree

Python/ceval.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,14 +2198,9 @@ eval_frame(PyFrameObject *f)
21982198
f->f_lasti -= 2;
21992199
PyTraceBack_Here(f);
22002200

2201-
if (tstate->use_tracing) {
2202-
if (tstate->c_tracefunc)
2203-
call_exc_trace(tstate->c_tracefunc,
2204-
tstate->c_traceobj, f);
2205-
if (tstate->c_profilefunc)
2206-
call_exc_trace(tstate->c_profilefunc,
2207-
tstate->c_profileobj,f);
2208-
}
2201+
if (tstate->c_tracefunc != NULL)
2202+
call_exc_trace(tstate->c_tracefunc,
2203+
tstate->c_traceobj, f);
22092204
}
22102205

22112206
/* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
@@ -2301,8 +2296,7 @@ eval_frame(PyFrameObject *f)
23012296
why = WHY_EXCEPTION;
23022297
}
23032298
}
2304-
if (tstate->c_profilefunc
2305-
&& (why == WHY_RETURN || why == WHY_YIELD)) {
2299+
if (tstate->c_profilefunc) {
23062300
if (call_trace(tstate->c_profilefunc,
23072301
tstate->c_profileobj, f,
23082302
PyTrace_RETURN, retval)) {

Python/sysmodule.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ profile_trampoline(PyObject *self, PyFrameObject *frame,
260260
PyThreadState *tstate = frame->f_tstate;
261261
PyObject *result;
262262

263+
if (arg == NULL)
264+
arg = Py_None;
263265
result = call_trampoline(tstate, self, frame, what, arg);
264266
if (result == NULL) {
265267
PyEval_SetProfile(NULL, NULL);

0 commit comments

Comments
 (0)