sys.monitoring — Execution event monitoring

Added in version 3.12.


Note

sys.monitoring is a namespace within thesys module,not an independent module, so there is no need toimportsys.monitoring, simplyimportsys and then usesys.monitoring.

This namespace provides access to the functions and constants necessary toactivate and control event monitoring.

As programs execute, events occur that might be of interest to tools thatmonitor execution. Thesys.monitoring namespace provides means toreceive 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 eachother and to allow multiple tools to operate at the same time.Currently tools are completely independent and cannot be used tomonitor 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

sys.monitoring.use_tool_id(tool_id:int,name:str,/)None

Must be called beforetool_id can be used.tool_id must be in the range 0 to 5 inclusive.Raises aValueError iftool_id is in use.

sys.monitoring.clear_tool_id(tool_id:int,/)None

Unregister all events and callback functions associated withtool_id.

sys.monitoring.free_tool_id(tool_id:int,/)None

Should be called once a tool no longer requirestool_id.Will callclear_tool_id() before releasingtool_id.

sys.monitoring.get_tool(tool_id:int,/)str|None

Returns the name of the tool iftool_id is in use,otherwise it returnsNone.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 thefollowing IDs are pre-defined to make co-operation of tools easier:

sys.monitoring.DEBUGGER_ID=0sys.monitoring.COVERAGE_ID=1sys.monitoring.PROFILER_ID=2sys.monitoring.OPTIMIZER_ID=5

Events

The following events are supported:

sys.monitoring.events.BRANCH_LEFT

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”, exceptthat it will be consistent for the duration of the program.

sys.monitoring.events.BRANCH_RIGHT

A conditional branch goes right.

sys.monitoring.events.CALL

A call in Python code (event occurs before the call).

sys.monitoring.events.C_RAISE

An exception raised from any callable, except for Python functions (event occurs after the exit).

sys.monitoring.events.C_RETURN

Return from any callable, except for Python functions (event occurs after the return).

sys.monitoring.events.EXCEPTION_HANDLED

An exception is handled.

sys.monitoring.events.INSTRUCTION

A VM instruction is about to be executed.

sys.monitoring.events.JUMP

An unconditional jump in the control flow graph is made.

sys.monitoring.events.LINE

An instruction is about to be executed that has a different line number from the preceding instruction.

sys.monitoring.events.PY_RESUME

Resumption of a Python function (for generator and coroutine functions), except forthrow() calls.

sys.monitoring.events.PY_RETURN

Return from a Python function (occurs immediately before the return, the callee’s frame will be on the stack).

sys.monitoring.events.PY_START

Start of a Python function (occurs immediately after the call, the callee’s frame will be on the stack)

sys.monitoring.events.PY_THROW

A Python function is resumed by athrow() call.

sys.monitoring.events.PY_UNWIND

Exit from a Python function during exception unwinding. This includes exceptions raised directly within thefunction and that are allowed to continue to propagate.

sys.monitoring.events.PY_YIELD

Yield from a Python function (occurs immediately before the yield, the callee’s frame will be on the stack).

sys.monitoring.events.RAISE

An exception is raised, except those that cause aSTOP_ITERATION event.

sys.monitoring.events.RERAISE

An exception is re-raised, for example at the end of afinally block.

sys.monitoring.events.STOP_ITERATION

An artificialStopIteration is raised; seethe STOP_ITERATION event.

More events may be added in the future.

These events are attributes of thesys.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 bothPY_RETURN andPY_STARTevents, use the expressionPY_RETURN|PY_START.

sys.monitoring.events.NO_EVENTS

An alias for0 so users can do explicit comparisons like:

ifget_events(DEBUGGER_ID)==NO_EVENTS:...

Setting this event deactivates all events.

Local events

Local events are associated with normal execution of the program and happenat clearly defined locations. All local events can be disabled.The local events are:

Deprecated event

  • BRANCH

TheBRANCH event is deprecated in 3.14.UsingBRANCH_LEFT andBRANCH_RIGHTevents will give much better performance as they can be disabledindependently.

Ancillary events

Ancillary events can be monitored like other events, but are controlledby another event:

TheC_RETURN andC_RAISE eventsare controlled by theCALL event.C_RETURN andC_RAISE events will only beseen if the correspondingCALL event is being monitored.

Other events

Other events are not necessarily tied to a specific location in theprogram and cannot be individually disabled viaDISABLE.

