Skip to content

Commit e64a47b

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

File tree

3 files changed

+41
-21
lines changed

3 files changed

+41
-21
lines changed

‎Doc/c-api/init.rst‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,15 +1057,19 @@ in previous versions.
10571057
function as its first parameter, and may be any Python object, or *NULL*. If
10581058
the profile function needs to maintain state, using a different value for *obj*
10591059
for each thread provides a convenient and thread-safe place to store it. The
1060-
profile function is called for all monitored events except the line-number
1061-
events.
1060+
profile function is called for all monitored events except :const:`PyTrace_LINE`
1061+
and :const:`PyTrace_EXCEPTION`.
10621062
10631063
10641064
.. c:function:: void PyEval_SetTrace(Py_tracefunc func, PyObject *obj)
10651065
10661066
Set the tracing function to *func*. This is similar to
10671067
:c:func:`PyEval_SetProfile`, except the tracing function does receive line-number
1068-
events.
1068+
events and does not receive any event related to C function objects being called. Any
1069+
trace function registered using :c:func:`PyEval_SetTrace` will not receive
1070+
:const:`PyTrace_C_CALL`, :const:`PyTrace_C_EXCEPTION` or :const:`PyTrace_C_RETURN`
1071+
as a value for the *what* parameter.
1072+
10691073
10701074
.. c:function:: PyObject* PyEval_GetCallStats(PyObject *self)
10711075

‎Doc/library/sys.rst‎

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

867+
Profile functions should have three arguments: *frame*, *event*, and
868+
*arg*. *frame* is the current stack frame. *event* is a string: ``'call'``,
869+
``'return'``, ``'c_call'``, ``'c_return'``, or ``'c_exception'``. *arg* depends
870+
on the event type.
871+
872+
The events have the following meaning:
873+
874+
``'call'``
875+
A function is called (or some other code block entered). The
876+
profile function is called; *arg* is ``None``.
877+
878+
``'return'``
879+
A function (or other code block) is about to return. The profile
880+
function is called; *arg* is the value that will be returned, or ``None``
881+
if the event is caused by an exception being raised.
882+
883+
``'c_call'``
884+
A C function is about to be called. This may be an extension function or
885+
a built-in. *arg* is the C function object.
886+
887+
``'c_return'``
888+
A C function has returned. *arg* is the C function object.
889+
890+
``'c_exception'``
891+
A C function has raised an exception. *arg* is the C function object.
867892

868893
.. function:: setrecursionlimit(limit)
869894

@@ -890,8 +915,8 @@ always available.
890915

891916
Trace functions should have three arguments: *frame*, *event*, and
892917
*arg*. *frame* is the current stack frame. *event* is a string: ``'call'``,
893-
``'line'``, ``'return'``, ``'exception'``, ``'c_call'``, ``'c_return'``, or
894-
``'c_exception'``. *arg* depends on the event type.
918+
``'line'``, ``'return'`` or ``'exception'``. *arg* depends on
919+
the event type.
895920

896921
The trace function is invoked (with *event* set to ``'call'``) whenever a new
897922
local scope is entered; it should return a reference to a local trace
@@ -926,16 +951,6 @@ always available.
926951
tuple ``(exception, value, traceback)``; the return value specifies the
927952
new local trace function.
928953

929-
``'c_call'``
930-
A C function is about to be called. This may be an extension function or
931-
a built-in. *arg* is the C function object.
932-
933-
``'c_return'``
934-
A C function has returned. *arg* is the C function object.
935-
936-
``'c_exception'``
937-
A C function has raised an exception. *arg* is the C function object.
938-
939954
Note that as an exception is propagated down the chain of callers, an
940955
``'exception'`` event is generated at each level.
941956

@@ -1078,4 +1093,3 @@ always available.
10781093
.. rubric:: Citations
10791094

10801095
.. [C99] ISO/IEC 9899:1999. "Programming languages -- C." A public draft of this standard is available at http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf\ .
1081-
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)