@@ -137,7 +137,8 @@ The following events are supported:
137
137
138
138
.. monitoring-event :: PY_UNWIND
139
139
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.
141
142
142
143
.. monitoring-event :: PY_YIELD
143
144
@@ -171,7 +172,7 @@ events, use the expression ``PY_RETURN | PY_START``.
171
172
if get_events(DEBUGGER_ID) == NO_EVENTS:
172
173
...
173
174
174
- Events are divided into three groups:
175
+ Setting this event deactivates all events.
175
176
176
177
.. _monitoring-event-local :
177
178
@@ -243,20 +244,23 @@ raise an exception unless it would be visible to other code.
243
244
244
245
To allow tools to monitor for real exceptions without slowing down generators
245
246
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 `.
247
249
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 `.
252
256
253
257
Turning events on and off
254
258
-------------------------
255
259
256
260
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 .
260
264
261
265
262
266
Setting events globally
@@ -292,10 +296,6 @@ in Python (see :ref:`c-api-monitoring`).
292
296
Activates all the local events for *code * which are set in *event_set *.
293
297
Raises a :exc: `ValueError ` if *tool_id * is not in use.
294
298
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
-
299
299
300
300
Disabling events
301
301
''''''''''''''''
@@ -325,8 +325,6 @@ except for a few breakpoints.
325
325
Registering callback functions
326
326
------------------------------
327
327
328
- To register a callable for events call
329
-
330
328
.. function :: register_callback(tool_id: int, event: int, func: Callable | None, /) -> Callable | None
331
329
332
330
Registers the callable *func * for the *event * with the given *tool_id *
@@ -335,12 +333,16 @@ To register a callable for events call
335
333
it is unregistered and returned.
336
334
Otherwise :func: `register_callback ` returns ``None ``.
337
335
338
-
339
336
Functions can be unregistered by calling
340
337
``sys.monitoring.register_callback(tool_id, event, None) ``.
341
338
342
339
Callback functions can be registered and unregistered at any time.
343
340
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
+
344
346
Registering or unregistering a callback function will generate a :func: `sys.audit ` event.
345
347
346
348
@@ -353,37 +355,46 @@ Callback function arguments
353
355
that there are no arguments to the call.
354
356
355
357
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.
356
359
Different events will provide the callback function with different arguments, as follows:
357
360
358
361
* :monitoring-event: `PY_START ` and :monitoring-event: `PY_RESUME `::
359
362
360
- func(code: CodeType, instruction_offset: int) -> DISABLE | Any
363
+ func(code: CodeType, instruction_offset: int) -> object
361
364
362
365
* :monitoring-event: `PY_RETURN ` and :monitoring-event: `PY_YIELD `::
363
366
364
- func(code: CodeType, instruction_offset: int, retval: object) -> DISABLE | Any
367
+ func(code: CodeType, instruction_offset: int, retval: object) -> object
365
368
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)::
367
371
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
369
373
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).
370
377
If there are no arguments, *arg0 * is set to :data: `sys.monitoring.MISSING `.
371
378
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
+
372
383
* :monitoring-event: `RAISE `, :monitoring-event: `RERAISE `, :monitoring-event: `EXCEPTION_HANDLED `,
373
384
:monitoring-event: `PY_UNWIND `, :monitoring-event: `PY_THROW ` and :monitoring-event: `STOP_ITERATION `::
374
385
375
- func(code: CodeType, instruction_offset: int, exception: BaseException) -> DISABLE | Any
386
+ func(code: CodeType, instruction_offset: int, exception: BaseException) -> object
376
387
377
388
* :monitoring-event: `LINE `::
378
389
379
- func(code: CodeType, line_number: int) -> DISABLE | Any
390
+ func(code: CodeType, line_number: int) -> object
380
391
381
392
* :monitoring-event: `BRANCH_LEFT `, :monitoring-event: `BRANCH_RIGHT ` and :monitoring-event: `JUMP `::
382
393
383
- func(code: CodeType, instruction_offset: int, destination_offset: int) -> DISABLE | Any
394
+ func(code: CodeType, instruction_offset: int, destination_offset: int) -> object
384
395
385
396
Note that the *destination_offset* is where the code will next execute.
386
397
387
398
* :monitoring-event: `INSTRUCTION `::
388
399
389
- func(code: CodeType, instruction_offset: int) -> DISABLE | Any
400
+ func(code: CodeType, instruction_offset: int) -> object
0 commit comments