Clarify some details regarding `sys.monitoring` (#133981) · python/cpython@b499105 · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

Commit b499105

Browse files
authored
Clarify some details regarding sys.monitoring (#133981)
1 parent 0243f97 commit b499105

File tree

1 file changed

+36
-25
lines changed

1 file changed

+36
-25
lines changed

Doc/library/sys.monitoring.rst

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ The following events are supported:
137137

138138
.. monitoring-event:: PY_UNWIND
139139

140-
Exit from a Python function during exception unwinding.
140+
Exit from a Python function during exception unwinding. This includes exceptions raised directly within the
141+
function and that are allowed to continue to propagate.
141142

142143
.. monitoring-event:: PY_YIELD
143144

@@ -171,7 +172,7 @@ events, use the expression ``PY_RETURN | PY_START``.
171172
if get_events(DEBUGGER_ID) == NO_EVENTS:
172173
...
173174

174-
Events are divided into three groups:
175+
Setting this event deactivates all events.
175176

176177
.. _monitoring-event-local:
177178

@@ -243,20 +244,23 @@ raise an exception unless it would be visible to other code.
243244

244245
To allow tools to monitor for real exceptions without slowing down generators
245246
and coroutines, the :monitoring-event:`STOP_ITERATION` event is provided.
246-
:monitoring-event:`STOP_ITERATION` can be locally disabled, unlike :monitoring-event:`RAISE`.
247+
:monitoring-event:`STOP_ITERATION` can be locally disabled, unlike
248+
:monitoring-event:`RAISE`.
247249

248-
Note that the :monitoring-event:`STOP_ITERATION` event and the :monitoring-event:`RAISE`
249-
event for a :exc:`StopIteration` exception are equivalent, and are treated as interchangeable
250-
when generating events. Implementations will favor :monitoring-event:`STOP_ITERATION` for
251-
performance reasons, but may generate a :monitoring-event:`RAISE` event with a :exc:`StopIteration`.
250+
Note that the :monitoring-event:`STOP_ITERATION` event and the
251+
:monitoring-event:`RAISE` event for a :exc:`StopIteration` exception are
252+
equivalent, and are treated as interchangeable when generating events.
253+
Implementations will favor :monitoring-event:`STOP_ITERATION` for performance
254+
reasons, but may generate a :monitoring-event:`RAISE` event with a
255+
:exc:`StopIteration`.
252256

253257
Turning events on and off
254258
-------------------------
255259

256260
In order to monitor an event, it must be turned on and a corresponding callback
257-
must be registered.
258-
Events can be turned on or off by setting the events either globally or
259-
for a particular code object.
261+
must be registered. Events can be turned on or off by setting the events either
262+
globally and/or for a particular code object. An event will trigger only once,
263+
even if it is turned on both globally and locally.
260264

261265

262266
Setting events globally
@@ -292,10 +296,6 @@ in Python (see :ref:`c-api-monitoring`).
292296
Activates all the local events for *code* which are set in *event_set*.
293297
Raises a :exc:`ValueError` if *tool_id* is not in use.
294298

295-
Local events add to global events, but do not mask them.
296-
In other words, all global events will trigger for a code object,
297-
regardless of the local events.
298-
299299

300300
Disabling events
301301
''''''''''''''''
@@ -325,8 +325,6 @@ except for a few breakpoints.
325325
Registering callback functions
326326
------------------------------
327327

328-
To register a callable for events call
329-
330328
.. function:: register_callback(tool_id: int, event: int, func: Callable | None, /) -> Callable | None
331329

332330
Registers the callable *func* for the *event* with the given *tool_id*
@@ -335,12 +333,16 @@ To register a callable for events call
335333
it is unregistered and returned.
336334
Otherwise :func:`register_callback` returns ``None``.
337335

338-
339336
Functions can be unregistered by calling
340337
``sys.monitoring.register_callback(tool_id, event, None)``.
341338

342339
Callback functions can be registered and unregistered at any time.
343340

341+
Callbacks are called only once regardless if the event is turned on both
342+
globally and locally. As such, if an event could be turned on for both global
343+
and local events by your code then the callback needs to be written to handle
344+
either trigger.
345+
344346
Registering or unregistering a callback function will generate a :func:`sys.audit` event.
345347

346348

@@ -353,37 +355,46 @@ Callback function arguments
353355
that there are no arguments to the call.
354356

355357
When an active event occurs, the registered callback function is called.
358+
Callback functions returning an object other than :data:`DISABLE` will have no effect.
356359
Different events will provide the callback function with different arguments, as follows:
357360

358361
* :monitoring-event:`PY_START` and :monitoring-event:`PY_RESUME`::
359362

360-
func(code: CodeType, instruction_offset: int) -> DISABLE | Any
363+
func(code: CodeType, instruction_offset: int) -> object
361364

362365
* :monitoring-event:`PY_RETURN` and :monitoring-event:`PY_YIELD`::
363366

364-
func(code: CodeType, instruction_offset: int, retval: object) -> DISABLE | Any
367+
func(code: CodeType, instruction_offset: int, retval: object) -> object
365368

366-
* :monitoring-event:`CALL`, :monitoring-event:`C_RAISE` and :monitoring-event:`C_RETURN`::
369+
* :monitoring-event:`CALL`, :monitoring-event:`C_RAISE` and :monitoring-event:`C_RETURN`
370+
(*arg0* can be :data:`MISSING` specifically)::
367371

368-
func(code: CodeType, instruction_offset: int, callable: object, arg0: object | MISSING) -> DISABLE | Any
372+
func(code: CodeType, instruction_offset: int, callable: object, arg0: object) -> object
369373

374+
*code* represents the code object where the call is being made, while
375+
*callable* is the object that is about to be called (and thus
376+
triggered the event).
370377
If there are no arguments, *arg0* is set to :data:`sys.monitoring.MISSING`.
371378

379+
For instance methods, *callable* will be the function object as found on the
380+
class with *arg0* set to the instance (i.e. the ``self`` argument to the
381+
method).
382+
372383
* :monitoring-event:`RAISE`, :monitoring-event:`RERAISE`, :monitoring-event:`EXCEPTION_HANDLED`,
373384
:monitoring-event:`PY_UNWIND`, :monitoring-event:`PY_THROW` and :monitoring-event:`STOP_ITERATION`::
374385

375-
func(code: CodeType, instruction_offset: int, exception: BaseException) -> DISABLE | Any
386+
func(code: CodeType, instruction_offset: int, exception: BaseException) -> object
376387

377388
* :monitoring-event:`LINE`::
378389

379-
func(code: CodeType, line_number: int) -> DISABLE | Any
390+
func(code: CodeType, line_number: int) -> object
380391

381392
* :monitoring-event:`BRANCH_LEFT`, :monitoring-event:`BRANCH_RIGHT` and :monitoring-event:`JUMP`::
382393

383-
func(code: CodeType, instruction_offset: int, destination_offset: int) -> DISABLE | Any
394+
func(code: CodeType, instruction_offset: int, destination_offset: int) -> object
384395

385396
Note that the *destination_offset* is where the code will next execute.
386397

387398
* :monitoring-event:`INSTRUCTION`::
388399

389-
func(code: CodeType, instruction_offset: int) -> DISABLE | Any
400+
func(code: CodeType, instruction_offset: int) -> object

0 commit comments

Comments
 (0)

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.