Debian Codegraph @

pypy/module/cpyext/test/test_sliceobject.py

raw symbols SCIP
1 import sys
2 from rpython.rtyper.lltypesystem import rffi, lltype
3 from pypy.module.cpyext.test.test_api import BaseApiTest
4 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
5 from pypy.module.cpyext.api import Py_ssize_t, Py_ssize_tP
6
7 class TestSliceObject(BaseApiTest):
8
9 def test_GetIndicesEx(self, space, api):
10 w = space.wrap
11 def get_indices(w_start, w_stop, w_step, length):
12 w_slice = space.newslice(w_start, w_stop, w_step)
13 values = lltype.malloc(Py_ssize_tP.TO, 4, flavor='raw')
14
15 res = api.PySlice_GetIndicesEx(w_slice, 100, values,
16 rffi.ptradd(values, 1),
17 rffi.ptradd(values, 2),
18 rffi.ptradd(values, 3))
19 assert res == 0
20 rv = values[0], values[1], values[2], values[3]
21 lltype.free(values, flavor='raw')
22 return rv
23 assert get_indices(w(10), w(20), w(1), 200) == (10, 20, 1, 10)
24
25 def test_GetIndices(self, space, api):
26 w = space.wrap
27 def get_indices(w_start, w_stop, w_step, length):
28 w_slice = space.newslice(w_start, w_stop, w_step)
29 values = lltype.malloc(Py_ssize_tP.TO, 3, flavor='raw')
30
31 res = api.PySlice_GetIndices(w_slice, 100, values,
32 rffi.ptradd(values, 1),
33 rffi.ptradd(values, 2))
34 assert res == 0
35 rv = values[0], values[1], values[2]
36 lltype.free(values, flavor='raw')
37 return rv
38 assert get_indices(w(10), w(20), w(1), 200) == (10, 20, 1)
39
40 class AppTestSliceMembers(AppTestCpythonExtensionBase):
41 def test_members(self):
42 module = self.import_extension('foo', [
43 ("clone", "METH_O",
44 """
45 PySliceObject *slice = (PySliceObject *)args;
46 if (Py_TYPE(slice) != &PySlice_Type) {
47 PyErr_SetNone(PyExc_ValueError);
48 return NULL;
49 }
50 return PySlice_New(slice->start,
51 slice->stop,
52 slice->step);
53 """),
54 ])
55 s = slice(10, 20, 30)
56 assert module.clone(s) == s
57
58 def test_nulls(self):
59 module = self.import_extension('foo', [
60 ("nullslice", "METH_NOARGS",
61 """
62 return PySlice_New(NULL, NULL, NULL);
63 """),
64 ])
65 assert module.nullslice() == slice(None, None, None)
66
67 def test_ellipsis(self):
68 module = self.import_extension('foo', [
69 ("get_ellipsis", "METH_NOARGS",
70 """
71 PyObject *ret = Py_Ellipsis;
72 Py_INCREF(ret);
73 return ret;
74 """),
75 ])
76 assert module.get_ellipsis() is Ellipsis
77
78 def test_typecheck(self):
79 module = self.import_extension('foo', [
80 ("check", "METH_O",
81 """
82 PySliceObject *slice = (PySliceObject *)args;
83 return PyLong_FromLong(PySlice_Check(slice));
84 """),
85 ])
86 s = slice(10, 20, 30)
87 assert module.check(s)
88
89 def test_Unpack(self):
90 from sys import maxsize as M
91 module = self.import_extension('foo', [
92 ("check", "METH_O",
93 """
94 Py_ssize_t start, stop, step;
95 if (PySlice_Unpack(args, &start, &stop, &step) != 0)
96 return NULL;
97 return Py_BuildValue("nnn", start, stop, step);
98 """),
99 ])
100 assert module.check(slice(10, 20, 1)) == (10, 20, 1)
101 assert module.check(slice(None, 20, 1)) == (0, 20, 1)
102 assert module.check(slice(10, None, 3)) == (10, M, 3)
103 assert module.check(slice(10, 20, None)) == (10, 20, 1)
104 assert module.check(slice(20, 5, 1)) == (20, 5, 1)
105 assert module.check(slice(None, None, None)) == (0, M, 1)
106
107 assert module.check(slice(20, 10, -1)) == (20, 10, -1)
108 assert module.check(slice(None, 20, -1)) == (M, 20, -1)
109 assert module.check(slice(10, None, -1)) == (10, -M-1, -1)
110
111 assert module.check(slice(M*2, M*3, 1)) == (M, M, 1)
112 assert module.check(slice(M*2, -123, 1)) == (M, -123, 1)
113 assert module.check(slice(-M*2, -M*3, 1)) == (-M-1, -M-1, 1)
114 assert module.check(slice(-M*2, 123, -2)) == (-M-1, 123, -2)
115
116 with raises(ValueError):
117 module.check(slice(2, 3, 0))
118 assert module.check(slice(2, 3, -M-1)) == (2, 3, -M)
119 assert module.check(slice(2, 3, -M-10)) == (2, 3, -M)
120 assert module.check(slice(2, 3, M+10)) == (2, 3, M)
121
122 def test_AdjustIndices(self):
123 module = self.import_extension('foo', [
124 ("check", "METH_NOARGS",
125 """
126 Py_ssize_t start = -35, stop = 99999, step = 10, result;
127 result = PySlice_AdjustIndices(100, &start, &stop, step);
128 return Py_BuildValue("nnnn", result, start, stop, step);
129 """),
130 ])
131 assert module.check() == (4, 65, 100, 10)