Skip to content

Commit fd844ef

Browse files
authored
bpo-17799: Explain real behaviour of sys.settrace and sys.setprofile (GH-4056) (#5298)
(cherry picked from commit 131fd7f)
1 parent d69794f commit fd844ef

File tree

3 files changed

+41
-20
lines changed

3 files changed

+41
-20
lines changed

Doc/c-api/init.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,15 +1137,19 @@ Python-level trace functions in previous versions.
11371137
function as its first parameter, and may be any Python object, or *NULL*. If
11381138
the profile function needs to maintain state, using a different value for *obj*
11391139
for each thread provides a convenient and thread-safe place to store it. The
1140-
profile function is called for all monitored events except the line-number
1141-
events.
1140+
profile function is called for all monitored events except :const:`PyTrace_LINE`
1141+
and :const:`PyTrace_EXCEPTION`.
11421142
11431143
11441144
.. c:function:: void PyEval_SetTrace(Py_tracefunc func, PyObject *obj)
11451145
11461146
Set the tracing function to *func*. This is similar to
11471147
:c:func:`PyEval_SetProfile`, except the tracing function does receive line-number
1148-
events.
1148+
events and does not receive any event related to C function objects being called. Any
1149+
trace function registered using :c:func:`PyEval_SetTrace` will not receive
1150+
:const:`PyTrace_C_CALL`, :const:`PyTrace_C_EXCEPTION` or :const:`PyTrace_C_RETURN`
1151+
as a value for the *what* parameter.
1152+
11491153
11501154
.. c:function:: PyObject* PyEval_GetCallStats(PyObject *self)
11511155

Doc/library/sys.rst

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,13 +1005,38 @@ always available.
10051005
Set the system's profile function, which allows you to implement a Python source
10061006
code profiler in Python. See chapter :ref:`profile` for more information on the
10071007
Python profiler. The system's profile function is called similarly to the
1008-
system's trace function (see :func:`settrace`), but it isn't called for each
1009-
executed line of code (only on call and return, but the return event is reported
1010-
even when an exception has been set). The function is thread-specific, but
1011-
there is no way for the profiler to know about context switches between threads,
1012-
so it does not make sense to use this in the presence of multiple threads. Also,
1008+
system's trace function (see :func:`settrace`), but it is called with different events,
1009+
for example it isn't called for each executed line of code (only on call and return,
1010+
but the return event is reported even when an exception has been set). The function is
1011+
thread-specific, but there is no way for the profiler to know about context switches between
1012+
threads, so it does not make sense to use this in the presence of multiple threads. Also,
10131013
its return value is not used, so it can simply return ``None``.
10141014

1015+
Profile functions should have three arguments: *frame*, *event*, and
1016+
*arg*. *frame* is the current stack frame. *event* is a string: ``'call'``,
1017+
``'return'``, ``'c_call'``, ``'c_return'``, or ``'c_exception'``. *arg* depends
1018+
on the event type.
1019+
1020+
The events have the following meaning:
1021+
1022+
``'call'``
1023+
A function is called (or some other code block entered). The
1024+
profile function is called; *arg* is ``None``.
1025+
1026+
``'return'``
1027+
A function (or other code block) is about to return. The profile
1028+
function is called; *arg* is the value that will be returned, or ``None``
1029+
if the event is caused by an exception being raised.
1030+
1031+
``'c_call'``
1032+
A C function is about to be called. This may be an extension function or
1033+
a built-in. *arg* is the C function object.
1034+
1035+
``'c_return'``
1036+
A C function has returned. *arg* is the C function object.
1037+
1038+
``'c_exception'``
1039+
A C function has raised an exception. *arg* is the C function object.
10151040

10161041
.. function:: setrecursionlimit(limit)
10171042

@@ -1058,8 +1083,8 @@ always available.
10581083

10591084
Trace functions should have three arguments: *frame*, *event*, and
10601085
*arg*. *frame* is the current stack frame. *event* is a string: ``'call'``,
1061-
``'line'``, ``'return'``, ``'exception'``, ``'c_call'``, ``'c_return'``, or
1062-
``'c_exception'``. *arg* depends on the event type.
1086+
``'line'``, ``'return'`` or ``'exception'``. *arg* depends on
1087+
the event type.
10631088

10641089
The trace function is invoked (with *event* set to ``'call'``) whenever a new
10651090
local scope is entered; it should return a reference to a local trace
@@ -1094,16 +1119,6 @@ always available.
10941119
tuple ``(exception, value, traceback)``; the return value specifies the
10951120
new local trace function.
10961121

1097-
``'c_call'``
1098-
A C function is about to be called. This may be an extension function or
1099-
a built-in. *arg* is the C function object.
1100-
1101-
``'c_return'``
1102-
A C function has returned. *arg* is the C function object.
1103-
1104-
``'c_exception'``
1105-
A C function has raised an exception. *arg* is the C function object.
1106-
11071122
Note that as an exception is propagated down the chain of callers, an
11081123
``'exception'`` event is generated at each level.
11091124

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Explain real behaviour of sys.settrace and sys.setprofile and their C-API counterparts
2+
regarding which type of events are received in each function. Patch by Pablo Galindo Salgado.

0 commit comments

Comments
 (0)