Coverage Report

Created: 2026-12-01 06:09

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
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
6
#  include "pycore_gc.h"          // PyGC_Head
7
#  include "pycore_runtime.h"     // _Py_ID()
8
#endif
9
#include "pycore_modsupport.h"    // _PyArg_CheckPositional()
10
11
PyDoc_STRVAR(_contextvars_Context_get__doc__,
12
"get($self, key, default=None, /)\n"
13
"--\n"
14
"\n"
15
"Return the value for `key` if `key` has the value in the context object.\n"
16
"\n"
17
"If `key` does not exist, return `default`.  If `default` is not\n"
18
"given, return None.");
19
20
#define _CONTEXTVARS_CONTEXT_GET_METHODDEF    \
21
    {"get", _PyCFunction_CAST(_contextvars_Context_get), METH_FASTCALL, _contextvars_Context_get__doc__},
22
23
static PyObject *
24
_contextvars_Context_get_impl(PyContext *self, PyObject *key,
25
                              PyObject *default_value);
26
27
static PyObject *
28
_contextvars_Context_get(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
29
0
{
30
0
    PyObject *return_value = NULL;
31
0
    PyObject *key;
32
0
    PyObject *default_value = Py_None;
33
34
0
    if (!_PyArg_CheckPositional("get", nargs, 1, 2)) {
35
0
        goto exit;
36
0
    }
37
0
    key = args[0];
38
0
    if (nargs < 2) {
39
0
        goto skip_optional;
40
0
    }
41
0
    default_value = args[1];
42
0
skip_optional:
43
0
    return_value = _contextvars_Context_get_impl((PyContext *)self, key, default_value);
44
45
0
exit:
46
0
    return return_value;
47
0
}
48
49
PyDoc_STRVAR(_contextvars_Context_items__doc__,
50
"items($self, /)\n"
51
"--\n"
52
"\n"
53
"Return all variables and their values in the context object.\n"
54
"\n"
55
"The result is returned as a list of 2-tuples (variable, value).");
56
57
#define _CONTEXTVARS_CONTEXT_ITEMS_METHODDEF    \
58
    {"items", (PyCFunction)_contextvars_Context_items, METH_NOARGS, _contextvars_Context_items__doc__},
59
60
static PyObject *
61
_contextvars_Context_items_impl(PyContext *self);
62
63
static PyObject *
64
_contextvars_Context_items(PyObject *self, PyObject *Py_UNUSED(ignored))
65
0
{
66
0
    return _contextvars_Context_items_impl((PyContext *)self);
67
0
}
68
69
PyDoc_STRVAR(_contextvars_Context_keys__doc__,
70
"keys($self, /)\n"
71
"--\n"
72
"\n"
73
"Return a list of all variables in the context object.");
74
75
#define _CONTEXTVARS_CONTEXT_KEYS_METHODDEF    \
76
    {"keys", (PyCFunction)_contextvars_Context_keys, METH_NOARGS, _contextvars_Context_keys__doc__},
77
78
static PyObject *
79
_contextvars_Context_keys_impl(PyContext *self);
80
81
static PyObject *
82
_contextvars_Context_keys(PyObject *self, PyObject *Py_UNUSED(ignored))
83
0
{
84
0
    return _contextvars_Context_keys_impl((PyContext *)self);
85
0
}
86
87
PyDoc_STRVAR(_contextvars_Context_values__doc__,
88
"values($self, /)\n"
89
"--\n"
90
"\n"
91
"Return a list of all variables\' values in the context object.");
92
93
#define _CONTEXTVARS_CONTEXT_VALUES_METHODDEF    \
94
    {"values", (PyCFunction)_contextvars_Context_values, METH_NOARGS, _contextvars_Context_values__doc__},
95
96
static PyObject *
97
_contextvars_Context_values_impl(PyContext *self);
98
99
static PyObject *
100
_contextvars_Context_values(PyObject *self, PyObject *Py_UNUSED(ignored))
101
0
{
102
0
    return _contextvars_Context_values_impl((PyContext *)self);
103
0
}
104
105
PyDoc_STRVAR(_contextvars_Context_copy__doc__,
106
"copy($self, /)\n"
107
"--\n"
108
"\n"
109
"Return a shallow copy of the context object.");
110
111
#define _CONTEXTVARS_CONTEXT_COPY_METHODDEF    \
112
    {"copy", (PyCFunction)_contextvars_Context_copy, METH_NOARGS, _contextvars_Context_copy__doc__},
113
114
static PyObject *
115
_contextvars_Context_copy_impl(PyContext *self);
116
117
static PyObject *
118
_contextvars_Context_copy(PyObject *self, PyObject *Py_UNUSED(ignored))
119
0
{
120
0
    return _contextvars_Context_copy_impl((PyContext *)self);
121
0
}
122
123
static PyObject *
124
contextvar_tp_new_impl(PyTypeObject *type, PyObject *name, PyObject *def);
125
126
static PyObject *
127
contextvar_tp_new_helper(PyTypeObject *type, PyObject *const *args,
128
    Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames)
129
29
{
130
29
    PyObject *return_value = NULL;
131
29
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
132
133
29
    #define NUM_KEYWORDS 1
134
29
    static struct {
135
29
        PyGC_Head _this_is_not_used;
136
29
        PyObject_VAR_HEAD
137
29
        Py_hash_t ob_hash;
138
29
        PyObject *ob_item[NUM_KEYWORDS];
139
29
    } _kwtuple = {
140
29
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
141
29
        .ob_hash = -1,
142
29
        .ob_item = { &_Py_ID(default), },
143
29
    };
144
29
    #undef NUM_KEYWORDS
145
29
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
146
147
    #else  // !Py_BUILD_CORE
148
    #  define KWTUPLE NULL
149
    #endif  // !Py_BUILD_CORE
150
151
29
    static const char * const _keywords[] = {"", "default", NULL};
152
29
    static _PyArg_Parser _parser = {
153
29
        .keywords = _keywords,
154
29
        .fname = "ContextVar",
155
29
        .kwtuple = KWTUPLE,
156
29
    };
157
29
    #undef KWTUPLE
158
29
    PyObject *argsbuf[2];
159
29
    PyObject * const *fastargs;
160
29
    Py_ssize_t noptargs = nargs + nkw - 1;
161
29
    PyObject *name;
162
29
    PyObject *def = NULL;
163
164
29
    fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser,
165
29
            /*minpos*/ 1, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
166
29
    if (!fastargs) {
167
0
        goto exit;
168
0
    }
169
29
    name = fastargs[0];
170
29
    if (!noptargs) {
171
29
        goto skip_optional_kwonly;
172
29
    }
173
0
    def = fastargs[1];
174
29
skip_optional_kwonly:
175
29
    return_value = contextvar_tp_new_impl(type, name, def);
176
177
29
exit:
178
29
    return return_value;
179
29
}
180
181
static PyObject *
182
contextvar_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
183
0
{
184
0
    return contextvar_tp_new_helper(type, _PyTuple_CAST(args)->ob_item,
185
0
        PyTuple_GET_SIZE(args),
186
0
        kwargs ? PyDict_GET_SIZE(kwargs) : 0,
187
0
        kwargs, NULL);
188
0
}
189
190
static PyObject *
191
contextvar_tp_vectorcall(PyObject *type, PyObject *const *args,
192
    size_t nargsf, PyObject *kwnames)
193
29
{
194
29
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
195
196
29
    assert(_PyType_CAST(type)->tp_new == contextvar_tp_new);
197
    /* Make sure the type object is immutable: the generated
198
     * vectorcall doesn't deal e.g. with users reassigning __init__. */
199
29
    assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE));
