Coverage Report

Created: 2026-05-30 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Python/clinic/context.c.h
Line
Count
Source
1
/*[clinic input]
2
preserve
3
[clinic start generated code]*/
4
5
#include "pycore_modsupport.h"    // _PyArg_CheckPositional()
6
7
PyDoc_STRVAR(_contextvars_Context_get__doc__,
8
"get($self, key, default=None, /)\n"
9
"--\n"
10
"\n"
11
"Return the value for `key` if `key` has the value in the context object.\n"
12
"\n"
13
"If `key` does not exist, return `default`.  If `default` is not\n"
14
"given, return None.");
15
16
#define _CONTEXTVARS_CONTEXT_GET_METHODDEF    \
17
    {"get", _PyCFunction_CAST(_contextvars_Context_get), METH_FASTCALL, _contextvars_Context_get__doc__},
18
19
static PyObject *
20
_contextvars_Context_get_impl(PyContext *self, PyObject *key,
21
                              PyObject *default_value);
22
23
static PyObject *
24
_contextvars_Context_get(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
25
0
{
26
0
    PyObject *return_value = NULL;
27
0
    PyObject *key;
28
0
    PyObject *default_value = Py_None;
29
30
0
    if (!_PyArg_CheckPositional("get", nargs, 1, 2)) {
31
0
        goto exit;
32
0
    }
33
0
    key = args[0];
34
0
    if (nargs < 2) {
35
0
        goto skip_optional;
36
0
    }
37
0
    default_value = args[1];
38
0
skip_optional:
39
0
    return_value = _contextvars_Context_get_impl((PyContext *)self, key, default_value);
40
41
0
exit:
42
0
    return return_value;
43
0
}
44
45
PyDoc_STRVAR(_contextvars_Context_items__doc__,
46
"items($self, /)\n"
47
"--\n"
48
"\n"
49
"Return all variables and their values in the context object.\n"
50
"\n"
51
"The result is returned as a list of 2-tuples (variable, value).");
52
53
#define _CONTEXTVARS_CONTEXT_ITEMS_METHODDEF    \
54
    {"items", (PyCFunction)_contextvars_Context_items, METH_NOARGS, _contextvars_Context_items__doc__},
55
56
static PyObject *
57
_contextvars_Context_items_impl(PyContext *self);
58
59
static PyObject *
60
_contextvars_Context_items(PyObject *self, PyObject *Py_UNUSED(ignored))
61
0
{
62
0
    return _contextvars_Context_items_impl((PyContext *)self);
63
0
}
64
65
PyDoc_STRVAR(_contextvars_Context_keys__doc__,
66
"keys($self, /)\n"
67
"--\n"
68
"\n"
69
"Return a list of all variables in the context object.");
70
71
#define _CONTEXTVARS_CONTEXT_KEYS_METHODDEF    \
72
    {"keys", (PyCFunction)_contextvars_Context_keys, METH_NOARGS, _contextvars_Context_keys__doc__},
73
74
static PyObject *
75
_contextvars_Context_keys_impl(PyContext *self);
76
77
static PyObject *
78
_contextvars_Context_keys(PyObject *self, PyObject *Py_UNUSED(ignored))
79
0
{
80
0
    return _contextvars_Context_keys_impl((PyContext *)self);
81
0
}
82
83
PyDoc_STRVAR(_contextvars_Context_values__doc__,
84
"values($self, /)\n"
85
"--\n"
86
"\n"
87
"Return a list of all variables\' values in the context object.");
88
89
#define _CONTEXTVARS_CONTEXT_VALUES_METHODDEF    \
90
    {"values", (PyCFunction)_contextvars_Context_values, METH_NOARGS, _contextvars_Context_values__doc__},
91
92
static PyObject *
93
_contextvars_Context_values_impl(PyContext *self);
94
95
static PyObject *
96
_contextvars_Context_values(PyObject *self, PyObject *Py_UNUSED(ignored))
97
0
{
98
0
    return _contextvars_Context_values_impl((PyContext *)self);
99
0
}
100
101
PyDoc_STRVAR(_contextvars_Context_copy__doc__,
102
"copy($self, /)\n"
103
"--\n"
104
"\n"
105
"Return a shallow copy of the context object.");
106
107
#define _CONTEXTVARS_CONTEXT_COPY_METHODDEF    \
108
    {"copy", (PyCFunction)_contextvars_Context_copy, METH_NOARGS, _contextvars_Context_copy__doc__},
109
110
static PyObject *
111
_contextvars_Context_copy_impl(PyContext *self);
112
113
static PyObject *
114
_contextvars_Context_copy(PyObject *self, PyObject *Py_UNUSED(ignored))
115
0
{
116
0
    return _contextvars_Context_copy_impl((PyContext *)self);
117
0
}
118
119
PyDoc_STRVAR(_contextvars_ContextVar_get__doc__,
120
"get($self, default=<unrepresentable>, /)\n"
121
"--\n"
122
"\n"
123
"Return a value for the context variable for the current context.\n"
124
"\n"
125
"If there is no value for the variable in the current context, the\n"
126
"method will:\n"
127
" * return the value of the default argument of the method, if\n"
128
"   provided; or\n"
129
" * return the default value for the context variable, if it was\n"
130
"   created with one; or\n"
131
" * raise a LookupError.");
132
133
#define _CONTEXTVARS_CONTEXTVAR_GET_METHODDEF    \
134
    {"get", _PyCFunction_CAST(_contextvars_ContextVar_get), METH_FASTCALL, _contextvars_ContextVar_get__doc__},
135
136
static PyObject *
137
_contextvars_ContextVar_get_impl(PyContextVar *self, PyObject *default_value);
138
139
static PyObject *
140
_contextvars_ContextVar_get(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
141
0
{
142
0
    PyObject *return_value = NULL;
143
0
    PyObject *default_value = NULL;
144
145
0
    if (!_PyArg_CheckPositional("get", nargs, 0, 1)) {
146
0
        goto exit;
147
0
    }
148
0
    if (nargs < 1) {
149
0
        goto skip_optional;
150
0
    }
151
0
    default_value = args[0];
152
0
skip_optional:
153
0
    return_value = _contextvars_ContextVar_get_impl((PyContextVar *)self, default_value);
154
155
0
exit:
156
0
    return return_value;
157
0
}
158
159
PyDoc_STRVAR(_contextvars_ContextVar_set__doc__,
160
"set($self, value, /)\n"
161
"--\n"
162
"\n"
163
"Call to set a new value for the context variable in the current context.\n"
164
"\n"
165
"The required value argument is the new value for the context\n"
166
"variable.\n"
167
"\n"
168
"Returns a Token object that can be used to restore the variable to\n"
169
"its previous value via the `ContextVar.reset()` method.");
170
171
#define _CONTEXTVARS_CONTEXTVAR_SET_METHODDEF    \
172
    {"set", (PyCFunction)_contextvars_ContextVar_set, METH_O, _contextvars_ContextVar_set__doc__},
173
174
static PyObject *
175
_contextvars_ContextVar_set_impl(PyContextVar *self, PyObject *value);
176
177
static PyObject *
178
_contextvars_ContextVar_set(PyObject *self, PyObject *value)
179
0
{
180
0
    PyObject *return_value = NULL;
181
182
0
    return_value = _contextvars_ContextVar_set_impl((PyContextVar *)self, value);
183
184
0
    return return_value;
185
0
}
186
187
PyDoc_STRVAR(_contextvars_ContextVar_reset__doc__,
188
"reset($self, token, /)\n"
189
"--\n"
190
"\n"
191
"Reset the context variable.\n"
192
"\n"
193
"The variable is reset to the value it had before the\n"
194
"`ContextVar.set()` that created the token was used.");
195
196
#define _CONTEXTVARS_CONTEXTVAR_RESET_METHODDEF    \
197
    {"reset", (PyCFunction)_contextvars_ContextVar_reset, METH_O, _contextvars_ContextVar_reset__doc__},
198
199
static PyObject *
200
_contextvars_ContextVar_reset_impl(PyContextVar *self, PyObject *token);
201
202
static PyObject *
203
_contextvars_ContextVar_reset(PyObject *self, PyObject *token)
204
0
{
205
0
    PyObject *return_value = NULL;
206
207
0
    return_value = _contextvars_ContextVar_reset_impl((PyContextVar *)self, token);
208
209
0
    return return_value;
210
0
}
211
212
PyDoc_STRVAR(token_enter__doc__,
213
"__enter__($self, /)\n"
214
"--\n"
215
"\n"
216
"Enter into Token context manager.");
217
218
#define TOKEN_ENTER_METHODDEF    \
219
    {"__enter__", (PyCFunction)token_enter, METH_NOARGS, token_enter__doc__},
220
221
static PyObject *
222
token_enter_impl(PyContextToken *self);
223
224
static PyObject *
225
token_enter(PyObject *self, PyObject *Py_UNUSED(ignored))
226
0
{
227
0
    return token_enter_impl((PyContextToken *)self);
228
0
}
229
230
PyDoc_STRVAR(token_exit__doc__,
231
"__exit__($self, type, val, tb, /)\n"
232
"--\n"
233
"\n"
234
"Exit from Token context manager, restore the linked ContextVar.");
235
236
#define TOKEN_EXIT_METHODDEF    \
237
    {"__exit__", _PyCFunction_CAST(token_exit), METH_FASTCALL, token_exit__doc__},
238
239
static PyObject *
240
token_exit_impl(PyContextToken *self, PyObject *type, PyObject *val,
241
                PyObject *tb);
242
243
static PyObject *
244
token_exit(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
245
0
{
246
0
    PyObject *return_value = NULL;
247
0
    PyObject *type;
248
0
    PyObject *val;
249
0
    PyObject *tb;
250
251
0
    if (!_PyArg_CheckPositional("__exit__", nargs, 3, 3)) {
252
0
        goto exit;
253
0
    }
254
0
    type = args[0];
255
0
    val = args[1];
256
0
    tb = args[2];
257
0
    return_value = token_exit_impl((PyContextToken *)self, type, val, tb);
258
259
0
exit:
260
0
    return return_value;
261
0
}
262
/*[clinic end generated code: output=90ec3e4375804e9b input=a9049054013a1b77]*/