1 | n/a | /* |
---|
2 | n/a | Written by Jim Hugunin and Chris Chase. |
---|
3 | n/a | |
---|
4 | n/a | This includes both the singular ellipsis object and slice objects. |
---|
5 | n/a | |
---|
6 | n/a | Guido, feel free to do whatever you want in the way of copyrights |
---|
7 | n/a | for this file. |
---|
8 | n/a | */ |
---|
9 | n/a | |
---|
10 | n/a | /* |
---|
11 | n/a | Py_Ellipsis encodes the '...' rubber index token. It is similar to |
---|
12 | n/a | the Py_NoneStruct in that there is no way to create other objects of |
---|
13 | n/a | this type and there is exactly one in existence. |
---|
14 | n/a | */ |
---|
15 | n/a | |
---|
16 | n/a | #include "Python.h" |
---|
17 | n/a | #include "structmember.h" |
---|
18 | n/a | |
---|
19 | n/a | static PyObject * |
---|
20 | n/a | ellipsis_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
---|
21 | n/a | { |
---|
22 | n/a | if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) { |
---|
23 | n/a | PyErr_SetString(PyExc_TypeError, "EllipsisType takes no arguments"); |
---|
24 | n/a | return NULL; |
---|
25 | n/a | } |
---|
26 | n/a | Py_INCREF(Py_Ellipsis); |
---|
27 | n/a | return Py_Ellipsis; |
---|
28 | n/a | } |
---|
29 | n/a | |
---|
30 | n/a | static PyObject * |
---|
31 | n/a | ellipsis_repr(PyObject *op) |
---|
32 | n/a | { |
---|
33 | n/a | return PyUnicode_FromString("Ellipsis"); |
---|
34 | n/a | } |
---|
35 | n/a | |
---|
36 | n/a | static PyObject * |
---|
37 | n/a | ellipsis_reduce(PyObject *op) |
---|
38 | n/a | { |
---|
39 | n/a | return PyUnicode_FromString("Ellipsis"); |
---|
40 | n/a | } |
---|
41 | n/a | |
---|
42 | n/a | static PyMethodDef ellipsis_methods[] = { |
---|
43 | n/a | {"__reduce__", (PyCFunction)ellipsis_reduce, METH_NOARGS, NULL}, |
---|
44 | n/a | {NULL, NULL} |
---|
45 | n/a | }; |
---|
46 | n/a | |
---|
47 | n/a | PyTypeObject PyEllipsis_Type = { |
---|
48 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
---|
49 | n/a | "ellipsis", /* tp_name */ |
---|
50 | n/a | 0, /* tp_basicsize */ |
---|
51 | n/a | 0, /* tp_itemsize */ |
---|
52 | n/a | 0, /*never called*/ /* tp_dealloc */ |
---|
53 | n/a | 0, /* tp_print */ |
---|
54 | n/a | 0, /* tp_getattr */ |
---|
55 | n/a | 0, /* tp_setattr */ |
---|
56 | n/a | 0, /* tp_reserved */ |
---|
57 | n/a | ellipsis_repr, /* tp_repr */ |
---|
58 | n/a | 0, /* tp_as_number */ |
---|
59 | n/a | 0, /* tp_as_sequence */ |
---|
60 | n/a | 0, /* tp_as_mapping */ |
---|
61 | n/a | 0, /* tp_hash */ |
---|
62 | n/a | 0, /* tp_call */ |
---|
63 | n/a | 0, /* tp_str */ |
---|
64 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
---|
65 | n/a | 0, /* tp_setattro */ |
---|
66 | n/a | 0, /* tp_as_buffer */ |
---|
67 | n/a | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
---|
68 | n/a | 0, /* tp_doc */ |
---|
69 | n/a | 0, /* tp_traverse */ |
---|
70 | n/a | 0, /* tp_clear */ |
---|
71 | n/a | 0, /* tp_richcompare */ |
---|
72 | n/a | 0, /* tp_weaklistoffset */ |
---|
73 | n/a | 0, /* tp_iter */ |
---|
74 | n/a | 0, /* tp_iternext */ |
---|
75 | n/a | ellipsis_methods, /* tp_methods */ |
---|
76 | n/a | 0, /* tp_members */ |
---|
77 | n/a | 0, /* tp_getset */ |
---|
78 | n/a | 0, /* tp_base */ |
---|
79 | n/a | 0, /* tp_dict */ |
---|
80 | n/a | 0, /* tp_descr_get */ |
---|
81 | n/a | 0, /* tp_descr_set */ |
---|
82 | n/a | 0, /* tp_dictoffset */ |
---|
83 | n/a | 0, /* tp_init */ |
---|
84 | n/a | 0, /* tp_alloc */ |
---|
85 | n/a | ellipsis_new, /* tp_new */ |
---|
86 | n/a | }; |
---|
87 | n/a | |
---|
88 | n/a | PyObject _Py_EllipsisObject = { |
---|
89 | n/a | _PyObject_EXTRA_INIT |
---|
90 | n/a | 1, &PyEllipsis_Type |
---|
91 | n/a | }; |
---|
92 | n/a | |
---|
93 | n/a | |
---|
94 | n/a | /* Slice object implementation */ |
---|
95 | n/a | |
---|
96 | n/a | /* Using a cache is very effective since typically only a single slice is |
---|
97 | n/a | * created and then deleted again |
---|
98 | n/a | */ |
---|
99 | n/a | static PySliceObject *slice_cache = NULL; |
---|
100 | n/a | void PySlice_Fini(void) |
---|
101 | n/a | { |
---|
102 | n/a | PySliceObject *obj = slice_cache; |
---|
103 | n/a | if (obj != NULL) { |
---|
104 | n/a | slice_cache = NULL; |
---|
105 | n/a | PyObject_GC_Del(obj); |
---|
106 | n/a | } |
---|
107 | n/a | } |
---|
108 | n/a | |
---|
109 | n/a | /* start, stop, and step are python objects with None indicating no |
---|
110 | n/a | index is present. |
---|
111 | n/a | */ |
---|
112 | n/a | |
---|
113 | n/a | PyObject * |
---|
114 | n/a | PySlice_New(PyObject *start, PyObject *stop, PyObject *step) |
---|
115 | n/a | { |
---|
116 | n/a | PySliceObject *obj; |
---|
117 | n/a | if (slice_cache != NULL) { |
---|
118 | n/a | obj = slice_cache; |
---|
119 | n/a | slice_cache = NULL; |
---|
120 | n/a | _Py_NewReference((PyObject *)obj); |
---|
121 | n/a | } else { |
---|
122 | n/a | obj = PyObject_GC_New(PySliceObject, &PySlice_Type); |
---|
123 | n/a | if (obj == NULL) |
---|
124 | n/a | return NULL; |
---|
125 | n/a | } |
---|
126 | n/a | |
---|
127 | n/a | if (step == NULL) step = Py_None; |
---|
128 | n/a | Py_INCREF(step); |
---|
129 | n/a | if (start == NULL) start = Py_None; |
---|
130 | n/a | Py_INCREF(start); |
---|
131 | n/a | if (stop == NULL) stop = Py_None; |
---|
132 | n/a | Py_INCREF(stop); |
---|
133 | n/a | |
---|
134 | n/a | obj->step = step; |
---|
135 | n/a | obj->start = start; |
---|
136 | n/a | obj->stop = stop; |
---|
137 | n/a | |
---|
138 | n/a | _PyObject_GC_TRACK(obj); |
---|
139 | n/a | return (PyObject *) obj; |
---|
140 | n/a | } |
---|
141 | n/a | |
---|
142 | n/a | PyObject * |
---|
143 | n/a | _PySlice_FromIndices(Py_ssize_t istart, Py_ssize_t istop) |
---|
144 | n/a | { |
---|
145 | n/a | PyObject *start, *end, *slice; |
---|
146 | n/a | start = PyLong_FromSsize_t(istart); |
---|
147 | n/a | if (!start) |
---|
148 | n/a | return NULL; |
---|
149 | n/a | end = PyLong_FromSsize_t(istop); |
---|
150 | n/a | if (!end) { |
---|
151 | n/a | Py_DECREF(start); |
---|
152 | n/a | return NULL; |
---|
153 | n/a | } |
---|
154 | n/a | |
---|
155 | n/a | slice = PySlice_New(start, end, NULL); |
---|
156 | n/a | Py_DECREF(start); |
---|
157 | n/a | Py_DECREF(end); |
---|
158 | n/a | return slice; |
---|
159 | n/a | } |
---|
160 | n/a | |
---|
161 | n/a | int |
---|
162 | n/a | PySlice_GetIndices(PyObject *_r, Py_ssize_t length, |
---|
163 | n/a | Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step) |
---|
164 | n/a | { |
---|
165 | n/a | PySliceObject *r = (PySliceObject*)_r; |
---|
166 | n/a | /* XXX support long ints */ |
---|
167 | n/a | if (r->step == Py_None) { |
---|
168 | n/a | *step = 1; |
---|
169 | n/a | } else { |
---|
170 | n/a | if (!PyLong_Check(r->step)) return -1; |
---|
171 | n/a | *step = PyLong_AsSsize_t(r->step); |
---|
172 | n/a | } |
---|
173 | n/a | if (r->start == Py_None) { |
---|
174 | n/a | *start = *step < 0 ? length-1 : 0; |
---|
175 | n/a | } else { |
---|
176 | n/a | if (!PyLong_Check(r->start)) return -1; |
---|
177 | n/a | *start = PyLong_AsSsize_t(r->start); |
---|
178 | n/a | if (*start < 0) *start += length; |
---|
179 | n/a | } |
---|
180 | n/a | if (r->stop == Py_None) { |
---|
181 | n/a | *stop = *step < 0 ? -1 : length; |
---|
182 | n/a | } else { |
---|
183 | n/a | if (!PyLong_Check(r->stop)) return -1; |
---|
184 | n/a | *stop = PyLong_AsSsize_t(r->stop); |
---|
185 | n/a | if (*stop < 0) *stop += length; |
---|
186 | n/a | } |
---|
187 | n/a | if (*stop > length) return -1; |
---|
188 | n/a | if (*start >= length) return -1; |
---|
189 | n/a | if (*step == 0) return -1; |
---|
190 | n/a | return 0; |
---|
191 | n/a | } |
---|
192 | n/a | |
---|
193 | n/a | int |
---|
194 | n/a | PySlice_Unpack(PyObject *_r, |
---|
195 | n/a | Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step) |
---|
196 | n/a | { |
---|
197 | n/a | PySliceObject *r = (PySliceObject*)_r; |
---|
198 | n/a | /* this is harder to get right than you might think */ |
---|
199 | n/a | |
---|
200 | n/a | if (r->step == Py_None) { |
---|
201 | n/a | *step = 1; |
---|
202 | n/a | } |
---|
203 | n/a | else { |
---|
204 | n/a | if (!_PyEval_SliceIndex(r->step, step)) return -1; |
---|
205 | n/a | if (*step == 0) { |
---|
206 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
207 | n/a | "slice step cannot be zero"); |
---|
208 | n/a | return -1; |
---|
209 | n/a | } |
---|
210 | n/a | /* Here *step might be -PY_SSIZE_T_MAX-1; in this case we replace it |
---|
211 | n/a | * with -PY_SSIZE_T_MAX. This doesn't affect the semantics, and it |
---|
212 | n/a | * guards against later undefined behaviour resulting from code that |
---|
213 | n/a | * does "step = -step" as part of a slice reversal. |
---|
214 | n/a | */ |
---|
215 | n/a | if (*step < -PY_SSIZE_T_MAX) |
---|
216 | n/a | *step = -PY_SSIZE_T_MAX; |
---|
217 | n/a | } |
---|
218 | n/a | |
---|
219 | n/a | if (r->start == Py_None) { |
---|
220 | n/a | *start = *step < 0 ? PY_SSIZE_T_MAX-1 : 0;; |
---|
221 | n/a | } |
---|
222 | n/a | else { |
---|
223 | n/a | if (!_PyEval_SliceIndex(r->start, start)) return -1; |
---|
224 | n/a | } |
---|
225 | n/a | |
---|
226 | n/a | if (r->stop == Py_None) { |
---|
227 | n/a | *stop = *step < 0 ? -PY_SSIZE_T_MAX : PY_SSIZE_T_MAX; |
---|
228 | n/a | } |
---|
229 | n/a | else { |
---|
230 | n/a | if (!_PyEval_SliceIndex(r->stop, stop)) return -1; |
---|
231 | n/a | } |
---|
232 | n/a | |
---|
233 | n/a | return 0; |
---|
234 | n/a | } |
---|
235 | n/a | |
---|
236 | n/a | Py_ssize_t |
---|
237 | n/a | PySlice_AdjustIndices(Py_ssize_t length, |
---|
238 | n/a | Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t step) |
---|
239 | n/a | { |
---|
240 | n/a | /* this is harder to get right than you might think */ |
---|
241 | n/a | |
---|
242 | n/a | assert(step != 0); |
---|
243 | n/a | assert(step >= -PY_SSIZE_T_MAX); |
---|
244 | n/a | |
---|
245 | n/a | if (*start < 0) { |
---|
246 | n/a | *start += length; |
---|
247 | n/a | if (*start < 0) { |
---|
248 | n/a | *start = (step < 0) ? -1 : 0; |
---|
249 | n/a | } |
---|
250 | n/a | } |
---|
251 | n/a | else if (*start >= length) { |
---|
252 | n/a | *start = (step < 0) ? length - 1 : length; |
---|
253 | n/a | } |
---|
254 | n/a | |
---|
255 | n/a | if (*stop < 0) { |
---|
256 | n/a | *stop += length; |
---|
257 | n/a | if (*stop < 0) { |
---|
258 | n/a | *stop = (step < 0) ? -1 : 0; |
---|
259 | n/a | } |
---|
260 | n/a | } |
---|
261 | n/a | else if (*stop >= length) { |
---|
262 | n/a | *stop = (step < 0) ? length - 1 : length; |
---|
263 | n/a | } |
---|
264 | n/a | |
---|
265 | n/a | if (step < 0) { |
---|
266 | n/a | if (*stop < *start) { |
---|
267 | n/a | return (*start - *stop - 1) / (-step) + 1; |
---|
268 | n/a | } |
---|
269 | n/a | } |
---|
270 | n/a | else { |
---|
271 | n/a | if (*start < *stop) { |
---|
272 | n/a | return (*stop - *start - 1) / step + 1; |
---|
273 | n/a | } |
---|
274 | n/a | } |
---|
275 | n/a | return 0; |
---|
276 | n/a | } |
---|
277 | n/a | |
---|
278 | n/a | #undef PySlice_GetIndicesEx |
---|
279 | n/a | |
---|
280 | n/a | int |
---|
281 | n/a | PySlice_GetIndicesEx(PyObject *_r, Py_ssize_t length, |
---|
282 | n/a | Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, |
---|
283 | n/a | Py_ssize_t *slicelength) |
---|
284 | n/a | { |
---|
285 | n/a | if (PySlice_Unpack(_r, start, stop, step) < 0) |
---|
286 | n/a | return -1; |
---|
287 | n/a | *slicelength = PySlice_AdjustIndices(length, start, stop, *step); |
---|
288 | n/a | return 0; |
---|
289 | n/a | } |
---|
290 | n/a | |
---|
291 | n/a | static PyObject * |
---|
292 | n/a | slice_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
---|
293 | n/a | { |
---|
294 | n/a | PyObject *start, *stop, *step; |
---|
295 | n/a | |
---|
296 | n/a | start = stop = step = NULL; |
---|
297 | n/a | |
---|
298 | n/a | if (!_PyArg_NoKeywords("slice()", kw)) |
---|
299 | n/a | return NULL; |
---|
300 | n/a | |
---|
301 | n/a | if (!PyArg_UnpackTuple(args, "slice", 1, 3, &start, &stop, &step)) |
---|
302 | n/a | return NULL; |
---|
303 | n/a | |
---|
304 | n/a | /* This swapping of stop and start is to maintain similarity with |
---|
305 | n/a | range(). */ |
---|
306 | n/a | if (stop == NULL) { |
---|
307 | n/a | stop = start; |
---|
308 | n/a | start = NULL; |
---|
309 | n/a | } |
---|
310 | n/a | return PySlice_New(start, stop, step); |
---|
311 | n/a | } |
---|
312 | n/a | |
---|
313 | n/a | PyDoc_STRVAR(slice_doc, |
---|
314 | n/a | "slice(stop)\n\ |
---|
315 | n/a | slice(start, stop[, step])\n\ |
---|
316 | n/a | \n\ |
---|
317 | n/a | Create a slice object. This is used for extended slicing (e.g. a[0:10:2])."); |
---|
318 | n/a | |
---|
319 | n/a | static void |
---|
320 | n/a | slice_dealloc(PySliceObject *r) |
---|
321 | n/a | { |
---|
322 | n/a | _PyObject_GC_UNTRACK(r); |
---|
323 | n/a | Py_DECREF(r->step); |
---|
324 | n/a | Py_DECREF(r->start); |
---|
325 | n/a | Py_DECREF(r->stop); |
---|
326 | n/a | if (slice_cache == NULL) |
---|
327 | n/a | slice_cache = r; |
---|
328 | n/a | else |
---|
329 | n/a | PyObject_GC_Del(r); |
---|
330 | n/a | } |
---|
331 | n/a | |
---|
332 | n/a | static PyObject * |
---|
333 | n/a | slice_repr(PySliceObject *r) |
---|
334 | n/a | { |
---|
335 | n/a | return PyUnicode_FromFormat("slice(%R, %R, %R)", r->start, r->stop, r->step); |
---|
336 | n/a | } |
---|
337 | n/a | |
---|
338 | n/a | static PyMemberDef slice_members[] = { |
---|
339 | n/a | {"start", T_OBJECT, offsetof(PySliceObject, start), READONLY}, |
---|
340 | n/a | {"stop", T_OBJECT, offsetof(PySliceObject, stop), READONLY}, |
---|
341 | n/a | {"step", T_OBJECT, offsetof(PySliceObject, step), READONLY}, |
---|
342 | n/a | {0} |
---|
343 | n/a | }; |
---|
344 | n/a | |
---|
345 | n/a | /* Helper function to convert a slice argument to a PyLong, and raise TypeError |
---|
346 | n/a | with a suitable message on failure. */ |
---|
347 | n/a | |
---|
348 | n/a | static PyObject* |
---|
349 | n/a | evaluate_slice_index(PyObject *v) |
---|
350 | n/a | { |
---|
351 | n/a | if (PyIndex_Check(v)) { |
---|
352 | n/a | return PyNumber_Index(v); |
---|
353 | n/a | } |
---|
354 | n/a | else { |
---|
355 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
356 | n/a | "slice indices must be integers or " |
---|
357 | n/a | "None or have an __index__ method"); |
---|
358 | n/a | return NULL; |
---|
359 | n/a | } |
---|
360 | n/a | } |
---|
361 | n/a | |
---|
362 | n/a | /* Compute slice indices given a slice and length. Return -1 on failure. Used |
---|
363 | n/a | by slice.indices and rangeobject slicing. Assumes that `len` is a |
---|
364 | n/a | nonnegative instance of PyLong. */ |
---|
365 | n/a | |
---|
366 | n/a | int |
---|
367 | n/a | _PySlice_GetLongIndices(PySliceObject *self, PyObject *length, |
---|
368 | n/a | PyObject **start_ptr, PyObject **stop_ptr, |
---|
369 | n/a | PyObject **step_ptr) |
---|
370 | n/a | { |
---|
371 | n/a | PyObject *start=NULL, *stop=NULL, *step=NULL; |
---|
372 | n/a | PyObject *upper=NULL, *lower=NULL; |
---|
373 | n/a | int step_is_negative, cmp_result; |
---|
374 | n/a | |
---|
375 | n/a | /* Convert step to an integer; raise for zero step. */ |
---|
376 | n/a | if (self->step == Py_None) { |
---|
377 | n/a | step = PyLong_FromLong(1L); |
---|
378 | n/a | if (step == NULL) |
---|
379 | n/a | goto error; |
---|
380 | n/a | step_is_negative = 0; |
---|
381 | n/a | } |
---|
382 | n/a | else { |
---|
383 | n/a | int step_sign; |
---|
384 | n/a | step = evaluate_slice_index(self->step); |
---|
385 | n/a | if (step == NULL) |
---|
386 | n/a | goto error; |
---|
387 | n/a | step_sign = _PyLong_Sign(step); |
---|
388 | n/a | if (step_sign == 0) { |
---|
389 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
390 | n/a | "slice step cannot be zero"); |
---|
391 | n/a | goto error; |
---|
392 | n/a | } |
---|
393 | n/a | step_is_negative = step_sign < 0; |
---|
394 | n/a | } |
---|
395 | n/a | |
---|
396 | n/a | /* Find lower and upper bounds for start and stop. */ |
---|
397 | n/a | if (step_is_negative) { |
---|
398 | n/a | lower = PyLong_FromLong(-1L); |
---|
399 | n/a | if (lower == NULL) |
---|
400 | n/a | goto error; |
---|
401 | n/a | |
---|
402 | n/a | upper = PyNumber_Add(length, lower); |
---|
403 | n/a | if (upper == NULL) |
---|
404 | n/a | goto error; |
---|
405 | n/a | } |
---|
406 | n/a | else { |
---|
407 | n/a | lower = PyLong_FromLong(0L); |
---|
408 | n/a | if (lower == NULL) |
---|
409 | n/a | goto error; |
---|
410 | n/a | |
---|
411 | n/a | upper = length; |
---|
412 | n/a | Py_INCREF(upper); |
---|
413 | n/a | } |
---|
414 | n/a | |
---|
415 | n/a | /* Compute start. */ |
---|
416 | n/a | if (self->start == Py_None) { |
---|
417 | n/a | start = step_is_negative ? upper : lower; |
---|
418 | n/a | Py_INCREF(start); |
---|
419 | n/a | } |
---|
420 | n/a | else { |
---|
421 | n/a | start = evaluate_slice_index(self->start); |
---|
422 | n/a | if (start == NULL) |
---|
423 | n/a | goto error; |
---|
424 | n/a | |
---|
425 | n/a | if (_PyLong_Sign(start) < 0) { |
---|
426 | n/a | /* start += length */ |
---|
427 | n/a | PyObject *tmp = PyNumber_Add(start, length); |
---|
428 | n/a | Py_DECREF(start); |
---|
429 | n/a | start = tmp; |
---|
430 | n/a | if (start == NULL) |
---|
431 | n/a | goto error; |
---|
432 | n/a | |
---|
433 | n/a | cmp_result = PyObject_RichCompareBool(start, lower, Py_LT); |
---|
434 | n/a | if (cmp_result < 0) |
---|
435 | n/a | goto error; |
---|
436 | n/a | if (cmp_result) { |
---|
437 | n/a | Py_INCREF(lower); |
---|
438 | n/a | Py_DECREF(start); |
---|
439 | n/a | start = lower; |
---|
440 | n/a | } |
---|
441 | n/a | } |
---|
442 | n/a | else { |
---|
443 | n/a | cmp_result = PyObject_RichCompareBool(start, upper, Py_GT); |
---|
444 | n/a | if (cmp_result < 0) |
---|
445 | n/a | goto error; |
---|
446 | n/a | if (cmp_result) { |
---|
447 | n/a | Py_INCREF(upper); |
---|
448 | n/a | Py_DECREF(start); |
---|
449 | n/a | start = upper; |
---|
450 | n/a | } |
---|
451 | n/a | } |
---|
452 | n/a | } |
---|
453 | n/a | |
---|
454 | n/a | /* Compute stop. */ |
---|
455 | n/a | if (self->stop == Py_None) { |
---|
456 | n/a | stop = step_is_negative ? lower : upper; |
---|
457 | n/a | Py_INCREF(stop); |
---|
458 | n/a | } |
---|
459 | n/a | else { |
---|
460 | n/a | stop = evaluate_slice_index(self->stop); |
---|
461 | n/a | if (stop == NULL) |
---|
462 | n/a | goto error; |
---|
463 | n/a | |
---|
464 | n/a | if (_PyLong_Sign(stop) < 0) { |
---|
465 | n/a | /* stop += length */ |
---|
466 | n/a | PyObject *tmp = PyNumber_Add(stop, length); |
---|
467 | n/a | Py_DECREF(stop); |
---|
468 | n/a | stop = tmp; |
---|
469 | n/a | if (stop == NULL) |
---|
470 | n/a | goto error; |
---|
471 | n/a | |
---|
472 | n/a | cmp_result = PyObject_RichCompareBool(stop, lower, Py_LT); |
---|
473 | n/a | if (cmp_result < 0) |
---|
474 | n/a | goto error; |
---|
475 | n/a | if (cmp_result) { |
---|
476 | n/a | Py_INCREF(lower); |
---|
477 | n/a | Py_DECREF(stop); |
---|
478 | n/a | stop = lower; |
---|
479 | n/a | } |
---|
480 | n/a | } |
---|
481 | n/a | else { |
---|
482 | n/a | cmp_result = PyObject_RichCompareBool(stop, upper, Py_GT); |
---|
483 | n/a | if (cmp_result < 0) |
---|
484 | n/a | goto error; |
---|
485 | n/a | if (cmp_result) { |
---|
486 | n/a | Py_INCREF(upper); |
---|
487 | n/a | Py_DECREF(stop); |
---|
488 | n/a | stop = upper; |
---|
489 | n/a | } |
---|
490 | n/a | } |
---|
491 | n/a | } |
---|
492 | n/a | |
---|
493 | n/a | *start_ptr = start; |
---|
494 | n/a | *stop_ptr = stop; |
---|
495 | n/a | *step_ptr = step; |
---|
496 | n/a | Py_DECREF(upper); |
---|
497 | n/a | Py_DECREF(lower); |
---|
498 | n/a | return 0; |
---|
499 | n/a | |
---|
500 | n/a | error: |
---|
501 | n/a | *start_ptr = *stop_ptr = *step_ptr = NULL; |
---|
502 | n/a | Py_XDECREF(start); |
---|
503 | n/a | Py_XDECREF(stop); |
---|
504 | n/a | Py_XDECREF(step); |
---|
505 | n/a | Py_XDECREF(upper); |
---|
506 | n/a | Py_XDECREF(lower); |
---|
507 | n/a | return -1; |
---|
508 | n/a | } |
---|
509 | n/a | |
---|
510 | n/a | /* Implementation of slice.indices. */ |
---|
511 | n/a | |
---|
512 | n/a | static PyObject* |
---|
513 | n/a | slice_indices(PySliceObject* self, PyObject* len) |
---|
514 | n/a | { |
---|
515 | n/a | PyObject *start, *stop, *step; |
---|
516 | n/a | PyObject *length; |
---|
517 | n/a | int error; |
---|
518 | n/a | |
---|
519 | n/a | /* Convert length to an integer if necessary; raise for negative length. */ |
---|
520 | n/a | length = PyNumber_Index(len); |
---|
521 | n/a | if (length == NULL) |
---|
522 | n/a | return NULL; |
---|
523 | n/a | |
---|
524 | n/a | if (_PyLong_Sign(length) < 0) { |
---|
525 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
526 | n/a | "length should not be negative"); |
---|
527 | n/a | Py_DECREF(length); |
---|
528 | n/a | return NULL; |
---|
529 | n/a | } |
---|
530 | n/a | |
---|
531 | n/a | error = _PySlice_GetLongIndices(self, length, &start, &stop, &step); |
---|
532 | n/a | Py_DECREF(length); |
---|
533 | n/a | if (error == -1) |
---|
534 | n/a | return NULL; |
---|
535 | n/a | else |
---|
536 | n/a | return Py_BuildValue("(NNN)", start, stop, step); |
---|
537 | n/a | } |
---|
538 | n/a | |
---|
539 | n/a | PyDoc_STRVAR(slice_indices_doc, |
---|
540 | n/a | "S.indices(len) -> (start, stop, stride)\n\ |
---|
541 | n/a | \n\ |
---|
542 | n/a | Assuming a sequence of length len, calculate the start and stop\n\ |
---|
543 | n/a | indices, and the stride length of the extended slice described by\n\ |
---|
544 | n/a | S. Out of bounds indices are clipped in a manner consistent with the\n\ |
---|
545 | n/a | handling of normal slices."); |
---|
546 | n/a | |
---|
547 | n/a | static PyObject * |
---|
548 | n/a | slice_reduce(PySliceObject* self) |
---|
549 | n/a | { |
---|
550 | n/a | return Py_BuildValue("O(OOO)", Py_TYPE(self), self->start, self->stop, self->step); |
---|
551 | n/a | } |
---|
552 | n/a | |
---|
553 | n/a | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
---|
554 | n/a | |
---|
555 | n/a | static PyMethodDef slice_methods[] = { |
---|
556 | n/a | {"indices", (PyCFunction)slice_indices, |
---|
557 | n/a | METH_O, slice_indices_doc}, |
---|
558 | n/a | {"__reduce__", (PyCFunction)slice_reduce, |
---|
559 | n/a | METH_NOARGS, reduce_doc}, |
---|
560 | n/a | {NULL, NULL} |
---|
561 | n/a | }; |
---|
562 | n/a | |
---|
563 | n/a | static PyObject * |
---|
564 | n/a | slice_richcompare(PyObject *v, PyObject *w, int op) |
---|
565 | n/a | { |
---|
566 | n/a | PyObject *t1; |
---|
567 | n/a | PyObject *t2; |
---|
568 | n/a | PyObject *res; |
---|
569 | n/a | |
---|
570 | n/a | if (!PySlice_Check(v) || !PySlice_Check(w)) |
---|
571 | n/a | Py_RETURN_NOTIMPLEMENTED; |
---|
572 | n/a | |
---|
573 | n/a | if (v == w) { |
---|
574 | n/a | /* XXX Do we really need this shortcut? |
---|
575 | n/a | There's a unit test for it, but is that fair? */ |
---|
576 | n/a | switch (op) { |
---|
577 | n/a | case Py_EQ: |
---|
578 | n/a | case Py_LE: |
---|
579 | n/a | case Py_GE: |
---|
580 | n/a | res = Py_True; |
---|
581 | n/a | break; |
---|
582 | n/a | default: |
---|
583 | n/a | res = Py_False; |
---|
584 | n/a | break; |
---|
585 | n/a | } |
---|
586 | n/a | Py_INCREF(res); |
---|
587 | n/a | return res; |
---|
588 | n/a | } |
---|
589 | n/a | |
---|
590 | n/a | t1 = PyTuple_New(3); |
---|
591 | n/a | if (t1 == NULL) |
---|
592 | n/a | return NULL; |
---|
593 | n/a | t2 = PyTuple_New(3); |
---|
594 | n/a | if (t2 == NULL) { |
---|
595 | n/a | Py_DECREF(t1); |
---|
596 | n/a | return NULL; |
---|
597 | n/a | } |
---|
598 | n/a | |
---|
599 | n/a | PyTuple_SET_ITEM(t1, 0, ((PySliceObject *)v)->start); |
---|
600 | n/a | PyTuple_SET_ITEM(t1, 1, ((PySliceObject *)v)->stop); |
---|
601 | n/a | PyTuple_SET_ITEM(t1, 2, ((PySliceObject *)v)->step); |
---|
602 | n/a | PyTuple_SET_ITEM(t2, 0, ((PySliceObject *)w)->start); |
---|
603 | n/a | PyTuple_SET_ITEM(t2, 1, ((PySliceObject *)w)->stop); |
---|
604 | n/a | PyTuple_SET_ITEM(t2, 2, ((PySliceObject *)w)->step); |
---|
605 | n/a | |
---|
606 | n/a | res = PyObject_RichCompare(t1, t2, op); |
---|
607 | n/a | |
---|
608 | n/a | PyTuple_SET_ITEM(t1, 0, NULL); |
---|
609 | n/a | PyTuple_SET_ITEM(t1, 1, NULL); |
---|
610 | n/a | PyTuple_SET_ITEM(t1, 2, NULL); |
---|
611 | n/a | PyTuple_SET_ITEM(t2, 0, NULL); |
---|
612 | n/a | PyTuple_SET_ITEM(t2, 1, NULL); |
---|
613 | n/a | PyTuple_SET_ITEM(t2, 2, NULL); |
---|
614 | n/a | |
---|
615 | n/a | Py_DECREF(t1); |
---|
616 | n/a | Py_DECREF(t2); |
---|
617 | n/a | |
---|
618 | n/a | return res; |
---|
619 | n/a | } |
---|
620 | n/a | |
---|
621 | n/a | static int |
---|
622 | n/a | slice_traverse(PySliceObject *v, visitproc visit, void *arg) |
---|
623 | n/a | { |
---|
624 | n/a | Py_VISIT(v->start); |
---|
625 | n/a | Py_VISIT(v->stop); |
---|
626 | n/a | Py_VISIT(v->step); |
---|
627 | n/a | return 0; |
---|
628 | n/a | } |
---|
629 | n/a | |
---|
630 | n/a | PyTypeObject PySlice_Type = { |
---|
631 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
---|
632 | n/a | "slice", /* Name of this type */ |
---|
633 | n/a | sizeof(PySliceObject), /* Basic object size */ |
---|
634 | n/a | 0, /* Item size for varobject */ |
---|
635 | n/a | (destructor)slice_dealloc, /* tp_dealloc */ |
---|
636 | n/a | 0, /* tp_print */ |
---|
637 | n/a | 0, /* tp_getattr */ |
---|
638 | n/a | 0, /* tp_setattr */ |
---|
639 | n/a | 0, /* tp_reserved */ |
---|
640 | n/a | (reprfunc)slice_repr, /* tp_repr */ |
---|
641 | n/a | 0, /* tp_as_number */ |
---|
642 | n/a | 0, /* tp_as_sequence */ |
---|
643 | n/a | 0, /* tp_as_mapping */ |
---|
644 | n/a | PyObject_HashNotImplemented, /* tp_hash */ |
---|
645 | n/a | 0, /* tp_call */ |
---|
646 | n/a | 0, /* tp_str */ |
---|
647 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
---|
648 | n/a | 0, /* tp_setattro */ |
---|
649 | n/a | 0, /* tp_as_buffer */ |
---|
650 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
---|
651 | n/a | slice_doc, /* tp_doc */ |
---|
652 | n/a | (traverseproc)slice_traverse, /* tp_traverse */ |
---|
653 | n/a | 0, /* tp_clear */ |
---|
654 | n/a | slice_richcompare, /* tp_richcompare */ |
---|
655 | n/a | 0, /* tp_weaklistoffset */ |
---|
656 | n/a | 0, /* tp_iter */ |
---|
657 | n/a | 0, /* tp_iternext */ |
---|
658 | n/a | slice_methods, /* tp_methods */ |
---|
659 | n/a | slice_members, /* tp_members */ |
---|
660 | n/a | 0, /* tp_getset */ |
---|
661 | n/a | 0, /* tp_base */ |
---|
662 | n/a | 0, /* tp_dict */ |
---|
663 | n/a | 0, /* tp_descr_get */ |
---|
664 | n/a | 0, /* tp_descr_set */ |
---|
665 | n/a | 0, /* tp_dictoffset */ |
---|
666 | n/a | 0, /* tp_init */ |
---|
667 | n/a | 0, /* tp_alloc */ |
---|
668 | n/a | slice_new, /* tp_new */ |
---|
669 | n/a | }; |
---|