Skip to content

Commit 131fd7f

Browse files
pablogsalzhangyangyu
authored andcommitted
bpo-17799: Explain real behaviour of sys.settrace and sys.setprofile (#4056)
1 parent 018e1b7 commit 131fd7f

File tree

3 files changed

+40
-22
lines changed

3 files changed

+40
-22
lines changed

Doc/c-api/init.rst

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,6 @@ Python-level trace functions in previous versions.
13001300
| :const:`PyTrace_C_RETURN` | Function object being called. |
13011301
+------------------------------+--------------------------------------+
13021302
1303-
13041303
.. c:var:: int PyTrace_CALL
13051304
13061305
The value of the *what* parameter to a :c:type:`Py_tracefunc` function when a new
@@ -1357,16 +1356,18 @@ Python-level trace functions in previous versions.
13571356
function as its first parameter, and may be any Python object, or *NULL*. If
13581357
the profile function needs to maintain state, using a different value for *obj*
13591358
for each thread provides a convenient and thread-safe place to store it. The
1360-
profile function is called for all monitored events except the line-number
1361-
events.
1359+
profile function is called for all monitored events except :const:`PyTrace_LINE`
1360+
and :const:`PyTrace_EXCEPTION`.
13621361
13631362
13641363
.. c:function:: void PyEval_SetTrace(Py_tracefunc func, PyObject *obj)
13651364
13661365
Set the tracing function to *func*. This is similar to
13671366
:c:func:`PyEval_SetProfile`, except the tracing function does receive line-number
1368-
events.
1369-
1367+
events and does not receive any event related to C function objects being called. Any
1368+
trace function registered using :c:func:`PyEval_SetTrace` will not receive
1369+
:const:`PyTrace_C_CALL`, :const:`PyTrace_C_EXCEPTION` or :const:`PyTrace_C_RETURN`
1370+
as a value for the *what* parameter.
13701371
13711372
.. _advanced-debugging:
13721373

Doc/library/sys.rst

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

1094+
Profile functions should have three arguments: *frame*, *event*, and
1095+
*arg*. *frame* is the current stack frame. *event* is a string: ``'call'``,
1096+
``'return'``, ``'c_call'``, ``'c_return'``, or ``'c_exception'``. *arg* depends
1097+
on the event type.
1098+
1099+
The events have the following meaning:
1100+
1101+
``'call'``
1102+
A function is called (or some other code block entered). The
1103+
profile function is called; *arg* is ``None``.
1104+
1105+
``'return'``
1106+
A function (or other code block) is about to return. The profile
1107+
function is called; *arg* is the value that will be returned, or ``None``
1108+
if the event is caused by an exception being raised.
1109+
1110+
``'c_call'``
1111+
A C function is about to be called. This may be an extension function or
1112+
a built-in. *arg* is the C function object.
1113+
1114+
``'c_return'``
1115+
A C function has returned. *arg* is the C function object.
1116+
1117+
``'c_exception'``
1118+
A C function has raised an exception. *arg* is the C function object.
10941119

10951120
.. function:: setrecursionlimit(limit)
10961121

@@ -1137,8 +1162,8 @@ always available.
11371162

11381163
Trace functions should have three arguments: *frame*, *event*, and
11391164
*arg*. *frame* is the current stack frame. *event* is a string: ``'call'``,
1140-
``'line'``, ``'return'``, ``'exception'``, ``'c_call'``, ``'c_return'``, or
1141-
``'c_exception'``, ``'opcode'``. *arg* depends on the event type.
1165+
``'line'``, ``'return'``, ``'exception'`` or ``'opcode'``. *arg* depends on
1166+
the event type.
11421167

11431168
The trace function is invoked (with *event* set to ``'call'``) whenever a new
11441169
local scope is entered; it should return a reference to a local trace
@@ -1175,16 +1200,6 @@ always available.
11751200
tuple ``(exception, value, traceback)``; the return value specifies the
11761201
new local trace function.
11771202

1178-
``'c_call'``
1179-
A C function is about to be called. This may be an extension function or
1180-
a built-in. *arg* is the C function object.
1181-
1182-
``'c_return'``
1183-
A C function has returned. *arg* is the C function object.
1184-
1185-
``'c_exception'``
1186-
A C function has raised an exception. *arg* is the C function object.
1187-
11881203
``'opcode'``
11891204
The interpreter is about to execute a new opcode (see :mod:`dis` for
11901205
opcode details). The local trace function is called; *arg* is
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)