Context Variables Objects¶
Added in version 3.7.
Changed in version 3.7.1:
Note
In Python 3.7.1 the signatures of all context variablesC APIs werechanged to usePyObject pointers insteadofPyContext,PyContextVar, andPyContextToken, e.g.:
// in 3.7.0:PyContext*PyContext_New(void);// in 3.7.1+:PyObject*PyContext_New(void);
Seebpo-34762 for more details.
This section details the public C API for thecontextvars module.
- typePyContext¶
The C structure used to represent a
contextvars.Contextobject.
- typePyContextVar¶
The C structure used to represent a
contextvars.ContextVarobject.
- typePyContextToken¶
The C structure used to represent a
contextvars.Tokenobject.
- PyTypeObjectPyContext_Type¶
The type object representing thecontext type.
- PyTypeObjectPyContextVar_Type¶
The type object representing thecontext variable type.
- PyTypeObjectPyContextToken_Type¶
The type object representing thecontext variable token type.
Type-check macros:
- intPyContext_CheckExact(PyObject*o)¶
Return true ifo is of type
PyContext_Type.o must not beNULL. This function always succeeds.
- intPyContextVar_CheckExact(PyObject*o)¶
Return true ifo is of type
PyContextVar_Type.o must not beNULL. This function always succeeds.
- intPyContextToken_CheckExact(PyObject*o)¶
Return true ifo is of type
PyContextToken_Type.o must not beNULL. This function always succeeds.
Context object management functions:
- PyObject*PyContext_New(void)¶
- Return value: New reference.
Create a new empty context object. Returns
NULLif an errorhas occurred.
- PyObject*PyContext_Copy(PyObject*ctx)¶
- Return value: New reference.
Create a shallow copy of the passedctx context object.Returns
NULLif an error has occurred.
- PyObject*PyContext_CopyCurrent(void)¶
- Return value: New reference.
Create a shallow copy of the current thread context.Returns
NULLif an error has occurred.
- intPyContext_Enter(PyObject*ctx)¶
Setctx as the current context for the current thread.Returns
0on success, and-1on error.
- intPyContext_Exit(PyObject*ctx)¶
Deactivate thectx context and restore the previous context as thecurrent context for the current thread. Returns
0on success,and-1on error.
- intPyContext_AddWatcher(PyContext_WatchCallbackcallback)¶
Registercallback as a context object watcher for the current interpreter.Return an ID which may be passed to
PyContext_ClearWatcher().In case of error (e.g. no more watcher IDs available),return-1and set an exception.Added in version 3.14.
- intPyContext_ClearWatcher(intwatcher_id)¶
Clear watcher identified bywatcher_id previously returned from
PyContext_AddWatcher()for the current interpreter.Return0on success, or-1and set an exception on error(e.g. if the givenwatcher_id was never registered.)Added in version 3.14.
- typePyContextEvent¶
Enumeration of possible context object watcher events:
Py_CONTEXT_SWITCHED: Thecurrent context has switched to adifferent context. The object passed to the watch callback is thenow-currentcontextvars.Contextobject, or None if no context iscurrent.
Added in version 3.14.
- typedefint(*PyContext_WatchCallback)(PyContextEventevent,PyObject*obj)¶
Context object watcher callback function. The object passed to the callbackis event-specific; see
PyContextEventfor details.If the callback returns with an exception set, it must return
-1; thisexception will be printed as an unraisable exception usingPyErr_FormatUnraisable(). Otherwise it should return0.There may already be a pending exception set on entry to the callback. Inthis case, the callback should return
0with the same exception stillset. This means the callback may not call any other API that can set anexception unless it saves and clears the exception state first, and restoresit before returning.Added in version 3.14.
Context variable functions:
- PyObject*PyContextVar_New(constchar*name,PyObject*def)¶
- Return value: New reference.
Create a new
ContextVarobject. Thename parameter is usedfor introspection and debug purposes. Thedef parameter specifiesa default value for the context variable, orNULLfor no default.If an error has occurred, this function returnsNULL.
- intPyContextVar_Get(PyObject*var,PyObject*default_value,PyObject**value)¶
Get the value of a context variable. Returns
-1if an error hasoccurred during lookup, and0if no error occurred, whether or nota value was found.If the context variable was found,value will be a pointer to it.If the context variable wasnot found,value will point to:
default_value, if not
NULL;the default value ofvar, if not
NULL;NULL
Except for
NULL, the function returns a new reference.
- PyObject*PyContextVar_Set(PyObject*var,PyObject*value)¶
- Return value: New reference.
Set the value ofvar tovalue in the current context. Returnsa new token object for this change, or
NULLif an error has occurred.
- intPyContextVar_Reset(PyObject*var,PyObject*token)¶
Reset the state of thevar context variable to that it was in before
PyContextVar_Set()that returned thetoken was called.This function returns0on success and-1on error.