diff options
author | Itamar Ostricher <itamarost@gmail.com> | 2022-12-02 17:28:27 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-02 17:28:27 (GMT) |
commit | 3c137dc613c860f605d3520d7fd722cd8ed79da6 (patch) | |
tree | 1d790ed26497a0f7c5262a4a00ca6311cf3950e9 /Doc/c-api/code.rst | |
parent | 0563be23a557917228a8b48cbb31bda285a3a815 (diff) | |
download | cpython-3c137dc613c860f605d3520d7fd722cd8ed79da6.zip cpython-3c137dc613c860f605d3520d7fd722cd8ed79da6.tar.gz cpython-3c137dc613c860f605d3520d7fd722cd8ed79da6.tar.bz2 |
GH-91054: Add code object watchers API (GH-99859)
* Add API to allow extensions to set callback function on creation and destruction of PyCodeObject
Co-authored-by: Ye11ow-Flash <janshah@cs.stonybrook.edu>
Diffstat (limited to 'Doc/c-api/code.rst')
-rw-r--r-- | Doc/c-api/code.rst | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/Doc/c-api/code.rst b/Doc/c-api/code.rst index 9054e7e..a6eb86f 100644 --- a/Doc/c-api/code.rst +++ b/Doc/c-api/code.rst @@ -115,3 +115,51 @@ bound into a function. the free variables. On error, ``NULL`` is returned and an exception is raised. .. versionadded:: 3.11 + +.. c:function:: int PyCode_AddWatcher(PyCode_WatchCallback callback) + + Register *callback* as a code object watcher for the current interpreter. + Return an ID which may be passed to :c:func:`PyCode_ClearWatcher`. + In case of error (e.g. no more watcher IDs available), + return ``-1`` and set an exception. + + .. versionadded:: 3.12 + +.. c:function:: int PyCode_ClearWatcher(int watcher_id) + + Clear watcher identified by *watcher_id* previously returned from + :c:func:`PyCode_AddWatcher` for the current interpreter. + Return ``0`` on success, or ``-1`` and set an exception on error + (e.g. if the given *watcher_id* was never registered.) + + .. versionadded:: 3.12 + +.. c:type:: PyCodeEvent + + Enumeration of possible code object watcher events: + - ``PY_CODE_EVENT_CREATE`` + - ``PY_CODE_EVENT_DESTROY`` + + .. versionadded:: 3.12 + +.. c:type:: int (*PyCode_WatchCallback)(PyCodeEvent event, PyCodeObject* co) + + Type of a code object watcher callback function. + + If *event* is ``PY_CODE_EVENT_CREATE``, then the callback is invoked + after `co` has been fully initialized. Otherwise, the callback is invoked + before the destruction of *co* takes place, so the prior state of *co* + can be inspected. + + Users of this API should not rely on internal runtime implementation + details. Such details may include, but are not limited to, the exact + order and timing of creation and destruction of code objects. While + changes in these details may result in differences observable by watchers + (including whether a callback is invoked or not), it does not change + the semantics of the Python code being executed. + + If the callback returns with an exception set, it must return ``-1``; this + exception will be printed as an unraisable exception using + :c:func:`PyErr_WriteUnraisable`. Otherwise it should return ``0``. + + .. versionadded:: 3.12 |