200
29
    return contextvar_tp_new_helper(_PyType_CAST(type), args, nargs,
201
29
        kwnames ? PyTuple_GET_SIZE(kwnames) : 0,
202
29
        NULL, kwnames);
203
29
}
204
205
PyDoc_STRVAR(_contextvars_ContextVar_get__doc__,
206
"get($self, default=<unrepresentable>, /)\n"
207
"--\n"
208
"\n"
209
"Return a value for the context variable for the current context.\n"
210
"\n"
211
"If there is no value for the variable in the current context, the\n"
212
"method will:\n"
213
" * return the value of the default argument of the method, if\n"
214
"   provided; or\n"
215
" * return the default value for the context variable, if it was\n"
216
"   created with one; or\n"
217
" * raise a LookupError.");
218
219
#define _CONTEXTVARS_CONTEXTVAR_GET_METHODDEF    \
220
    {"get", _PyCFunction_CAST(_contextvars_ContextVar_get), METH_FASTCALL, _contextvars_ContextVar_get__doc__},
221
222
static PyObject *
223
_contextvars_ContextVar_get_impl(PyContextVar *self, PyObject *default_value);
224
225
static PyObject *
226
_contextvars_ContextVar_get(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
227
0
{
228
0
    PyObject *return_value = NULL;
229
0
    PyObject *default_value = NULL;
230
231
0
    if (!_PyArg_CheckPositional("get", nargs, 0, 1)) {
232
0
        goto exit;
233
0
    }
234
0
    if (nargs < 1) {
235
0
        goto skip_optional;
236
0
    }
237
0
    default_value = args[0];
238
0
skip_optional:
239
0
    return_value = _contextvars_ContextVar_get_impl((PyContextVar *)self, default_value);
240
241
0
exit:
242
0
    return return_value;
243
0
}
244
245
PyDoc_STRVAR(_contextvars_ContextVar_set__doc__,
246
"set($self, value, /)\n"
247
"--\n"
248
"\n"
249
"Call to set a new value for the context variable in the current context.\n"
250
"\n"
251
"The required value argument is the new value for the context\n"
252
"variable.\n"
253
"\n"
254
"Returns a Token object that can be used to restore the variable to\n"
255
"its previous value via the `ContextVar.reset()` method.");
256
257
#define _CONTEXTVARS_CONTEXTVAR_SET_METHODDEF    \
258
    {"set", (PyCFunction)_contextvars_ContextVar_set, METH_O, _contextvars_ContextVar_set__doc__},
259
260
static PyObject *
261
_contextvars_ContextVar_set_impl(PyContextVar *self, PyObject *value);
262
263
static PyObject *
264
_contextvars_ContextVar_set(PyObject *self, PyObject *value)
265
0
{
266
0
    PyObject *return_value = NULL;
267
268
0
    return_value = _contextvars_ContextVar_set_impl((PyContextVar *)self, value);
269
270
0
    return return_value;
271
0
}
272
273
PyDoc_STRVAR(_contextvars_ContextVar_reset__doc__,
274
"reset($self, token, /)\n"
275
"--\n"
276
"\n"
277
"Reset the context variable.\n"
278
"\n"
279
"The variable is reset to the value it had before the\n"
280
"`ContextVar.set()` that created the token was used.");
281
282
#define _CONTEXTVARS_CONTEXTVAR_RESET_METHODDEF    \
283
    {"reset", (PyCFunction)_contextvars_ContextVar_reset, METH_O, _contextvars_ContextVar_reset__doc__},
284
285
static PyObject *
286
_contextvars_ContextVar_reset_impl(PyContextVar *self, PyObject *token);
287
288
static PyObject *
289
_contextvars_ContextVar_reset(PyObject *self, PyObject *token)
290
0
{
291
0
    PyObject *return_value = NULL;
292
293
0
    return_value = _contextvars_ContextVar_reset_impl((PyContextVar *)self, token);
294
295
0
    return return_value;
296
0
}
297
298
PyDoc_STRVAR(token_enter__doc__,
299
"__enter__($self, /)\n"
300
"--\n"
301
"\n"
302
"Enter into Token context manager.");
303
304
#define TOKEN_ENTER_METHODDEF    \
305
    {"__enter__", (PyCFunction)token_enter, METH_NOARGS, token_enter__doc__},
306
307
static PyObject *
308
token_enter_impl(PyContextToken *self);
309
310
static PyObject *
311
token_enter(PyObject *self, PyObject *Py_UNUSED(ignored))
312
0
{
313
0
    return token_enter_impl((PyContextToken *)self);
314
0
}
315
316
PyDoc_STRVAR(token_exit__doc__,
317
"__exit__($self, type, val, tb, /)\n"
318
"--\n"
319
"\n"
320
"Exit from Token context manager, restore the linked ContextVar.");
321
322
#define TOKEN_EXIT_METHODDEF    \
323
    {"__exit__", _PyCFunction_CAST(token_exit), METH_FASTCALL, token_exit__doc__},
324
325
static PyObject *
326
token_exit_impl(PyContextToken *self, PyObject *type, PyObject *val,
327
                PyObject *tb);
328
329
static PyObject *
330
token_exit(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
331
0
{
332
0
    PyObject *return_value = NULL;
333
0
    PyObject *type;
334
0
    PyObject *val;
335
0
    PyObject *tb;
336
337
0
    if (!_PyArg_CheckPositional("__exit__", nargs, 3, 3)) {
338
0
        goto exit;
339
0
    }
340
0
    type = args[0];
341
0
    val = args[1];
342
0
    tb = args[2];
343
0
    return_value = token_exit_impl((PyContextToken *)self, type, val, tb);
344
345
0
exit:
346
0
    return return_value;
347
0
}
348
/*[clinic end generated code: output=488efab9f3932fbb input=a9049054013a1b77]*/