情境變數物件¶
在 3.7 版被加入.
在 3.7.1 版的變更:
備註
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);
更多細節請見bpo-34762。
This section details the public C API for thecontextvars
module.
- typePyContext¶
The C structure used to represent a
contextvars.Context
object.
- typePyContextVar¶
The C structure used to represent a
contextvars.ContextVar
object.
- typePyContextToken¶
The C structure used to represent a
contextvars.Token
object.
- 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)¶
- 回傳值:新的參照。
Create a new empty context object. Returns
NULL
if an errorhas occurred.
- PyObject*PyContext_Copy(PyObject*ctx)¶
- 回傳值:新的參照。
Create a shallow copy of the passedctx context object.Returns
NULL
if an error has occurred.
- PyObject*PyContext_CopyCurrent(void)¶
- 回傳值:新的參照。
Create a shallow copy of the current thread context.Returns
NULL
if an error has occurred.
- intPyContext_Enter(PyObject*ctx)¶
Setctx as the current context for the current thread.Returns
0
on success, and-1
on error.
- intPyContext_Exit(PyObject*ctx)¶
Deactivate thectx context and restore the previous context as thecurrent context for the current thread. Returns
0
on success,and-1
on error.
Context variable functions:
- PyObject*PyContextVar_New(constchar*name,PyObject*def)¶
- 回傳值:新的參照。
Create a new
ContextVar
object. Thename parameter is usedfor introspection and debug purposes. Thedef parameter specifiesa default value for the context variable, orNULL
for 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
-1
if an error hasoccurred during lookup, and0
if 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)¶
- 回傳值:新的參照。
Set the value ofvar tovalue in the current context. Returnsa new token object for this change, or
NULL
if 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 returns0
on success and-1
on error.