|
3 | 3 | class TestString(HPyTest): |
4 | 4 | def test_unicode_builder(self): |
5 | 5 | mod = self.make_module(""" |
6 | | - HPyDef_METH(f, "f", f_impl, HPyFunc_O) |
7 | | - static HPy f_impl(HPyContext *ctx, HPy h_self, HPy h_arg) |
| 6 | + HPyDef_METH(f, "f", f_impl, HPyFunc_NOARGS) |
| 7 | + static HPy f_impl(HPyContext *ctx, HPy h_self) |
8 | 8 | { |
9 | 9 | HPyUnicodeBuilder builder = HPyUnicodeBuilder_New(ctx, 0); |
10 | 10 | if(HPy_IsNull(builder)) { |
11 | 11 | HPyErr_SetString(ctx, ctx->h_RuntimeError, "Could not create HPyUnicodeBuilder"); |
12 | 12 | return HPy_NULL; |
13 | 13 | } |
14 | | - HPy h_s1 = HPyUnicode_FromString(ctx, "hello "); |
15 | | - HPy h_s2 = HPyUnicode_FromString(ctx, "!"); |
16 | | - HPyUnicodeBuilder_Add(ctx, builder, h_s1); |
17 | | - HPyUnicodeBuilder_Add(ctx, builder, h_arg); |
18 | | - HPyUnicodeBuilder_Add(ctx, builder, h_s2); |
| 14 | + HPyUnicodeBuilder_Add(ctx, builder, "hello "); |
| 15 | + HPyUnicodeBuilder_Add(ctx, builder, "world"); |
| 16 | + HPyUnicodeBuilder_Add(ctx, builder, "!"); |
19 | 17 | HPy h_string = HPyUnicodeBuilder_Build(ctx, builder); |
20 | | - HPy_Close(ctx, h_s1); |
21 | | - HPy_Close(ctx, h_s2); |
22 | 18 | return h_string; |
23 | 19 | } |
24 | 20 | @EXPORT(f) |
25 | 21 | @INIT |
26 | 22 | """) |
27 | | - assert mod.f("world") == "hello world!" |
28 | | - |
29 | | - def test_type_error(self): |
30 | | - mod = self.make_module(""" |
31 | | - HPyDef_METH(f, "f", f_impl, HPyFunc_O) |
32 | | - static HPy f_impl(HPyContext *ctx, HPy h_self, HPy h_arg) |
33 | | - { |
34 | | - HPyUnicodeBuilder builder = HPyUnicodeBuilder_New(ctx, 0); |
35 | | - if(HPy_IsNull(builder)) { |
36 | | - HPyErr_SetString(ctx, ctx->h_RuntimeError, "Could not create HPyUnicodeBuilder"); |
37 | | - return HPy_NULL; |
38 | | - } |
39 | | - HPy h_long = HPyLong_FromLong(ctx, 42); |
40 | | - HPyUnicodeBuilder_Add(ctx, builder, h_long); |
41 | | - HPy_Close(ctx, h_long); |
42 | | - HPyUnicodeBuilder_Cancel(ctx, builder); |
43 | | - return HPy_NULL; |
44 | | - } |
45 | | - @EXPORT(f) |
46 | | - @INIT |
47 | | - """) |
48 | | - import pytest |
49 | | - with pytest.raises(TypeError) as exc_info: |
50 | | - mod.f("world") |
51 | | - assert exc_info.match("Argument must be of type HPyUnicode") |
| 23 | + assert mod.f() == "hello world!" |
0 commit comments