Debian Codegraph @

pypy/module/_hpy_universal/test/_vendored/test_hpybytes.py

raw symbols SCIP
1 from .support import HPyTest
2
3 class TestBytes(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 (HPyBytes_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 MyBytes(bytes):
18 pass
19
20 assert mod.f(b'hello') is True
21 assert mod.f('hello') is False
22 assert mod.f(MyBytes(b'hello')) is True
23
24 def test_Size(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_ssize_t a = HPyBytes_Size(ctx, arg);
30 HPy_ssize_t b = HPyBytes_GET_SIZE(ctx, arg);
31 return HPyLong_FromLongLong(ctx, 10 * a + b);
32 }
33 @EXPORT(f)
34 @INIT
35 """)
36 assert mod.f(b'hello') == 55
37
38 def test_AsString(self):
39 mod = self.make_module("""
40 HPyDef_METH(f, "f", HPyFunc_O)
41 static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
42 {
43 long res = 0;
44 HPy_ssize_t n = HPyBytes_Size(ctx, arg);
45 const char *buf = HPyBytes_AsString(ctx, arg);
46 for(int i=0; i<n; i++)
47 res = (res * 10) + buf[i];
48 return HPyLong_FromLong(ctx, res);
49 }
50 @EXPORT(f)
51 @INIT
52 """)
53 assert mod.f(b'ABC') == 100*ord('A') + 10*ord('B') + ord('C')
54
55 def test_AS_STRING(self):
56 mod = self.make_module("""
57 HPyDef_METH(f, "f", HPyFunc_O)
58 static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
59 {
60 long res = 0;
61 HPy_ssize_t n = HPyBytes_Size(ctx, arg);
62 const char *buf = HPyBytes_AS_STRING(ctx, arg);
63 for(int i=0; i<n; i++)
64 res = (res * 10) + buf[i];
65 return HPyLong_FromLong(ctx, res);
66 }
67 @EXPORT(f)
68 @INIT
69 """)
70 assert mod.f(b'ABC') == 100*ord('A') + 10*ord('B') + ord('C')
71
72 def test_FromString(self):
73 mod = self.make_module("""
74 HPyDef_METH(f, "f", HPyFunc_O)
75 static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
76 {
77 const char *buf;
78 buf = HPyBytes_AsString(ctx, arg);
79 return HPyBytes_FromString(ctx, buf);
80 }
81
82 @EXPORT(f)
83 @INIT
84 """)
85 assert mod.f(b"aaa") == b"aaa"
86 assert mod.f(b"") == b""
87
88 def test_FromStringAndSize(self):
89 import pytest
90 mod = self.make_module("""
91 HPyDef_METH(f, "f", HPyFunc_VARARGS)
92 static HPy f_impl(HPyContext *ctx, HPy self, const HPy *args,
93 size_t nargs)
94 {
95 HPy src;
96 long len;
97 const char *buf;
98 if (!HPyArg_Parse(ctx, NULL, args, nargs, "Ol", &src, &len)) {
99 return HPy_NULL;
100 }
101 buf = HPyBytes_AsString(ctx, src);
102 return HPyBytes_FromStringAndSize(ctx, buf, len);
103 }
104
105 HPyDef_METH(f_null, "f_null", HPyFunc_VARARGS)
106 static HPy f_null_impl(HPyContext *ctx, HPy self, const HPy *args,
107 size_t nargs)
108 {
109 long len;
110 if (!HPyArg_Parse(ctx, NULL, args, nargs, "l", &len)) {
111 return HPy_NULL;
112 }
113
114 return HPyBytes_FromStringAndSize(ctx, NULL, len);
115 }
116
117 @EXPORT(f)
118 @EXPORT(f_null)
119 @INIT
120 """)
121 assert mod.f(b"aaa", 3) == b"aaa"
122 assert mod.f(b"abc", 2) == b"ab"
123 assert mod.f(b"", 0) == b""
124 with pytest.raises(SystemError):
125 # negative size passed to HPyBytes_FromStringAndSize
126 mod.f(b"abc", -1)
127 for i in (-1, 0, 1):
128 with pytest.raises(ValueError) as err:
129 mod.f_null(i)
130 assert str(err.value) == (
131 "NULL char * passed to HPyBytes_FromStringAndSize")