summaryrefslogtreecommitdiff
path: root/Doc/library/sys.rst
diff options
context:
space:
mode:
authorXiang Zhang <angwerzx@126.com>2018-01-24 22:44:53 +0800
committerGitHub <noreply@github.com>2018-01-24 22:44:53 +0800
commitfd844efa9c31e1f00e04b07940875b9dacff3d77 (patch)
tree49a5f5a5f94e7822eb6db685c05e4432e70740f5 /Doc/library/sys.rst
parentd69794f4df81de731cc66dc82136e28bee691e1e (diff)
downloadcpython-git-fd844efa9c31e1f00e04b07940875b9dacff3d77.tar.gz
bpo-17799: Explain real behaviour of sys.settrace and sys.setprofile (GH-4056) (#5298)
(cherry picked from commit 131fd7f96c619bc7eaea956e45c6337175f4b27f)
Diffstat (limited to 'Doc/library/sys.rst')
-rw-r--r--Doc/library/sys.rst49
1 files changed, 32 insertions, 17 deletions
diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst
index dd51ffd56c..68521df37e 100644
--- a/Doc/library/sys.rst
+++ b/Doc/library/sys.rst
@@ -1005,13 +1005,38 @@ always available.
Set the system's profile function, which allows you to implement a Python source
code profiler in Python. See chapter :ref:`profile` for more information on the
Python profiler. The system's profile function is called similarly to the
- system's trace function (see :func:`settrace`), but it isn't called for each
- executed line of code (only on call and return, but the return event is reported
- even when an exception has been set). The function is thread-specific, but
- there is no way for the profiler to know about context switches between threads,
- so it does not make sense to use this in the presence of multiple threads. Also,
+ system's trace function (see :func:`settrace`), but it is called with different events,
+ for example it isn't called for each executed line of code (only on call and return,
+ but the return event is reported even when an exception has been set). The function is
+ thread-specific, but there is no way for the profiler to know about context switches between
+ threads, so it does not make sense to use this in the presence of multiple threads. Also,
its return value is not used, so it can simply return ``None``.
+ Profile functions should have three arguments: *frame*, *event*, and
+ *arg*. *frame* is the current stack frame. *event* is a string: ``'call'``,
+ ``'return'``, ``'c_call'``, ``'c_return'``, or ``'c_exception'``. *arg* depends
+ on the event type.
+
+ The events have the following meaning:
+
+ ``'call'``
+ A function is called (or some other code block entered). The
+ profile function is called; *arg* is ``None``.
+
+ ``'return'``
+ A function (or other code block) is about to return. The profile
+ function is called; *arg* is the value that will be returned, or ``None``
+ if the event is caused by an exception being raised.
+
+ ``'c_call'``
+ A C function is about to be called. This may be an extension function or
+ a built-in. *arg* is the C function object.
+
+ ``'c_return'``
+ A C function has returned. *arg* is the C function object.
+
+ ``'c_exception'``
+ A C function has raised an exception. *arg* is the C function object.
.. function:: setrecursionlimit(limit)
@@ -1058,8 +1083,8 @@ always available.
Trace functions should have three arguments: *frame*, *event*, and
*arg*. *frame* is the current stack frame. *event* is a string: ``'call'``,
- ``'line'``, ``'return'``, ``'exception'``, ``'c_call'``, ``'c_return'``, or
- ``'c_exception'``. *arg* depends on the event type.
+ ``'line'``, ``'return'`` or ``'exception'``. *arg* depends on
+ the event type.
The trace function is invoked (with *event* set to ``'call'``) whenever a new
local scope is entered; it should return a reference to a local trace
@@ -1094,16 +1119,6 @@ always available.
tuple ``(exception, value, traceback)``; the return value specifies the
new local trace function.
- ``'c_call'``
- A C function is about to be called. This may be an extension function or
- a built-in. *arg* is the C function object.
-
- ``'c_return'``
- A C function has returned. *arg* is the C function object.
-
- ``'c_exception'``
- A C function has raised an exception. *arg* is the C function object.
-
Note that as an exception is propagated down the chain of callers, an
``'exception'`` event is generated at each level.