sys.monitoring
— 执行事件监视
¶
3.12 版添加。
sys.monitoring
— 执行事件监视
¶
3.12 版添加。
注意
sys.monitoring
是名称空间在
sys
模块,不是独立模块,因此不需要
import sys.monitoring
,只需
import sys
然后使用
sys.monitoring
.
此名称空间提供对有必要激活和控制事件监视,函数和常量的访问。
当程序执行时,可能出现对监视执行的工具感兴趣的事件。
sys.monitoring
名称空间提供接收回调的手段,当对出现事件感兴趣时。
监视 API 由 3 个组件组成:
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.
必须被调用,先于
tool_id
可以使用。
tool_id
必须在范围 0 到 5 (包括)。引发
ValueError
if
tool_id
在使用中。
Should be called once a tool no longer requires tool_id .
注意
free_tool_id()
will not disable global or local events associated with
tool_id
, nor will it unregister any callback functions. This function is only intended to be used to notify the VM that the particular
tool_id
is no longer in use.
返回工具的名称,若
tool_id
在使用中,否则返回
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 = 5
支持下列事件:
A conditional branch is taken (or not).
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()
调用。
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()
调用。
Exit from a Python function during exception unwinding.
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
事件。
人工
StopIteration
被引发;见
the STOP_ITERATION event
.
未来可能添加更多事件。
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
.
别名化的
0
so users can do explicit comparisons like:
if get_events(DEBUGGER_ID) == NO_EVENTS: ...
Events are divided into three groups:
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:
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
事件。
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:
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
.
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 or for a particular code object.
Events can be controlled globally by modifying the set of events being monitored.
返回
int
representing all the active events.
Activates all events which are set in
event_set
。引发
ValueError
if
tool_id
is not in use.
No events are active by default.
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
。引发
ValueError
if
tool_id
is not in use.
Local events add to global events, but do not mask them. In other words, all global events will trigger for a code object, regardless of the local events.
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.
To register a callable for events call
Registers the callable func 为 event 采用给定 tool_id
If another callback was registered for the given
tool_id
and
event
, it is unregistered and returned. Otherwise
register_callback()
返回
None
.
Functions can be unregistered by calling
sys.monitoring.register_callback(tool_id, event, None)
.
Callback functions can be registered and unregistered at any time.
Registering or unregistering a callback function will generate a
sys.audit()
事件。
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. Different events will provide the callback function with different arguments, as follows:
func(code: CodeType, instruction_offset: int) -> DISABLE | Any
func(code: CodeType, instruction_offset: int, retval: object) -> DISABLE | Any
func(code: CodeType, instruction_offset: int, callable: object, arg0: object | MISSING) -> DISABLE | Any
若没有自变量,
arg0
被设为
sys.monitoring.MISSING
.
RAISE
,
RERAISE
,
EXCEPTION_HANDLED
,
PY_UNWIND
,
PY_THROW
and
STOP_ITERATION
:
func(code: CodeType, instruction_offset: int, exception: BaseException) -> DISABLE | Any
LINE
:
func(code: CodeType, line_number: int) -> DISABLE | Any
func(code: CodeType, instruction_offset: int, destination_offset: int) -> DISABLE | Any
注意, destination_offset is where the code will next execute. For an untaken branch this will be the offset of the instruction following the branch.
func(code: CodeType, instruction_offset: int) -> DISABLE | Any