blob: eedfc88654b6242fdb8fa0d9cb9a7f9c7960fc24 [file] [log] [blame]
Yury Selivanovf23746a2018-01-22 19:11:18 -05001/*[clinic input]
2preserve
3[clinic start generated code]*/
4
5PyDoc_STRVAR(_contextvars_Context_get__doc__,
6"get($self, key, default=None, /)\n"
7"--\n"
Peter Lamut20678fd2018-07-30 16:15:44 +01008"\n"
9"Return the value for `key` if `key` has the value in the context object.\n"
10"\n"
11"If `key` does not exist, return `default`. If `default` is not given,\n"
12"return None.");
Yury Selivanovf23746a2018-01-22 19:11:18 -050013
14#define _CONTEXTVARS_CONTEXT_GET_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +020015 {"get", (PyCFunction)(void(*)(void))_contextvars_Context_get, METH_FASTCALL, _contextvars_Context_get__doc__},
Yury Selivanovf23746a2018-01-22 19:11:18 -050016
17static PyObject *
18_contextvars_Context_get_impl(PyContext *self, PyObject *key,
19 PyObject *default_value);
20
21static PyObject *
22_contextvars_Context_get(PyContext *self, PyObject *const *args, Py_ssize_t nargs)
23{
24 PyObject *return_value = NULL;
25 PyObject *key;
26 PyObject *default_value = Py_None;
27
Serhiy Storchaka2a39d252019-01-11 18:01:42 +020028 if (!_PyArg_CheckPositional("get", nargs, 1, 2)) {
Yury Selivanovf23746a2018-01-22 19:11:18 -050029 goto exit;
30 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +020031 key = args[0];
32 if (nargs < 2) {
33 goto skip_optional;
34 }
35 default_value = args[1];
36skip_optional:
Yury Selivanovf23746a2018-01-22 19:11:18 -050037 return_value = _contextvars_Context_get_impl(self, key, default_value);
38
39exit:
40 return return_value;
41}
42
43PyDoc_STRVAR(_contextvars_Context_items__doc__,
44"items($self, /)\n"
45"--\n"
Peter Lamut20678fd2018-07-30 16:15:44 +010046"\n"
47"Return all variables and their values in the context object.\n"
48"\n"
49"The result is returned as a list of 2-tuples (variable, value).");
Yury Selivanovf23746a2018-01-22 19:11:18 -050050
51#define _CONTEXTVARS_CONTEXT_ITEMS_METHODDEF \
52 {"items", (PyCFunction)_contextvars_Context_items, METH_NOARGS, _contextvars_Context_items__doc__},
53
54static PyObject *
55_contextvars_Context_items_impl(PyContext *self);
56
57static PyObject *
58_contextvars_Context_items(PyContext *self, PyObject *Py_UNUSED(ignored))
59{
60 return _contextvars_Context_items_impl(self);
61}
62
63PyDoc_STRVAR(_contextvars_Context_keys__doc__,
64"keys($self, /)\n"
65"--\n"
Peter Lamut20678fd2018-07-30 16:15:44 +010066"\n"
67"Return a list of all variables in the context object.");
Yury Selivanovf23746a2018-01-22 19:11:18 -050068
69#define _CONTEXTVARS_CONTEXT_KEYS_METHODDEF \
70 {"keys", (PyCFunction)_contextvars_Context_keys, METH_NOARGS, _contextvars_Context_keys__doc__},
71
72static PyObject *
73_contextvars_Context_keys_impl(PyContext *self);
74
75static PyObject *
76_contextvars_Context_keys(PyContext *self, PyObject *Py_UNUSED(ignored))
77{
78 return _contextvars_Context_keys_impl(self);
79}
80
81PyDoc_STRVAR(_contextvars_Context_values__doc__,
82"values($self, /)\n"
83"--\n"
Peter Lamut20678fd2018-07-30 16:15:44 +010084"\n"
animalize463572c2019-02-25 07:18:48 +080085"Return a list of all variables\' values in the context object.");
Yury Selivanovf23746a2018-01-22 19:11:18 -050086
87#define _CONTEXTVARS_CONTEXT_VALUES_METHODDEF \
88 {"values", (PyCFunction)_contextvars_Context_values, METH_NOARGS, _contextvars_Context_values__doc__},
89
90static PyObject *
91_contextvars_Context_values_impl(PyContext *self);
92
93static PyObject *
94_contextvars_Context_values(PyContext *self, PyObject *Py_UNUSED(ignored))
95{
96 return _contextvars_Context_values_impl(self);
97}
98
99PyDoc_STRVAR(_contextvars_Context_copy__doc__,
100"copy($self, /)\n"
101"--\n"
Peter Lamut20678fd2018-07-30 16:15:44 +0100102"\n"
103"Return a shallow copy of the context object.");
Yury Selivanovf23746a2018-01-22 19:11:18 -0500104
105#define _CONTEXTVARS_CONTEXT_COPY_METHODDEF \
106 {"copy", (PyCFunction)_contextvars_Context_copy, METH_NOARGS, _contextvars_Context_copy__doc__},
107
108static PyObject *
109_contextvars_Context_copy_impl(PyContext *self);
110
111static PyObject *
112_contextvars_Context_copy(PyContext *self, PyObject *Py_UNUSED(ignored))
113{
114 return _contextvars_Context_copy_impl(self);
115}
116
117PyDoc_STRVAR(_contextvars_ContextVar_get__doc__,
118"get($self, default=None, /)\n"
119"--\n"
Peter Lamut20678fd2018-07-30 16:15:44 +0100120"\n"
121"Return a value for the context variable for the current context.\n"
122"\n"
123"If there is no value for the variable in the current context, the method will:\n"
124" * return the value of the default argument of the method, if provided; or\n"
125" * return the default value for the context variable, if it was created\n"
126" with one; or\n"
127" * raise a LookupError.");
Yury Selivanovf23746a2018-01-22 19:11:18 -0500128
129#define _CONTEXTVARS_CONTEXTVAR_GET_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200130 {"get", (PyCFunction)(void(*)(void))_contextvars_ContextVar_get, METH_FASTCALL, _contextvars_ContextVar_get__doc__},
Yury Selivanovf23746a2018-01-22 19:11:18 -0500131
132static PyObject *
133_contextvars_ContextVar_get_impl(PyContextVar *self, PyObject *default_value);
134
135static PyObject *
136_contextvars_ContextVar_get(PyContextVar *self, PyObject *const *args, Py_ssize_t nargs)
137{
138 PyObject *return_value = NULL;
139 PyObject *default_value = NULL;
140
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200141 if (!_PyArg_CheckPositional("get", nargs, 0, 1)) {
Yury Selivanovf23746a2018-01-22 19:11:18 -0500142 goto exit;
143 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200144 if (nargs < 1) {
145 goto skip_optional;
146 }
147 default_value = args[0];
148skip_optional:
Yury Selivanovf23746a2018-01-22 19:11:18 -0500149 return_value = _contextvars_ContextVar_get_impl(self, default_value);
150
151exit:
152 return return_value;
153}
154
155PyDoc_STRVAR(_contextvars_ContextVar_set__doc__,
156"set($self, value, /)\n"
157"--\n"
Peter Lamut20678fd2018-07-30 16:15:44 +0100158"\n"
159"Call to set a new value for the context variable in the current context.\n"
160"\n"
161"The required value argument is the new value for the context variable.\n"
162"\n"
163"Returns a Token object that can be used to restore the variable to its previous\n"
164"value via the `ContextVar.reset()` method.");
Yury Selivanovf23746a2018-01-22 19:11:18 -0500165
166#define _CONTEXTVARS_CONTEXTVAR_SET_METHODDEF \
167 {"set", (PyCFunction)_contextvars_ContextVar_set, METH_O, _contextvars_ContextVar_set__doc__},
168
169PyDoc_STRVAR(_contextvars_ContextVar_reset__doc__,
170"reset($self, token, /)\n"
171"--\n"
Peter Lamut20678fd2018-07-30 16:15:44 +0100172"\n"
173"Reset the context variable.\n"
174"\n"
175"The variable is reset to the value it had before the `ContextVar.set()` that\n"
176"created the token was used.");
Yury Selivanovf23746a2018-01-22 19:11:18 -0500177
178#define _CONTEXTVARS_CONTEXTVAR_RESET_METHODDEF \
179 {"reset", (PyCFunction)_contextvars_ContextVar_reset, METH_O, _contextvars_ContextVar_reset__doc__},
animalize463572c2019-02-25 07:18:48 +0800180/*[clinic end generated code: output=a86b66e1516c25d4 input=a9049054013a1b77]*/