Debian Codegraph @

pypy/module/cpyext/test/test_contextvars.py

raw symbols SCIP
1 import pytest
2 import sys
3 import StringIO
4
5 from pypy.module.cpyext.state import State
6 from pypy.module.cpyext.pyobject import make_ref
7 from pypy.module.cpyext.test.test_api import BaseApiTest
8 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
9 from rpython.rtyper.lltypesystem import rffi
10
11 class AppTestContext(AppTestCpythonExtensionBase):
12
13 def test_context(self):
14 module = self.import_extension('foo', [
15 ("new", "METH_VARARGS",
16 '''
17 PyObject *obj = NULL;
18 const char *name;
19 if (!PyArg_ParseTuple(args, "s|O:new", &name, &obj)) {
20 return NULL;
21 }
22 return PyContextVar_New(name, obj);
23 '''
24 ),
25 ("set", "METH_VARARGS",
26 '''
27 PyObject *obj, *val;
28 if (!PyArg_ParseTuple(args, "OO:set", &obj, &val)) {
29 return NULL;
30 }
31 return PyContextVar_Set(obj, val);
32 '''
33 ),
34 ("reset", "METH_VARARGS",
35 '''
36 PyObject *obj, *token;
37 if (!PyArg_ParseTuple(args, "OO:reset", &obj, &token)) {
38 return NULL;
39 }
40 int ret = PyContextVar_Reset(obj, token);
41 if (ret < 0) return NULL;
42 return PyLong_FromLong(ret);
43 '''
44 ),
45 ("get", "METH_VARARGS",
46 '''
47 PyObject *obj, *def=NULL, *val;
48 if (!PyArg_ParseTuple(args, "O|O:get", &obj, &def)) {
49 return NULL;
50 }
51 if (PyContextVar_Get(obj, def, &val) < 0) {
52 return NULL;
53 }
54 if (val == NULL) {
55 Py_RETURN_NONE;
56 }
57 return val;
58
59 '''
60 ),
61 ("get_value", "METH_VARARGS",
62 '''
63 /* equivalent to cython's
64 Cython/Includes/cpython/contextvars.pxd
65 */
66 PyObject *var, *value=NULL, *default_value=NULL;
67 if (!PyArg_ParseTuple(args, "O|O:get_value", &var, &default_value)) {
68 return NULL;
69 }
70 if (PyContextVar_Get(var, NULL, &value) < 0) {
71 return NULL;
72 }
73 if (value == NULL) {
74 if (default_value == NULL)
75 Py_RETURN_NONE;
76 Py_INCREF(default_value);
77 return default_value;
78 }
79 return value;
80 '''
81 ),
82 ("get_value_no_default", "METH_VARARGS",
83 '''
84 /* equivalent to cython's
85 Cython/Includes/cpython/contextvars.pxd
86 */
87 PyObject *var, *value=NULL, *default_value=NULL;
88 if (!PyArg_ParseTuple(args, "O|O:get_value_no_default", &var,
89 &default_value)) {
90 return NULL;
91 }
92 if (PyContextVar_Get(var, default_value, &value) < 0) {
93 return NULL;
94 }
95 if (value == NULL) {
96 Py_RETURN_NONE;
97 }
98 return value;
99 '''
100 ),
101 ])
102
103 var = module.new("testme", 3)
104 tok4 = module.set(var, 4)
105 assert tok4.var is var
106 assert module.get(var) == 4
107 tok5 = module.set(var, 5)
108 assert tok5.var is var
109 assert module.get(var) == 5
110 module.reset(var, tok5)
111 assert module.get(var) == 4
112
113 # no default
114 var = module.new("testme")
115 five = module.get(var, 5)
116 assert five == 5
117
118 # cython tests
119 import contextvars
120 pycvar = contextvars.ContextVar("pycvar")
121 cvar = module.new("cvar")
122 cvar_with_default = module.new("cvar_wd", "DEFAULT")
123
124
125 assert module.get_value(cvar) is None
126 assert module.get_value(cvar, "default") == "default"
127 assert module.get_value(pycvar) is None
128 assert module.get_value(pycvar, "default") == "default"
129 assert module.get_value(cvar_with_default) == "DEFAULT"
130 assert module.get_value(cvar_with_default, "default") == "DEFAULT"
131
132 assert module.get_value_no_default(cvar) is None
133 assert module.get_value_no_default(cvar, "default") == "default"
134 assert module.get_value_no_default(pycvar) is None
135 assert module.get_value_no_default(pycvar, "default") == "default"
136 assert module.get_value_no_default(cvar_with_default) == "DEFAULT"
137 # this is the only variant that gives a different answer
138 ret = module.get_value_no_default(cvar_with_default, "default")
139 print('ret', ret)
140 assert ret == "default"