File: test_sliceobject.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-- 5,141 bytes parent folder | download | duplicates (5)
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
import sys
from rpython.rtyper.lltypesystem import rffi, lltype
from pypy.module.cpyext.test.test_api import BaseApiTest
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
from pypy.module.cpyext.api import Py_ssize_t, Py_ssize_tP

class TestSliceObject(BaseApiTest):

    def test_GetIndicesEx(self, space, api):
        w = space.wrap
        def get_indices(w_start, w_stop, w_step, length):
            w_slice = space.newslice(w_start, w_stop, w_step)
            values = lltype.malloc(Py_ssize_tP.TO, 4, flavor='raw')

            res = api.PySlice_GetIndicesEx(w_slice, 100, values,
                rffi.ptradd(values, 1),
                rffi.ptradd(values, 2),
                rffi.ptradd(values, 3))
            assert res == 0
            rv = values[0], values[1], values[2], values[3]
            lltype.free(values, flavor='raw')
            return rv
        assert get_indices(w(10), w(20), w(1), 200) == (10, 20, 1, 10)

    def test_GetIndices(self, space, api):
        w = space.wrap
        def get_indices(w_start, w_stop, w_step, length):
            w_slice = space.newslice(w_start, w_stop, w_step)
            values = lltype.malloc(Py_ssize_tP.TO, 3, flavor='raw')

            res = api.PySlice_GetIndices(w_slice, 100, values,
                rffi.ptradd(values, 1),
                rffi.ptradd(values, 2))
            assert res == 0
            rv = values[0], values[1], values[2]
            lltype.free(values, flavor='raw')
            return rv
        assert get_indices(w(10), w(20), w(1), 200) == (10, 20, 1)

class AppTestSliceMembers(AppTestCpythonExtensionBase):
    def test_members(self):
        module = self.import_extension('foo', [
            ("clone", "METH_O",
             """
                 PySliceObject *slice = (PySliceObject *)args;
                 if (Py_TYPE(slice) != &PySlice_Type) {
                     PyErr_SetNone(PyExc_ValueError);
                     return NULL;
                 }
                 return PySlice_New(slice->start,
                                    slice->stop,
                                    slice->step);
             """),
            ])
        s = slice(10, 20, 30)
        assert module.clone(s) == s

    def test_nulls(self):
        module = self.import_extension('foo', [
            ("nullslice", "METH_NOARGS",
             """
                 return PySlice_New(NULL, NULL, NULL);
             """),
            ])
        assert module.nullslice() == slice(None, None, None)

    def test_ellipsis(self):
        module = self.import_extension('foo', [
            ("get_ellipsis", "METH_NOARGS",
             """
                 PyObject *ret = Py_Ellipsis;
                 Py_INCREF(ret);
                 return ret;
             """),
            ])
        assert module.get_ellipsis() is Ellipsis

    def test_typecheck(self):
        module = self.import_extension('foo', [
            ("check", "METH_O",
             """
                 PySliceObject *slice = (PySliceObject *)args;
                 return PyLong_FromLong(PySlice_Check(slice));
             """),
            ])
        s = slice(10, 20, 30)
        assert module.check(s)

    def test_Unpack(self):
        from sys import maxsize as M
        module = self.import_extension('foo', [
            ("check", "METH_O",
             """
                 Py_ssize_t start, stop, step;
                 if (PySlice_Unpack(args, &start, &stop, &step) != 0)
                     return NULL;
                 return Py_BuildValue("nnn", start, stop, step);
             """),
            ])
        assert module.check(slice(10, 20, 1)) == (10, 20, 1)
        assert module.check(slice(None, 20, 1)) == (0, 20, 1)
        assert module.check(slice(10, None, 3)) == (10, M, 3)
        assert module.check(slice(10, 20, None)) == (10, 20, 1)
        assert module.check(slice(20, 5, 1)) == (20, 5, 1)
        assert module.check(slice(None, None, None)) == (0, M, 1)

        assert module.check(slice(20, 10, -1)) == (20, 10, -1)
        assert module.check(slice(None, 20, -1)) == (M, 20, -1)
        assert module.check(slice(10, None, -1)) == (10, -M-1, -1)

        assert module.check(slice(M*2, M*3, 1)) == (M, M, 1)
        assert module.check(slice(M*2, -123, 1)) == (M, -123, 1)
        assert module.check(slice(-M*2, -M*3, 1)) == (-M-1, -M-1, 1)
        assert module.check(slice(-M*2, 123, -2)) == (-M-1, 123, -2)

        with raises(ValueError):
            module.check(slice(2, 3, 0))
        assert module.check(slice(2, 3, -M-1)) == (2, 3, -M)
        assert module.check(slice(2, 3, -M-10)) == (2, 3, -M)
        assert module.check(slice(2, 3, M+10)) == (2, 3, M)

    def test_AdjustIndices(self):
        module = self.import_extension('foo', [
            ("check", "METH_NOARGS",
             """
                 Py_ssize_t start = -35, stop = 99999, step = 10, result;
                 result = PySlice_AdjustIndices(100, &start, &stop, step);
                 return Py_BuildValue("nnnn", result, start, stop, step);
             """),
            ])
        assert module.check() == (4, 65, 100, 10)