Debian Codegraph @

pypy/module/cpyext/test/test_listobject.py

raw symbols SCIP
1 from pypy.module.cpyext.test.test_api import BaseApiTest, raises_w
2 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
3 from pypy.module.cpyext.pyobject import from_ref
4 from pypy.module.cpyext.listobject import (
5 PyList_Check, PyList_CheckExact, PyList_Size)
6
7 class TestListObject(BaseApiTest):
8 def test_list(self, space, api):
9 L = space.appexec([], """():
10 class L(list):
11 pass
12 return L
13 """)
14
15 l = api.PyList_New(0)
16 assert PyList_Check(space, l)
17 assert PyList_CheckExact(space, l)
18
19 l = space.call_function(L)
20 assert PyList_Check(space, l)
21 assert not PyList_CheckExact(space, l)
22
23 assert not PyList_Check(space, space.newtuple([]))
24 assert not PyList_CheckExact(space, space.newtuple([]))
25
26 def test_get_size(self, space, api):
27 l = api.PyList_New(0)
28 assert api.PyList_GET_SIZE(l) == 0
29 api.PyList_Append(l, space.wrap(3))
30 assert api.PyList_GET_SIZE(l) == 1
31
32 def test_size(self, space):
33 l = space.newlist([space.w_None, space.w_None])
34 assert PyList_Size(space, l) == 2
35 with raises_w(space, TypeError):
36 PyList_Size(space, space.w_None)
37
38 def test_insert(self, space, api):
39 w_l = space.newlist([space.w_None, space.w_None])
40 assert api.PyList_Insert(w_l, 0, space.wrap(1)) == 0
41 assert api.PyList_Size(w_l) == 3
42 assert api.PyList_Insert(w_l, 99, space.wrap(2)) == 0
43 assert space.unwrap(from_ref(space, api.PyList_GetItem(w_l, 3))) == 2
44 # insert at index -1: next-to-last
45 assert api.PyList_Insert(w_l, -1, space.wrap(3)) == 0
46 assert space.unwrap(from_ref(space, api.PyList_GET_ITEM(w_l, 3))) == 3
47
48 def test_sort(self, space, api):
49 l = space.newlist([space.wrap(1), space.wrap(0), space.wrap(7000)])
50 assert api.PyList_Sort(l) == 0
51 assert space.eq_w(l, space.newlist([space.wrap(0), space.wrap(1), space.wrap(7000)]))
52
53 def test_reverse(self, space, api):
54 l = space.newlist([space.wrap(3), space.wrap(2), space.wrap(1)])
55 assert api.PyList_Reverse(l) == 0
56 assert space.eq_w(l, space.newlist([space.wrap(1), space.wrap(2), space.wrap(3)]))
57
58 def test_list_tuple(self, space, api):
59 w_l = space.newlist([space.wrap(3), space.wrap(2), space.wrap(1)])
60 w_t = api.PyList_AsTuple(w_l)
61 assert space.unwrap(w_t) == (3, 2, 1)
62
63 def test_list_getslice(self, space, api):
64 w_l = space.newlist([space.wrap(3), space.wrap(2), space.wrap(1)])
65 w_s = api.PyList_GetSlice(w_l, 1, 5)
66 assert space.unwrap(w_s) == [2, 1]
67
68 def test_extend(self, space, api):
69 w_l = space.newlist([space.wrap(3), space.wrap(2), space.wrap(1)])
70 w_expected = space.newlist([space.wrap(3), space.wrap(2), space.wrap(1),
71 space.wrap(3), space.wrap(2), space.wrap(1),])
72 w_result = api._PyList_Extend(w_l, w_l)
73 assert space.eq_w(w_l, w_expected)
74
75 class AppTestListObject(AppTestCpythonExtensionBase):
76 def test_basic_listobject(self):
77 import sys
78 module = self.import_extension('foo', [
79 ("newlist", "METH_NOARGS",
80 """
81 PyObject *lst = PyList_New(3);
82 PyList_SetItem(lst, 0, PyLong_FromLong(3));
83 PyList_SetItem(lst, 2, PyLong_FromLong(1000));
84 PyList_SetItem(lst, 1, PyLong_FromLong(-5));
85 return lst;
86 """
87 ),
88 ("setlistitem", "METH_VARARGS",
89 """
90 PyObject *l = PyTuple_GetItem(args, 0);
91 int index = PyLong_AsLong(PyTuple_GetItem(args, 1));
92 Py_INCREF(Py_None);
93 if (PyList_SetItem(l, index, Py_None) < 0)
94 return NULL;
95 Py_INCREF(Py_None);
96 return Py_None;
97 """
98 ),
99 ("appendlist", "METH_VARARGS",
100 """
101 PyObject *l = PyTuple_GetItem(args, 0);
102 PyList_Append(l, PyTuple_GetItem(args, 1));
103 Py_RETURN_NONE;
104 """
105 ),
106 ("setslice", "METH_VARARGS",
107 """
108 PyObject *l = PyTuple_GetItem(args, 0);
109 PyObject *seq = PyTuple_GetItem(args, 1);
110 if (seq == Py_None) seq = NULL;
111 if (PyList_SetSlice(l, 1, 4, seq) < 0)
112 return NULL;
113 Py_RETURN_NONE;
114 """
115 ),
116 ('test_tp_as_', "METH_NOARGS",
117 '''
118 PyObject *l = PyList_New(3);
119 int ok = l->ob_type->tp_as_sequence != NULL; /* 1 */
120 ok += 2 * (l->ob_type->tp_as_number == NULL); /* 2 */
121 Py_DECREF(l);
122 return PyLong_FromLong(ok); /* should be 3 */
123 '''
124 ),
125 ])
126 l = module.newlist()
127 assert l == [3, -5, 1000]
128 module.setlistitem(l, 0)
129 assert l[0] is None
130
131 class L(list):
132 def __setitem__(self):
133 self.append("XYZ")
134
135 l = L([1])
136 module.setlistitem(l, 0)
137 assert len(l) == 1
138
139 raises(SystemError, module.setlistitem, (1, 2, 3), 0)
140
141 l = []
142 module.appendlist(l, 14)
143 assert len(l) == 1
144 assert l[0] == 14
145
146 l = list(range(6))
147 module.setslice(l, ['a'])
148 assert l == [0, 'a', 4, 5]
149
150 l = list(range(6))
151 module.setslice(l, None)
152 assert l == [0, 4, 5]
153
154 l = [1, 2, 3]
155 module.setlistitem(l,0)
156 assert l == [None, 2, 3]
157
158 # tp_as_sequence should be filled, but tp_as_number should be NULL
159 assert module.test_tp_as_() == 3
160
161 l = module.newlist()
162 p = l.pop()
163 assert p == 1000
164 p = l.pop(0)
165 assert p == 3
166 assert l == [-5]
167
168 def test_list_spec(self):
169 module = self.import_extension("foo", [
170 ("setslice_slot", "METH_VARARGS",
171 """
172 PyObject *l = PyTuple_GetItem(args, 0);
173 PyObject *seq = PyTuple_GetItem(args, 1);
174
175 PyMappingMethods* mp = Py_TYPE(l)->tp_as_mapping;
176 if (mp && mp->mp_ass_subscript) {
177 if (seq == Py_None) seq = NULL;
178 PyObject *py_slice, *py_start, *py_stop;
179 py_start = PyLong_FromSsize_t(1);
180 py_stop = PyLong_FromSsize_t(4);
181 py_slice = PySlice_New(py_start, py_stop, Py_None);
182 int result = mp->mp_ass_subscript(l, py_slice, seq);
183 Py_DECREF(py_start);
184 Py_DECREF(py_stop);
185 Py_DECREF(py_slice);
186 if (result < 0)
187 return NULL;
188 }
189 else {
190 PyErr_Format(PyExc_TypeError,
191 "slice assignment not supported");
192 return NULL;
193 }
194 Py_RETURN_NONE;
195 """),
196 ("get_sublist", "METH_NOARGS",
197 """
198 PyType_Slot slots[] = {
199 {0, 0},
200 };
201 PyType_Spec sublist_spec = {
202 "sublist",
203 0, 0,
204 Py_TPFLAGS_DEFAULT,
205 slots,
206 };
207 PyObject *t1 = PyTuple_Pack(1, (PyObject *)(&PyList_Type));
208 PyObject *ret = PyType_FromSpecWithBases(&sublist_spec, t1);
209 Py_DECREF(t1);
210 return ret;
211 """)])
212
213 sublist = module.get_sublist()
214 l = sublist(range(6))
215 module.setslice_slot(l, ['a'])
216 assert l == [0, 'a', 4, 5]
217
218 def test_list_macros(self):
219 """The PyList_* macros cast, and calls expecting that build."""
220 module = self.import_extension('foo', [
221 ("test_macro_invocations", "METH_O",
222 """
223 PyObject* o = PyList_New(2);
224 PyListObject* l = (PyListObject*)o;
225
226 Py_INCREF(args);
227 Py_INCREF(args);
228 PyList_SET_ITEM(o, 0, args);
229 PyList_SET_ITEM(l, 1, args);
230
231 if(PyList_GET_ITEM(o, 0) != PyList_GET_ITEM(l, 1))
232 {
233 PyErr_SetString(PyExc_AssertionError, "PyList_GET_ITEM error");
234 return NULL;
235 }
236
237 if(PyList_GET_SIZE(o) != PyList_GET_SIZE(l))
238 {
239 PyErr_SetString(PyExc_AssertionError, "PyList_GET_SIZE error");
240 return NULL;
241 }
242
243 return o;
244 """
245 )
246 ])
247 s = 'abcdef'
248 x = module.test_macro_invocations(s)
249 assert x[0] is x[1] is s
250
251 def test_get_item_macro(self):
252 module = self.import_extension('foo', [
253 ("test_get_item", "METH_NOARGS",
254 """
255 PyObject* o, *o2, *o3;
256 o = PyList_New(1);
257
258 o2 = PyBytes_FromString("test_get_item0");
259 Py_INCREF(o2);
260 PyList_SET_ITEM(o, 0, o2);
261
262 o3 = PyList_GET_ITEM(o, 0);
263 Py_INCREF(o3);
264 Py_DECREF(o);
265 Py_DECREF(o2);
266 return o3;
267 """)])
268 assert module.test_get_item() == b'test_get_item0'
269
270 def test_item_refcounts(self):
271 """PyList_SET_ITEM leaks a reference to the target."""
272 module = self.import_extension('foo', [
273 ("test_refcount_diff", "METH_VARARGS",
274 """
275 /* test that the refcount differences for functions
276 * are correct. diff1 - expected refcount diff for i1,
277 * diff2 - expected refcount diff for i2
278 */
279 #define CHECKCOUNT(diff1, diff2, action) \
280 new_count1 = Py_REFCNT(i1); \
281 new_count2 = Py_REFCNT(i2); \
282 diff = new_count1 - old_count1; \
283 if (diff != diff1) {\
284 sprintf(errbuffer, action \
285 " i1 expected diff of %ld got %ld", (long)diff1, (long)diff); \
286 PyErr_SetString(PyExc_AssertionError, errbuffer); \
287 return NULL; } \
288 diff = new_count2 - old_count2; \
289 if (diff != diff2) {\
290 sprintf(errbuffer, action \
291 " i2 expected diff of %ld got %ld", (long)diff2, (long)diff); \
292 PyErr_SetString(PyExc_AssertionError, errbuffer); \
293 return NULL; } \
294 old_count1 = new_count1; \
295 old_count2 = new_count2;
296
297 PyObject* tmp, *o = PyList_New(0);
298 char errbuffer[1024];
299 PyObject* i1 = PyTuple_GetItem(args, 0);
300 PyObject* i2 = PyTuple_GetItem(args, 1);
301 Py_ssize_t old_count1, new_count1;
302 Py_ssize_t old_count2, new_count2;
303 Py_ssize_t diff;
304 int ret;
305
306 old_count1 = Py_REFCNT(i1);
307 old_count2 = Py_REFCNT(i2);
308
309 ret = PyList_Append(o, i1);
310 if (ret != 0)
311 return NULL;
312 /* check the result of Append(), and also force the list
313 to use the CPyListStrategy now */
314 if (PyList_GET_ITEM(o, 0) != i1)
315 {
316 PyErr_SetString(PyExc_AssertionError, "Append() error?");
317 return NULL;
318 }
319 CHECKCOUNT(1, 0, "PyList_Append");
320
321 Py_INCREF(i2); /* for PyList_SET_ITEM */
322 CHECKCOUNT(0, 1, "Py_INCREF");
323
324 PyList_SET_ITEM(o, 0, i2);
325 CHECKCOUNT(0, 0, "PyList_SET_ITEM");
326
327 tmp = PyList_GET_ITEM(o, 0);
328 if (tmp != i2)
329 {
330 PyErr_SetString(PyExc_AssertionError, "SetItem() error?");
331 return NULL;
332 }
333 CHECKCOUNT(0, 0, "PyList_GET_ITEM");
334
335 PyList_SetItem(o, 0, i1);
336 CHECKCOUNT(0, -1, "PyList_Set_Item");
337
338 PyList_GetItem(o, 0);
339 CHECKCOUNT(0, 0, "PyList_Get_Item");
340
341 Py_DECREF(o);
342 #ifndef PYPY_VERSION
343 {
344 // PyPy deletes only at teardown
345 CHECKCOUNT(-1, 0, "Py_DECREF(o)");
346 }
347 #endif
348 return PyLong_FromSsize_t(0);
349 """)])
350 assert module.test_refcount_diff(["first"], ["second"]) == 0