| 1 | from .support import HPyTest |
| 2 | |
| 3 | class TestTuple(HPyTest): |
| 4 | |
| 5 | def test_Check(self): |
| 6 | mod = self.make_module(""" |
| 7 | HPyDef_METH(f, "f", HPyFunc_O) |
| 8 | static HPy f_impl(HPyContext *ctx, HPy self, HPy arg) |
| 9 | { |
| 10 | if (HPyTuple_Check(ctx, arg)) |
| 11 | return HPy_Dup(ctx, ctx->h_True); |
| 12 | return HPy_Dup(ctx, ctx->h_False); |
| 13 | } |
| 14 | @EXPORT(f) |
| 15 | @INIT |
| 16 | """) |
| 17 | class MyTuple(tuple): |
| 18 | pass |
| 19 | |
| 20 | assert mod.f(()) is True |
| 21 | assert mod.f([]) is False |
| 22 | assert mod.f(MyTuple()) is True |
| 23 | |
| 24 | def test_FromArray(self): |
| 25 | mod = self.make_module(""" |
| 26 | HPyDef_METH(f, "f", HPyFunc_O) |
| 27 | static HPy f_impl(HPyContext *ctx, HPy self, HPy arg) |
| 28 | { |
| 29 | HPy x = HPyLong_FromLong(ctx, 42); |
| 30 | if (HPy_IsNull(x)) |
| 31 | return HPy_NULL; |
| 32 | HPy items[] = {self, arg, x}; |
| 33 | HPy res = HPyTuple_FromArray(ctx, items, 3); |
| 34 | HPy_Close(ctx, x); |
| 35 | return res; |
| 36 | } |
| 37 | @EXPORT(f) |
| 38 | @INIT |
| 39 | """) |
| 40 | assert mod.f('hello') == (mod, 'hello', 42) |
| 41 | |
| 42 | def test_Pack(self): |
| 43 | mod = self.make_module(""" |
| 44 | HPyDef_METH(f, "f", HPyFunc_O) |
| 45 | static HPy f_impl(HPyContext *ctx, HPy self, HPy arg) |
| 46 | { |
| 47 | HPy x = HPyLong_FromLong(ctx, 42); |
| 48 | if (HPy_IsNull(x)) |
| 49 | return HPy_NULL; |
| 50 | HPy result = HPyTuple_Pack(ctx, 3, self, arg, x); |
| 51 | HPy_Close(ctx, x); |
| 52 | return result; |
| 53 | } |
| 54 | @EXPORT(f) |
| 55 | @INIT |
| 56 | """) |
| 57 | assert mod.f('hello') == (mod, 'hello', 42) |
| 58 | |
| 59 | def test_TupleBuilder(self): |
| 60 | mod = self.make_module(""" |
| 61 | HPyDef_METH(f, "f", HPyFunc_O) |
| 62 | static HPy f_impl(HPyContext *ctx, HPy h_self, HPy h_arg) |
| 63 | { |
| 64 | HPyTupleBuilder builder = HPyTupleBuilder_New(ctx, 3); |
| 65 | HPyTupleBuilder_Set(ctx, builder, 0, h_arg); |
| 66 | HPyTupleBuilder_Set(ctx, builder, 1, ctx->h_True); |
| 67 | HPy h_num = HPyLong_FromLong(ctx, -42); |
| 68 | if (HPy_IsNull(h_num)) |
| 69 | { |
| 70 | HPyTupleBuilder_Cancel(ctx, builder); |
| 71 | return HPy_NULL; |
| 72 | } |
| 73 | HPyTupleBuilder_Set(ctx, builder, 2, h_num); |
| 74 | HPy_Close(ctx, h_num); |
| 75 | HPy h_tuple = HPyTupleBuilder_Build(ctx, builder); |
| 76 | return h_tuple; |
| 77 | } |
| 78 | @EXPORT(f) |
| 79 | @INIT |
| 80 | """) |
| 81 | assert mod.f("xy") == ("xy", True, -42) |