| 1 | """ |
| 2 | NOTE: this tests are also meant to be run as PyPy "applevel" tests. |
| 3 | |
| 4 | This means that global imports will NOT be visible inside the test |
| 5 | functions. In particular, you have to "import pytest" inside the test in order |
| 6 | to be able to use e.g. pytest.raises (which on PyPy will be implemented by a |
| 7 | "fake pytest module") |
| 8 | """ |
| 9 | from .support import HPyTest |
| 10 | |
| 11 | |
| 12 | class TestHPyContextVar(HPyTest): |
| 13 | |
| 14 | def test_basics(self): |
| 15 | mod = self.make_module(""" |
| 16 | HPyDef_METH(new_ctxv, "new_ctxv", HPyFunc_NOARGS) |
| 17 | static HPy new_ctxv_impl(HPyContext *ctx, HPy self) |
| 18 | { |
| 19 | return HPyContextVar_New(ctx, "test_contextvar", HPy_NULL); |
| 20 | } |
| 21 | |
| 22 | HPyDef_METH(set_ctxv, "set_ctxv", HPyFunc_VARARGS) |
| 23 | static HPy set_ctxv_impl(HPyContext *ctx, HPy self, const HPy *args, size_t nargs) |
| 24 | { |
| 25 | HPy obj, val; |
| 26 | if (!HPyArg_Parse(ctx, NULL, args, nargs, "OO", &obj, &val)) |
| 27 | return HPy_NULL; |
| 28 | return HPyContextVar_Set(ctx, obj, val); |
| 29 | } |
| 30 | HPyDef_METH(get_ctxv, "get_ctxv", HPyFunc_VARARGS) |
| 31 | static HPy get_ctxv_impl(HPyContext *ctx, HPy self, const HPy *args, size_t nargs) |
| 32 | { |
| 33 | HPy obj, def=HPy_NULL, val; |
| 34 | if (!HPyArg_Parse(ctx, NULL, args, nargs, "O|O", &obj, &def)) |
| 35 | return HPy_NULL; |
| 36 | if (HPyContextVar_Get(ctx, obj, def, &val) < 0) { |
| 37 | return HPy_NULL; |
| 38 | } |
| 39 | return val; |
| 40 | } |
| 41 | |
| 42 | |
| 43 | @EXPORT(new_ctxv) |
| 44 | @EXPORT(get_ctxv) |
| 45 | @EXPORT(set_ctxv) |
| 46 | @INIT |
| 47 | """) |
| 48 | var = mod.new_ctxv() |
| 49 | tok = mod.set_ctxv(var, 4) |
| 50 | assert tok.var is var |
| 51 | four = mod.get_ctxv(var) |
| 52 | assert four == 4 |