| 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 TestHPyModuleAddType(HPyTest): |
| 13 | def test_with_spec_only(self): |
| 14 | mod = self.make_module(""" |
| 15 | static HPyType_Spec dummy_spec = { |
| 16 | .name = "mytest.Dummy", |
| 17 | }; |
| 18 | |
| 19 | HPyDef_METH(f, "f", HPyFunc_NOARGS) |
| 20 | static HPy f_impl(HPyContext *ctx, HPy self) |
| 21 | { |
| 22 | if (!HPyHelpers_AddType(ctx, self, "Dummy", &dummy_spec, NULL)) |
| 23 | { |
| 24 | return HPy_NULL; |
| 25 | } |
| 26 | return HPy_Dup(ctx, ctx->h_None); |
| 27 | } |
| 28 | |
| 29 | @EXPORT(f) |
| 30 | @INIT |
| 31 | """) |
| 32 | assert not hasattr(mod, "Dummy") |
| 33 | mod.f() |
| 34 | assert isinstance(mod.Dummy, type) |
| 35 | assert mod.Dummy.__name__ == "Dummy" |
| 36 | assert isinstance(mod.Dummy(), mod.Dummy) |
| 37 | |
| 38 | def test_with_spec_and_params(self): |
| 39 | mod = self.make_module(""" |
| 40 | static HPyType_Spec dummy_spec = { |
| 41 | .name = "mytest.Dummy", |
| 42 | }; |
| 43 | |
| 44 | HPyDef_METH(f, "f", HPyFunc_NOARGS) |
| 45 | static HPy f_impl(HPyContext *ctx, HPy self) |
| 46 | { |
| 47 | HPyType_SpecParam param[] = { |
| 48 | { HPyType_SpecParam_Base, ctx->h_LongType }, |
| 49 | { (HPyType_SpecParam_Kind)0 } |
| 50 | }; |
| 51 | if (!HPyHelpers_AddType(ctx, self, "Dummy", &dummy_spec, param)) |
| 52 | { |
| 53 | return HPy_NULL; |
| 54 | } |
| 55 | return HPy_Dup(ctx, ctx->h_None); |
| 56 | } |
| 57 | |
| 58 | @EXPORT(f) |
| 59 | @INIT |
| 60 | """) |
| 61 | assert not hasattr(mod, "Dummy") |
| 62 | mod.f() |
| 63 | assert isinstance(mod.Dummy, type) |
| 64 | assert mod.Dummy.__name__ == "Dummy" |
| 65 | assert isinstance(mod.Dummy(), mod.Dummy) |
| 66 | assert isinstance(mod.Dummy(), int) |
| 67 | assert mod.Dummy() == 0 |
| 68 | assert mod.Dummy(3) == 3 |
| 69 | |
| 70 | def test_pack_args_and_keywords(self): |
| 71 | import pytest |
| 72 | mod = self.make_module(""" |
| 73 | HPyDef_METH(pack, "pack", HPyFunc_KEYWORDS) |
| 74 | static HPy pack_impl(HPyContext *ctx, HPy self, const HPy *args, |
| 75 | size_t nargs, HPy kwnames) |
| 76 | { |
| 77 | HPy out[] = { HPy_NULL, HPy_NULL }; |
| 78 | HPy result; |
| 79 | if (!HPyHelpers_PackArgsAndKeywords(ctx, args, nargs, kwnames, |
| 80 | &out[0], &out[1])) { |
| 81 | return HPy_NULL; |
| 82 | } |
| 83 | for (int i=0; i < 2; i++) { |
| 84 | if (HPy_IsNull(out[i])) { |
| 85 | out[i] = HPy_Dup(ctx, ctx->h_None); |
| 86 | } |
| 87 | } |
| 88 | result = HPyTuple_FromArray(ctx, out, 2); |
| 89 | for (int i=0; i < 2; i++) { |
| 90 | HPy_Close(ctx, out[i]); |
| 91 | } |
| 92 | return result; |
| 93 | } |
| 94 | |
| 95 | HPyDef_METH(pack_error, "pack_error", HPyFunc_O) |
| 96 | static HPy pack_error_impl(HPyContext *ctx, HPy self, HPy arg) |
| 97 | { |
| 98 | int success; |
| 99 | HPy t = HPy_NULL; |
| 100 | HPy d = HPy_NULL; |
| 101 | const HPy args[] = { ctx->h_None, ctx->h_True, ctx->h_False }; |
| 102 | size_t nargs = sizeof(args); |
| 103 | uint64_t mode = HPyLong_AsUInt64_t(ctx, arg); |
| 104 | switch (mode) { |
| 105 | case 0: |
| 106 | success = HPyHelpers_PackArgsAndKeywords(ctx, args, nargs, |
| 107 | HPy_NULL, NULL, &d); |
| 108 | break; |
| 109 | case 1: |
| 110 | success = HPyHelpers_PackArgsAndKeywords(ctx, args, nargs, |
| 111 | HPy_NULL, &t, NULL); |
| 112 | break; |
| 113 | case 2: |
| 114 | success = HPyHelpers_PackArgsAndKeywords(ctx, args, nargs, |
| 115 | HPy_NULL, NULL, NULL); |
| 116 | break; |
| 117 | case 3: |
| 118 | success = HPyHelpers_PackArgsAndKeywords(ctx, args, nargs, |
| 119 | ctx->h_None, &t, &d); |
| 120 | break; |
| 121 | default: |
| 122 | success = 0; |
| 123 | HPyErr_SetString(ctx, ctx->h_ValueError, |
| 124 | "unknown test mode"); |
| 125 | break; |
| 126 | } |
| 127 | if (success) |
| 128 | return HPy_Dup(ctx, ctx->h_None); |
| 129 | return HPy_NULL; |
| 130 | } |
| 131 | |
| 132 | @EXPORT(pack) |
| 133 | @EXPORT(pack_error) |
| 134 | @INIT |
| 135 | """) |
| 136 | assert mod.pack() == (None, None) |
| 137 | assert mod.pack(1, '2', b'3') == ((1, '2', b'3'), None) |
| 138 | assert mod.pack(1, '2', b'3', a='b', c='d') == ((1, '2', b'3'), dict(a='b', c='d')) |
| 139 | assert mod.pack(a='b', c='d') == (None, dict(a='b', c='d')) |
| 140 | for mode in range(3): |
| 141 | with pytest.raises(SystemError): |
| 142 | mod.pack_error(mode) |
| 143 | with pytest.raises(TypeError): |
| 144 | mod.pack_error(3) |