File: test_hpybytes.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (131 lines) | stat: -rw-r--r-- 4,418 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from .support import HPyTest

class TestBytes(HPyTest):

    def test_Check(self):
        mod = self.make_module("""
            HPyDef_METH(f, "f", HPyFunc_O)
            static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
            {
                if (HPyBytes_Check(ctx, arg))
                    return HPy_Dup(ctx, ctx->h_True);
                return HPy_Dup(ctx, ctx->h_False);
            }
            @EXPORT(f)
            @INIT
        """)
        class MyBytes(bytes):
            pass

        assert mod.f(b'hello') is True
        assert mod.f('hello') is False
        assert mod.f(MyBytes(b'hello')) is True

    def test_Size(self):
        mod = self.make_module("""
            HPyDef_METH(f, "f", HPyFunc_O)
            static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
            {
                HPy_ssize_t a = HPyBytes_Size(ctx, arg);
                HPy_ssize_t b = HPyBytes_GET_SIZE(ctx, arg);
                return HPyLong_FromLongLong(ctx, 10 * a + b);
            }
            @EXPORT(f)
            @INIT
        """)
        assert mod.f(b'hello') == 55

    def test_AsString(self):
        mod = self.make_module("""
            HPyDef_METH(f, "f", HPyFunc_O)
            static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
            {
                long res = 0;
                HPy_ssize_t n = HPyBytes_Size(ctx, arg);
                const char *buf = HPyBytes_AsString(ctx, arg);
                for(int i=0; i<n; i++)
                    res = (res * 10) + buf[i];
                return HPyLong_FromLong(ctx, res);
            }
            @EXPORT(f)
            @INIT
        """)
        assert mod.f(b'ABC') == 100*ord('A') + 10*ord('B') + ord('C')

    def test_AS_STRING(self):
        mod = self.make_module("""
            HPyDef_METH(f, "f", HPyFunc_O)
            static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
            {
                long res = 0;
                HPy_ssize_t n = HPyBytes_Size(ctx, arg);
                const char *buf = HPyBytes_AS_STRING(ctx, arg);
                for(int i=0; i<n; i++)
                    res = (res * 10) + buf[i];
                return HPyLong_FromLong(ctx, res);
            }
            @EXPORT(f)
            @INIT
        """)
        assert mod.f(b'ABC') == 100*ord('A') + 10*ord('B') + ord('C')

    def test_FromString(self):
        mod = self.make_module("""
            HPyDef_METH(f, "f", HPyFunc_O)
            static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
            {
                const char *buf;
                buf = HPyBytes_AsString(ctx, arg);
                return HPyBytes_FromString(ctx, buf);
            }

            @EXPORT(f)
            @INIT
        """)
        assert mod.f(b"aaa") == b"aaa"
        assert mod.f(b"") == b""

    def test_FromStringAndSize(self):
        import pytest
        mod = self.make_module("""
            HPyDef_METH(f, "f", HPyFunc_VARARGS)
            static HPy f_impl(HPyContext *ctx, HPy self, const HPy *args,
                              size_t nargs)
            {
                HPy src;
                long len;
                const char *buf;
                if (!HPyArg_Parse(ctx, NULL, args, nargs, "Ol", &src, &len)) {
                    return HPy_NULL;
                }
                buf = HPyBytes_AsString(ctx, src);
                return HPyBytes_FromStringAndSize(ctx, buf, len);
            }

            HPyDef_METH(f_null, "f_null", HPyFunc_VARARGS)
            static HPy f_null_impl(HPyContext *ctx, HPy self, const HPy *args,
                                   size_t nargs)
            {
                long len;
                if (!HPyArg_Parse(ctx, NULL, args, nargs, "l", &len)) {
                    return HPy_NULL;
                }

                return HPyBytes_FromStringAndSize(ctx, NULL, len);
            }

            @EXPORT(f)
            @EXPORT(f_null)
            @INIT
        """)
        assert mod.f(b"aaa", 3) == b"aaa"
        assert mod.f(b"abc", 2) == b"ab"
        assert mod.f(b"", 0) == b""
        with pytest.raises(SystemError):
            # negative size passed to HPyBytes_FromStringAndSize
            mod.f(b"abc", -1)
        for i in (-1, 0, 1):
            with pytest.raises(ValueError) as err:
                mod.f_null(i)
            assert str(err.value) == (
                "NULL char * passed to HPyBytes_FromStringAndSize")