Skip to content

Commit 3f0bb41

Browse files
Document PyUnstable_SetEvalCallback API
1 parent da4179e commit 3f0bb41

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

Doc/c-api/init.rst

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2074,6 +2074,77 @@ Python-level trace functions in previous versions.
20742074
20752075
.. versionadded:: 3.12
20762076
2077+
2078+
Trace/Profile callback notifications
2079+
------------------------------------
2080+
2081+
.. versionadded:: 3.15
2082+
2083+
.. c:type:: PyUnstable_EvalEvent
2084+
2085+
An enumeration of events that can trigger a callback registered via
2086+
:c:func:`PyUnstable_SetEvalCallback`. The possible values are:
2087+
2088+
.. c:macro:: PyUnstable_EVAL_TRACE_SET
2089+
2090+
A trace function was set via :func:`sys.settrace`, :c:func:`PyEval_SetTrace`,
2091+
or :c:func:`PyEval_SetTraceAllThreads`.
2092+
2093+
.. c:macro:: PyUnstable_EVAL_TRACE_CLEAR
2094+
2095+
The trace function was cleared.
2096+
2097+
.. c:macro:: PyUnstable_EVAL_PROFILE_SET
2098+
2099+
A profile function was set via :func:`sys.setprofile`, :c:func:`PyEval_SetProfile`,
2100+
or :c:func:`PyEval_SetProfileAllThreads`.
2101+
2102+
.. c:macro:: PyUnstable_EVAL_PROFILE_CLEAR
2103+
2104+
The profile function was cleared.
2105+
2106+
2107+
.. c:type:: int (*PyUnstable_EvalCallback)(PyUnstable_EvalEvent event, void *data)
2108+
2109+
The type of the callback function registered using
2110+
:c:func:`PyUnstable_SetEvalCallback`. The *event* parameter indicates
2111+
which tracing or profiling event occurred. The *data* parameter is the opaque
2112+
pointer that was provided when :c:func:`PyUnstable_SetEvalCallback` was called.
2113+
2114+
If the callback returns a negative value, the exception is logged using
2115+
:c:func:`PyErr_FormatUnraisable`.
2116+
2117+
2118+
.. c:function:: int PyUnstable_SetEvalCallback(PyUnstable_EvalCallback callback, void *data)
2119+
2120+
Register a callback to be notified when :func:`sys.settrace` or
2121+
:func:`sys.setprofile` (or their C equivalents) are called.
2122+
2123+
This allows JIT compilers and other tools using :pep:`523` frame evaluation
2124+
hooks to efficiently detect tracing or profiling changes without polling.
2125+
2126+
The *callback* will be invoked with an event indicating whether tracing or
2127+
profiling was set or cleared. The *data* pointer is passed through to the
2128+
callback.
2129+
2130+
Only one callback can be registered at a time per interpreter. Setting a new
2131+
callback replaces any previously registered callback. To clear the callback,
2132+
pass ``NULL`` for *callback*.
2133+
2134+
Return ``0`` on success.
2135+
2136+
2137+
.. c:function:: PyUnstable_EvalCallback PyUnstable_GetEvalCallback(void **data)
2138+
2139+
Retrieve the currently registered eval callback and its associated data.
2140+
2141+
If *data* is not ``NULL``, the opaque pointer that was passed to
2142+
:c:func:`PyUnstable_SetEvalCallback` is stored in ``*data``.
2143+
2144+
Return the currently registered callback, or ``NULL`` if no callback
2145+
is registered.
2146+
2147+
20772148
Reference tracing
20782149
=================
20792150

0 commit comments

Comments
 (0)