blob: 4c33ba4f994471c186159e286b78e6b6eafc1352 [file] [log] [blame]
Elvis Pranskevichusb2f5f592018-05-22 13:31:56 -04001.. highlightlang:: c
2
3.. _contextvarsobjects:
4
5Context Variables Objects
6-------------------------
7
8.. versionadded:: 3.7
9
10This section details the public C API for the :mod:`contextvars` module.
11
12.. c:type:: PyContext
13
14 The C structure used to represent a :class:`contextvars.Context`
15 object.
16
17.. c:type:: PyContextVar
18
19 The C structure used to represent a :class:`contextvars.ContextVar`
20 object.
21
22.. c:type:: PyContextToken
23
24 The C structure used to represent a :class:`contextvars.Token` object.
25
26.. c:var:: PyTypeObject PyContext_Type
27
28 The type object representing the *context* type.
29
30.. c:var:: PyTypeObject PyContextVar_Type
31
32 The type object representing the *context variable* type.
33
34.. c:var:: PyTypeObject PyContextToken_Type
35
36 The type object representing the *context variable token* type.
37
38
39Type-check macros:
40
41.. c:function:: int PyContext_CheckExact(PyObject *o)
42
43 Return true if *o* is of type :c:data:`PyContext_Type`. *o* must not be
44 *NULL*. This function always succeeds.
45
46.. c:function:: int PyContextVar_CheckExact(PyObject *o)
47
48 Return true if *o* is of type :c:data:`PyContextVar_Type`. *o* must not be
49 *NULL*. This function always succeeds.
50
51.. c:function:: int PyContextToken_CheckExact(PyObject *o)
52
53 Return true if *o* is of type :c:data:`PyContextToken_Type`.
54 *o* must not be *NULL*. This function always succeeds.
55
56
57Context object management functions:
58
59.. c:function:: PyContext *PyContext_New(void)
60
61 Create a new empty context object. Returns ``NULL`` if an error
62 has occurred.
63
64.. c:function:: PyContext *PyContext_Copy(PyContext *ctx)
65
66 Create a shallow copy of the passed *ctx* context object.
67 Returns ``NULL`` if an error has occurred.
68
69.. c:function:: PyContext *PyContext_CopyCurrent(void)
70
71 Create a shallow copy of the current thread context.
72 Returns ``NULL`` if an error has occurred.
73
74.. c:function:: int PyContext_Enter(PyContext *ctx)
75
76 Set *ctx* as the current context for the current thread.
77 Returns ``0`` on success, and ``-1`` on error.
78
79.. c:function:: int PyContext_Exit(PyContext *ctx)
80
81 Deactivate the *ctx* context and restore the previous context as the
82 current context for the current thread. Returns ``0`` on success,
83 and ``-1`` on error.
84
85.. c:function:: int PyContext_ClearFreeList()
86
87 Clear the context variable free list. Return the total number of
88 freed items. This function always succeeds.
89
90
91Context variable functions:
92
93.. c:function:: PyContextVar *PyContextVar_New(const char *name, PyObject *def)
94
95 Create a new ``ContextVar`` object. The *name* parameter is used
96 for introspection and debug purposes. The *def* parameter may optionally
97 specify the default value for the context variable. If an error has
98 occurred, this function returns ``NULL``.
99
100.. c:function:: int PyContextVar_Get(PyContextVar *var, PyObject *default_value, PyObject **value)
101
102 Get the value of a context variable. Returns ``-1`` if an error has
103 occurred during lookup, and ``0`` if no error occurred, whether or not
104 a value was found.
105
106 If the context variable was found, *value* will be a pointer to it.
107 If the context variable was *not* found, *value* will point to:
108
109 - *default_value*, if not ``NULL``;
110 - the default value of *var*, if not ``NULL``;
111 - ``NULL``
112
113 If the value was found, the function will create a new reference to it.
114
115.. c:function:: PyContextToken *PyContextVar_Set(PyContextVar *var, PyObject *value)
116
117 Set the value of *var* to *value* in the current context. Returns a
118 pointer to a :c:type:`PyContextToken` object, or ``NULL`` if an error
119 has occurred.
120
121.. c:function:: int PyContextVar_Reset(PyContextVar *var, PyContextToken *token)
122
123 Reset the state of the *var* context variable to that it was in before
124 :c:func:`PyContextVar_Set` that returned the *token* was called.
125 This function returns ``0`` on success and ``-1`` on error.