summaryrefslogtreecommitdiff
path: root/Include
diff options
context:
space:
mode:
authormpage <mpage@cs.stanford.edu>2022-11-22 04:06:44 -0800
committerGitHub <noreply@github.com>2022-11-22 13:06:44 +0100
commit3db0a21f731cec28a89f7495a82ee2670bce75fe (patch)
tree4a4cc72b265076d18d9dd4ec22d9e5013425cffc /Include
parent20d9749a0f9b9fa6946019f04a54b6287d16588e (diff)
downloadcpython-git-3db0a21f731cec28a89f7495a82ee2670bce75fe.tar.gz
gh-91053: Add an optional callback that is invoked whenever a function is modified (#98175)
Diffstat (limited to 'Include')
-rw-r--r--Include/cpython/funcobject.h49
-rw-r--r--Include/internal/pycore_function.h2
-rw-r--r--Include/internal/pycore_interp.h6
3 files changed, 57 insertions, 0 deletions
diff --git a/Include/cpython/funcobject.h b/Include/cpython/funcobject.h
index dd8f20b2c2..5979febc2e 100644
--- a/Include/cpython/funcobject.h
+++ b/Include/cpython/funcobject.h
@@ -131,6 +131,55 @@ PyAPI_DATA(PyTypeObject) PyStaticMethod_Type;
PyAPI_FUNC(PyObject *) PyClassMethod_New(PyObject *);
PyAPI_FUNC(PyObject *) PyStaticMethod_New(PyObject *);
+#define FOREACH_FUNC_EVENT(V) \
+ V(CREATE) \
+ V(DESTROY) \
+ V(MODIFY_CODE) \
+ V(MODIFY_DEFAULTS) \
+ V(MODIFY_KWDEFAULTS)
+
+typedef enum {
+ #define DEF_EVENT(EVENT) PyFunction_EVENT_##EVENT,
+ FOREACH_FUNC_EVENT(DEF_EVENT)
+ #undef DEF_EVENT
+} PyFunction_WatchEvent;
+
+/*
+ * A callback that is invoked for different events in a function's lifecycle.
+ *
+ * The callback is invoked with a borrowed reference to func, after it is
+ * created and before it is modified or destroyed. The callback should not
+ * modify func.
+ *
+ * When a function's code object, defaults, or kwdefaults are modified the
+ * callback will be invoked with the respective event and new_value will
+ * contain a borrowed reference to the new value that is about to be stored in
+ * the function. Otherwise the third argument is NULL.
+ *
+ * If the callback returns with an exception set, it must return -1. Otherwise
+ * it should return 0.
+ */
+typedef int (*PyFunction_WatchCallback)(
+ PyFunction_WatchEvent event,
+ PyFunctionObject *func,
+ PyObject *new_value);
+
+/*
+ * Register a per-interpreter callback that will be invoked for function lifecycle
+ * events.
+ *
+ * Returns a handle that may be passed to PyFunction_ClearWatcher on success,
+ * or -1 and sets an error if no more handles are available.
+ */
+PyAPI_FUNC(int) PyFunction_AddWatcher(PyFunction_WatchCallback callback);
+
+/*
+ * Clear the watcher associated with the watcher_id handle.
+ *
+ * Returns 0 on success or -1 if no watcher exists for the supplied id.
+ */
+PyAPI_FUNC(int) PyFunction_ClearWatcher(int watcher_id);
+
#ifdef __cplusplus
}
#endif
diff --git a/Include/internal/pycore_function.h b/Include/internal/pycore_function.h
index c95190f521..5cedb33d7e 100644
--- a/Include/internal/pycore_function.h
+++ b/Include/internal/pycore_function.h
@@ -8,6 +8,8 @@ extern "C" {
# error "this header requires Py_BUILD_CORE define"
#endif
+#define FUNC_MAX_WATCHERS 8
+
struct _py_func_runtime_state {
uint32_t next_version;
};
diff --git a/Include/internal/pycore_interp.h b/Include/internal/pycore_interp.h
index 437bf74b77..532b284990 100644
--- a/Include/internal/pycore_interp.h
+++ b/Include/internal/pycore_interp.h
@@ -17,6 +17,7 @@ extern "C" {
#include "pycore_dict_state.h" // struct _Py_dict_state
#include "pycore_exceptions.h" // struct _Py_exc_state
#include "pycore_floatobject.h" // struct _Py_float_state
+#include "pycore_function.h" // FUNC_MAX_WATCHERS
#include "pycore_genobject.h" // struct _Py_async_gen_state
#include "pycore_gc.h" // struct _gc_runtime_state
#include "pycore_list.h" // struct _Py_list_state
@@ -171,6 +172,11 @@ struct _is {
// Initialized to _PyEval_EvalFrameDefault().
_PyFrameEvalFunction eval_frame;
+ PyDict_WatchCallback dict_watchers[DICT_MAX_WATCHERS];
+ PyFunction_WatchCallback func_watchers[FUNC_MAX_WATCHERS];
+ // One bit is set for each non-NULL entry in func_watchers
+ uint8_t active_func_watchers;
+
Py_ssize_t co_extra_user_count;
freefunc co_extra_freefuncs[MAX_CO_EXTRA_USERS];