bpo-17799: Explain real behaviour of sys.settrace and sys.setprofile by pablogsal · Pull Request #4056 · python/cpython (original) (raw)
The docs for sys.settrace
mention that:
event is a string: 'call', 'line', 'return', 'exception', 'c_call', 'c_return', or 'c_exception'
But in the code for ceval.c
the only point where call_trace
is invoked with PyTrace_C_CALL
or PyTrace_C_RETURN
is under the C_TRACE
macro. In this macro this line prevents any function set up using sys.settrace
to call call_trace
with the mentioned arguments:
if (tstate->use_tracing && tstate->c_profilefunc)
Notice that from the code of PyEval_SetTrace
and PyEval_SetProfile
, only the later sets tstate->c_profilefunc
and therefore only functions installed using sys.setprofile
will recieve a c_call
for the event:
void PyEval_SetProfile(Py_tracefunc func, PyObject *arg) { ... tstate->c_profilefunc = func; tstate->c_profileobj = arg; tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL); }
void PyEval_SetTrace(Py_tracefunc func, PyObject *arg) { ... tstate->c_tracefunc = func; tstate->c_traceobj = arg; tstate->use_tracing = ((func != NULL) || (tstate->c_profilefunc != NULL)); }
This PR updates the documentation clarifying the real behaviour of both functions.