summaryrefslogtreecommitdiffstats
path: root/Include/cpython
diff options
context:
space:
mode:
authorItamar Ostricher <itamarost@gmail.com>2022-12-02 17:28:27 (GMT)
committerGitHub <noreply@github.com>2022-12-02 17:28:27 (GMT)
commit3c137dc613c860f605d3520d7fd722cd8ed79da6 (patch)
tree1d790ed26497a0f7c5262a4a00ca6311cf3950e9 /Include/cpython
parent0563be23a557917228a8b48cbb31bda285a3a815 (diff)
downloadcpython-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 'Include/cpython')
-rw-r--r--Include/cpython/code.h35
1 files changed, 35 insertions, 0 deletions
diff --git a/Include/cpython/code.h b/Include/cpython/code.h
index fd57e00..f11d099 100644
--- a/Include/cpython/code.h
+++ b/Include/cpython/code.h
@@ -181,6 +181,41 @@ PyAPI_FUNC(int) PyCode_Addr2Line(PyCodeObject *, int);
PyAPI_FUNC(int) PyCode_Addr2Location(PyCodeObject *, int, int *, int *, int *, int *);
+typedef enum PyCodeEvent {
+ PY_CODE_EVENT_CREATE,
+ PY_CODE_EVENT_DESTROY
+} PyCodeEvent;
+
+
+/*
+ * A callback that is invoked for different events in a code object's lifecycle.
+ *
+ * The callback is invoked with a borrowed reference to co, after it is
+ * created and before it is destroyed.
+ *
+ * If the callback returns with an exception set, it must return -1. Otherwise
+ * it should return 0.
+ */
+typedef int (*PyCode_WatchCallback)(
+ PyCodeEvent event,
+ PyCodeObject* co);
+
+/*
+ * Register a per-interpreter callback that will be invoked for code object
+ * lifecycle events.
+ *
+ * Returns a handle that may be passed to PyCode_ClearWatcher on success,
+ * or -1 and sets an error if no more handles are available.
+ */
+PyAPI_FUNC(int) PyCode_AddWatcher(PyCode_WatchCallback callback);
+
+/*
+ * Clear the watcher associated with the watcher_id handle.
+ *
+ * Returns 0 on success or -1 if no watcher exists for the provided id.
+ */
+PyAPI_FUNC(int) PyCode_ClearWatcher(int watcher_id);
+
/* for internal use only */
struct _opaque {
int computed_line;