gh-119333: Add C api to have contextvar enter/exit callbacks (#119335) · python/cpython@d87482b · GitHub
Skip to content

Commit d87482b

Browse files
gh-119333: Add C api to have contextvar enter/exit callbacks (#119335)
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
1 parent ad7c778 commit d87482b

10 files changed

Lines changed: 402 additions & 0 deletions

File tree

Doc/c-api/contextvars.rst

Lines changed: 46 additions & 0 deletions

Include/cpython/context.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,38 @@ PyAPI_FUNC(PyObject *) PyContext_CopyCurrent(void);
2727
PyAPI_FUNC(int) PyContext_Enter(PyObject *);
2828
PyAPI_FUNC(int) PyContext_Exit(PyObject *);
2929

30+
typedef enum {
31+
Py_CONTEXT_EVENT_ENTER,
32+
Py_CONTEXT_EVENT_EXIT,
33+
} PyContextEvent;
34+
35+
/*
36+
* A Callback to clue in non-python contexts impls about a
37+
* change in the active python context.
38+
*
39+
* The callback is invoked with the event and a reference to =
40+
* the context after its entered and before its exited.
41+
*
42+
* if the callback returns with an exception set, it must return -1. Otherwise
43+
* it should return 0
44+
*/
45+
typedef int (*PyContext_WatchCallback)(PyContextEvent, PyContext *);
46+
47+
/*
48+
* Register a per-interpreter callback that will be invoked for context object
49+
* enter/exit events.
50+
*
51+
* Returns a handle that may be passed to PyContext_ClearWatcher on success,
52+
* or -1 and sets and error if no more handles are available.
53+
*/
54+
PyAPI_FUNC(int) PyContext_AddWatcher(PyContext_WatchCallback callback);
55+
56+
/*
57+
* Clear the watcher associated with the watcher_id handle.
58+
*
59+
* Returns 0 on success or -1 if no watcher exists for the provided id.
60+
*/
61+
PyAPI_FUNC(int) PyContext_ClearWatcher(int watcher_id);
3062

3163
/* Create a new context variable.
3264

Include/internal/pycore_context.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "pycore_hamt.h" // PyHamtObject
99

10+
#define CONTEXT_MAX_WATCHERS 8
1011

1112
extern PyTypeObject _PyContextTokenMissing_Type;
1213

Include/internal/pycore_interp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,10 @@ struct _is {
240240
PyObject *audit_hooks;
241241
PyType_WatchCallback type_watchers[TYPE_MAX_WATCHERS];
242242
PyCode_WatchCallback code_watchers[CODE_MAX_WATCHERS];
243+
PyContext_WatchCallback context_watchers[CONTEXT_MAX_WATCHERS];
243244
// One bit is set for each non-NULL entry in code_watchers
244245
uint8_t active_code_watchers;
246+
uint8_t active_context_watchers;
245247

246248
struct _py_object_state object_state;
247249
struct _Py_unicode_state unicode;

Lib/test/test_capi/test_watchers.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
import contextvars
23

34
from contextlib import contextmanager, ExitStack
45
from test.support import (
@@ -571,5 +572,87 @@ def test_allocate_too_many_watchers(self):
571572
_testcapi.allocate_too_many_func_watchers()
572573

573574

575+
class TestContextObjectWatchers(unittest.TestCase):
576+
@contextmanager
577+
def context_watcher(self, which_watcher):
578+
wid = _testcapi.add_context_watcher(which_watcher)
579+
try:
580+
yield wid
581+
finally:
582+
_testcapi.clear_context_watcher(wid)
583+
584+
def assert_event_counts(self, exp_enter_0, exp_exit_0,
585+
exp_enter_1, exp_exit_1):
586+
self.assertEqual(
587+
exp_enter_0, _testcapi.get_context_watcher_num_enter_events(0))
588+
self.assertEqual(
589+
exp_exit_0, _testcapi.get_context_watcher_num_exit_events(0))
590+
self.assertEqual(
591+
exp_enter_1, _testcapi.get_context_watcher_num_enter_events(1))
592+
self.assertEqual(
593+
exp_exit_1, _testcapi.get_context_watcher_num_exit_events(1))
594+
595+
def test_context_object_events_dispatched(self):
596+
# verify that all counts are zero before any watchers are registered
597+
self.assert_event_counts(0, 0, 0, 0)
598+
599+
# verify that all counts remain zero when a context object is
600+
# entered and exited with no watchers registered
601+
ctx = contextvars.copy_context()
602+
ctx.run(self.assert_event_counts, 0, 0, 0, 0)
603+
self.assert_event_counts(0, 0, 0, 0)
604+
605+
# verify counts are as expected when first watcher is registered
606+
with self.context_watcher(0):
607+
self.assert_event_counts(0, 0, 0, 0)
608+
ctx.run(self.assert_event_counts, 1, 0, 0, 0)
609+
self.assert_event_counts(1, 1, 0, 0)
610+
611+
# again with second watcher registered
612+
with self.context_watcher(1):
613+
self.assert_event_counts(1, 1, 0, 0)
614+
ctx.run(self.assert_event_counts, 2, 1, 1, 0)
615+
self.assert_event_counts(2, 2, 1, 1)
616+
617+
# verify counts are reset and don't change after both watchers are cleared
618+
ctx.run(self.assert_event_counts, 0, 0, 0, 0)
619+
self.assert_event_counts(0, 0, 0, 0)
620+
621+
def test_enter_error(self):
622+
with self.context_watcher(2):
623+
with catch_unraisable_exception() as cm:
624+
ctx = contextvars.copy_context()
625+
ctx.run(int, 0)
626+
self.assertEqual(
627+
cm.unraisable.err_msg,
628+
"Exception ignored in "
629+
f"Py_CONTEXT_EVENT_EXIT watcher callback for {ctx!r}"
630+
)
631+
self.assertEqual(str(cm.unraisable.exc_value), "boom!")
632+
633+
def test_exit_error(self):
634+
ctx = contextvars.copy_context()
635+
def _in_context(stack):
636+
stack.enter_context(self.context_watcher(2))
637+
638+
with catch_unraisable_exception() as cm:
639+
with ExitStack() as stack:
640+
ctx.run(_in_context, stack)
641+
self.assertEqual(str(cm.unraisable.exc_value), "boom!")
642+
643+
def test_clear_out_of_range_watcher_id(self):
644+
with self.assertRaisesRegex(ValueError, r"Invalid context watcher ID -1"):
645+
_testcapi.clear_context_watcher(-1)
646+
with self.assertRaisesRegex(ValueError, r"Invalid context watcher ID 8"):
647+
_testcapi.clear_context_watcher(8) # CONTEXT_MAX_WATCHERS = 8
648+
649+
def test_clear_unassigned_watcher_id(self):
650+
with self.assertRaisesRegex(ValueError, r"No context watcher set for ID 1"):
651+
_testcapi.clear_context_watcher(1)
652+
653+
def test_allocate_too_many_watchers(self):
654+
with self.assertRaisesRegex(RuntimeError, r"no more context watcher IDs available"):
655+
_testcapi.allocate_too_many_context_watchers()
656+
574657
if __name__ == "__main__":
575658
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add :c:func:`PyContext_AddWatcher` and :c:func:`PyContext_ClearWatcher` APIs to
2+
register callbacks to receive notification on enter and exit of context objects.

Modules/_testcapi/watchers.c

Lines changed: 152 additions & 0 deletions

0 commit comments

Comments
 (0)