File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -157,6 +157,14 @@ Context variable functions:
157157 a default value for the context variable, or ``NULL `` for no default.
158158 If an error has occurred, this function returns ``NULL ``.
159159
160+ .. c :function :: PyObject *PyContextVar_NewThreadInheritable (const char *name, PyObject *def)
161+
162+ Create a new ``ContextVar `` object whose bindings are inherited by new
163+ :class: `threading.Thread ` instances. The parameters and return value are
164+ the same as for :c:func: `PyContextVar_New `.
165+
166+ .. versionadded :: 3.16
167+
160168.. c :function :: int PyContextVar_Get (PyObject *var, PyObject *default_value, PyObject **value)
161169
162170 Get the value of a context variable. Returns ``-1 `` if an error has
Original file line number Diff line number Diff line change @@ -68,6 +68,13 @@ PyAPI_FUNC(int) PyContext_ClearWatcher(int watcher_id);
6868PyAPI_FUNC (PyObject * ) PyContextVar_New (
6969 const char * name , PyObject * default_value );
7070
71+ /* Create a new thread-inheritable context variable.
72+
73+ default_value can be NULL.
74+ */
75+ PyAPI_FUNC (PyObject * ) PyContextVar_NewThreadInheritable (
76+ const char * name , PyObject * default_value );
77+
7178
7279/* Get a value for the variable.
7380
Original file line number Diff line number Diff line change @@ -281,18 +281,31 @@ PyContext_Exit(PyObject *octx)
281281}
282282
283283
284- PyObject *
285- PyContextVar_New (const char * name , PyObject * def )
284+ static PyObject *
285+ contextvar_new_from_utf8 (const char * name , PyObject * def ,
286+ int thread_inheritable )
286287{
287288 PyObject * pyname = PyUnicode_FromString (name );
288289 if (pyname == NULL ) {
289290 return NULL ;
290291 }
291- PyContextVar * var = contextvar_new (pyname , def , 0 );
292+ PyContextVar * var = contextvar_new (pyname , def , thread_inheritable );
292293 Py_DECREF (pyname );
293294 return (PyObject * )var ;
294295}
295296
297+ PyObject *
298+ PyContextVar_New (const char * name , PyObject * def )
299+ {
300+ return contextvar_new_from_utf8 (name , def , 0 );
301+ }
302+
303+ PyObject *
304+ PyContextVar_NewThreadInheritable (const char * name , PyObject * def )
305+ {
306+ return contextvar_new_from_utf8 (name , def , 1 );
307+ }
308+
296309
297310int
298311PyContextVar_Get (PyObject * ovar , PyObject * def , PyObject * * val )
You can’t perform that action at this time.
0 commit comments