sys.monitoring
â Execution event monitoring¶
Added in version 3.12.
Note
sys.monitoring
is a namespace within the sys
module, not an independent module, so there is no need to import sys.monitoring
, simply import sys
and then use sys.monitoring
.
This namespace provides access to the functions and constants necessary to activate and control event monitoring.
As programs execute, events occur that might be of interest to tools that monitor execution. The sys.monitoring
namespace provides means to receive callbacks when events of interest occur.
The monitoring API consists of three components:
Tool identifiers¶A tool identifier is an integer and the associated name. Tool identifiers are used to discourage tools from interfering with each other and to allow multiple tools to operate at the same time. Currently tools are completely independent and cannot be used to monitor each other. This restriction may be lifted in the future.
Before registering or activating events, a tool should choose an identifier. Identifiers are integers in the range 0 to 5 inclusive.
Registering and using tools¶Must be called before tool_id can be used. tool_id must be in the range 0 to 5 inclusive. Raises a ValueError
if tool_id is in use.
Unregister all events and callback functions associated with tool_id.
Should be called once a tool no longer requires tool_id. Will call clear_tool_id()
before releasing tool_id.
Returns the name of the tool if tool_id is in use, otherwise it returns None
. tool_id must be in the range 0 to 5 inclusive.
All IDs are treated the same by the VM with regard to events, but the following IDs are pre-defined to make co-operation of tools easier:
sys.monitoring.DEBUGGER_ID = 0 sys.monitoring.COVERAGE_ID = 1 sys.monitoring.PROFILER_ID = 2 sys.monitoring.OPTIMIZER_ID = 5Events¶
The following events are supported:
A conditional branch goes left.
It is up to the tool to determine how to present âleftâ and ârightâ branches. There is no guarantee which branch is âleftâ and which is ârightâ, except that it will be consistent for the duration of the program.
A conditional branch goes right.
A call in Python code (event occurs before the call).
An exception raised from any callable, except for Python functions (event occurs after the exit).
Return from any callable, except for Python functions (event occurs after the return).
An exception is handled.
A VM instruction is about to be executed.
An unconditional jump in the control flow graph is made.
An instruction is about to be executed that has a different line number from the preceding instruction.
Resumption of a Python function (for generator and coroutine functions), except for throw()
calls.
Return from a Python function (occurs immediately before the return, the calleeâs frame will be on the stack).
Start of a Python function (occurs immediately after the call, the calleeâs frame will be on the stack)
A Python function is resumed by a throw()
call.
Exit from a Python function during exception unwinding. This includes exceptions raised directly within the function and that are allowed to continue to propagate.
Yield from a Python function (occurs immediately before the yield, the calleeâs frame will be on the stack).
An exception is raised, except those that cause a STOP_ITERATION
event.
An exception is re-raised, for example at the end of a finally
block.
An artificial StopIteration
is raised; see the STOP_ITERATION event.
More events may be added in the future.
These events are attributes of the sys.monitoring.events
namespace. Each event is represented as a power-of-2 integer constant. To define a set of events, simply bitwise OR the individual events together. For example, to specify both PY_RETURN
and PY_START
events, use the expression PY_RETURN | PY_START
.
An alias for 0
so users can do explicit comparisons like:
if get_events(DEBUGGER_ID) == NO_EVENTS: ...
Setting this event deactivates all events.
Local events are associated with normal execution of the program and happen at clearly defined locations. All local events can be disabled. The local events are:
BRANCH
The BRANCH
event is deprecated in 3.14. Using BRANCH_LEFT
and BRANCH_RIGHT
events will give much better performance as they can be disabled independently.
Ancillary events can be monitored like other events, but are controlled by another event:
The C_RETURN
and C_RAISE
events are controlled by the CALL
event. C_RETURN
and C_RAISE
events will only be seen if the corresponding CALL
event is being monitored.
Other events are not necessarily tied to a specific location in the program and cannot be individually disabled.
The other events that can be monitored are:
The STOP_ITERATION event¶PEP 380 specifies that a StopIteration
exception is raised when returning a value from a generator or coroutine. However, this is a very inefficient way to return a value, so some Python implementations, notably CPython 3.12+, do not raise an exception unless it would be visible to other code.
To allow tools to monitor for real exceptions without slowing down generators and coroutines, the STOP_ITERATION
event is provided. STOP_ITERATION
can be locally disabled, unlike RAISE
.
Note that the STOP_ITERATION
event and the RAISE
event for a StopIteration
exception are equivalent, and are treated as interchangeable when generating events. Implementations will favor STOP_ITERATION
for performance reasons, but may generate a RAISE
event with a StopIteration
.
In order to monitor an event, it must be turned on and a corresponding callback must be registered. Events can be turned on or off by setting the events either globally and/or for a particular code object. An event will trigger only once, even if it is turned on both globally and locally.
Setting events globally¶Events can be controlled globally by modifying the set of events being monitored.
Returns the int
representing all the active events.
Activates all events which are set in event_set. Raises a ValueError
if tool_id is not in use.
No events are active by default.
Per code object events¶Events can also be controlled on a per code object basis. The functions defined below which accept a types.CodeType
should be prepared to accept a look-alike object from functions which are not defined in Python (see Monitoring C API).
Returns all the local events for code
Activates all the local events for code which are set in event_set. Raises a ValueError
if tool_id is not in use.
A special value that can be returned from a callback function to disable events for the current code location.
Local events can be disabled for a specific code location by returning sys.monitoring.DISABLE
from a callback function. This does not change which events are set, or any other code locations for the same event.
Disabling events for specific locations is very important for high performance monitoring. For example, a program can be run under a debugger with no overhead if the debugger disables all monitoring except for a few breakpoints.
Enable all the events that were disabled by sys.monitoring.DISABLE
for all tools.
Registers the callable func for the event with the given tool_id
If another callback was registered for the given tool_id and event, it is unregistered and returned. Otherwise register_callback()
returns None
.
Raises an auditing event sys.monitoring.register_callback
with argument func
.
Functions can be unregistered by calling sys.monitoring.register_callback(tool_id, event, None)
.
Callback functions can be registered and unregistered at any time.
Callbacks are called only once regardless if the event is turned on both globally and locally. As such, if an event could be turned on for both global and local events by your code then the callback needs to be written to handle either trigger.
Callback function arguments¶A special value that is passed to a callback function to indicate that there are no arguments to the call.
When an active event occurs, the registered callback function is called. Callback functions returning an object other than DISABLE
will have no effect. Different events will provide the callback function with different arguments, as follows:
func(code: CodeType, instruction_offset: int) -> object
func(code: CodeType, instruction_offset: int, retval: object) -> object
CALL
, C_RAISE
and C_RETURN
(arg0 can be MISSING
specifically):
func(code: CodeType, instruction_offset: int, callable: object, arg0: object) -> object
code represents the code object where the call is being made, while callable is the object that is about to be called (and thus triggered the event). If there are no arguments, arg0 is set to sys.monitoring.MISSING
.
For instance methods, callable will be the function object as found on the class with arg0 set to the instance (i.e. the self
argument to the method).
RAISE
, RERAISE
, EXCEPTION_HANDLED
, PY_UNWIND
, PY_THROW
and STOP_ITERATION
:
func(code: CodeType, instruction_offset: int, exception: BaseException) -> object
LINE
:
func(code: CodeType, line_number: int) -> object
BRANCH_LEFT
, BRANCH_RIGHT
and JUMP
:
func(code: CodeType, instruction_offset: int, destination_offset: int) -> object
Note that the destination_offset is where the code will next execute.
func(code: CodeType, instruction_offset: int) -> object
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4