The other events that can be monitored are:

The STOP_ITERATION event

PEP 380specifies that aStopIteration exception is raised when returning a valuefrom a generator or coroutine. However, this is a very inefficient way toreturn a value, so some Python implementations, notably CPython 3.12+, do notraise an exception unless it would be visible to other code.

To allow tools to monitor for real exceptions without slowing down generatorsand coroutines, theSTOP_ITERATION event is provided.STOP_ITERATION can be locally disabled, unlikeRAISE.

Note that theSTOP_ITERATION event and theRAISE event for aStopIteration exception areequivalent, and are treated as interchangeable when generating events.Implementations will favorSTOP_ITERATION for performancereasons, but may generate aRAISE event with aStopIteration.

Turning events on and off

In order to monitor an event, it must be turned on and a corresponding callbackmust be registered. Events can be turned on or off by setting the events eitherglobally 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.

sys.monitoring.get_events(tool_id:int,/)int

Returns theint representing all the active events.

sys.monitoring.set_events(tool_id:int,event_set:int,/)None

Activates all events which are set inevent_set.Raises aValueError iftool_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 functionsdefined below which accept atypes.CodeType should be preparedto accept a look-alike object from functions which are not definedin Python (seeMonitoring C API).

sys.monitoring.get_local_events(tool_id:int,code:CodeType,/)int

Returns all thelocal events forcode

sys.monitoring.set_local_events(tool_id:int,code:CodeType,event_set:int,/)None

Activates all thelocal events forcodewhich are set inevent_set. Raises aValueError iftool_id is notin use.

Disabling events

sys.monitoring.DISABLE

A special value that can be returned from a callback function to disableevents for the current code location.

Local events can be disabled for a specific codelocation by returningsys.monitoring.DISABLE from a callback function.This does not change which events are set, or any other code locations for thesame event.

Disabling events for specific locations is very important for highperformance monitoring. For example, a program can be run under adebugger with no overhead if the debugger disables all monitoringexcept for a few breakpoints.

IfDISABLE is returned by a callback for aglobal event,ValueError will be raisedby the interpreter in a non-specific location (that is, no traceback will beprovided).

sys.monitoring.restart_events()None

Enable all the events that were disabled bysys.monitoring.DISABLEfor all tools.

Registering callback functions

sys.monitoring.register_callback(tool_id:int,event:int,func:Callable|None,/)Callable|None

Registers the callablefunc for theevent with the giventool_id

If another callback was registered for the giventool_id andevent,it is unregistered and returned.Otherwiseregister_callback() returnsNone.

Raises anauditing eventsys.monitoring.register_callback with argumentfunc.

Functions can be unregistered by callingsys.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 bothglobally and locally. As such, if an event could be turned on for both globaland local events by your code then the callback needs to be written to handleeither trigger.

Callback function arguments

sys.monitoring.MISSING

A special value that is passed to a callback function to indicatethat there are no arguments to the call.

When an active event occurs, the registered callback function is called.Callback functions returning an object other thanDISABLE will have no effect.Different events will provide the callback function with different arguments, as follows:

  • PY_START andPY_RESUME:

    func(code:CodeType,instruction_offset:int)->object
  • PY_RETURN andPY_YIELD:

    func(code:CodeType,instruction_offset:int,retval:object)->object
  • CALL,C_RAISE andC_RETURN(arg0 can beMISSING specifically):

    func(code:CodeType,instruction_offset:int,callable:object,arg0:object)->object

    code represents the code object where the call is being made, whilecallable is the object that is about to be called (and thustriggered the event).If there are no arguments,arg0 is set tosys.monitoring.MISSING.

    For instance methods,callable will be the function object as found on theclass witharg0 set to the instance (i.e. theself argument to themethod).

  • RAISE,RERAISE,EXCEPTION_HANDLED,PY_UNWIND,PY_THROW andSTOP_ITERATION:

    func(code:CodeType,instruction_offset:int,exception:BaseException)->object
  • LINE:

    func(code:CodeType,line_number:int)->object
  • BRANCH_LEFT,BRANCH_RIGHT andJUMP:

    func(code:CodeType,instruction_offset:int,destination_offset:int)->object

    Note that thedestination_offset is where the code will next execute.

  • INSTRUCTION:

    func(code:CodeType,instruction_offset:int)->object