File: test_hpyiter.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 (88 lines) | stat: -rw-r--r-- 2,542 bytes parent folder | download | duplicates (2)
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
from .support import HPyTest

class TestIter(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 (HPyIter_Check(ctx, arg))
                    return HPy_Dup(ctx, ctx->h_True);
                return HPy_Dup(ctx, ctx->h_False);
            }
            @EXPORT(f)
            @INIT
        """)

        class CustomIterable:
            def __init__(self):
                self._iter = iter([1, 2, 3])

            def __iter__(self):
                return self._iter

        class CustomIterator:
            def __init__(self):
                self._iter = iter([1, 2, 3])

            def __iter__(self):
                return self._iter

            def __next__(self):
                return next(self._iter)

        assert mod.f(object()) is False
        assert mod.f(10) is False

        assert mod.f((1, 2)) is False
        assert mod.f(iter((1, 2))) is True

        assert mod.f([]) is False
        assert mod.f(iter([])) is True

        assert mod.f('hello') is False
        assert mod.f(iter('hello')) is True

        assert mod.f(map(int, ("1", "2"))) is True
        assert mod.f(range(1, 10)) is False

        assert mod.f(CustomIterable()) is False
        assert mod.f(iter(CustomIterable())) is True
        assert mod.f(CustomIterator()) is True

    def test_Next(self):
        mod = self.make_module("""
            HPyDef_METH(f, "f", HPyFunc_O)
            static HPy f_impl(HPyContext *ctx, HPy self, HPy arg)
            {
                HPy result = HPyIter_Next(ctx, arg);
                int is_null = HPy_IsNull(result);

                if (is_null && HPyErr_Occurred(ctx))
                    return HPy_NULL;
                if (is_null)
                    return HPyErr_SetObject(ctx, ctx->h_StopIteration, ctx->h_None);
                return result;
            }
            @EXPORT(f)
            @INIT
        """)

        import pytest
        with pytest.raises(StopIteration):
            assert mod.f(iter([]))

        class CustomIterator:
            def __init__(self):
                self._iter = iter(["a", "b", "c"])

            def __iter__(self):
                return self._iter

            def __next__(self):
                return next(self._iter)
            
        assert mod.f(iter([3, 2, 1])) == 3
        assert mod.f((i for i in range(1, 10))) == 1
        assert mod.f(CustomIterator()) == "a"