2 * General object operations and protocol implementations,
3 * including their specialisations for certain builtins.
5 * Optional optimisations for builtins are in Optimize.c.
7 * Required replacements of builtins are in Builtins.c.
10 /////////////// RaiseNoneIterError.proto ///////////////
12 static CYTHON_INLINE
void __Pyx_RaiseNoneNotIterableError(void);
14 /////////////// RaiseNoneIterError ///////////////
16 static CYTHON_INLINE
void __Pyx_RaiseNoneNotIterableError(void) {
17 PyErr_SetString(PyExc_TypeError
, "'NoneType' object is not iterable");
20 /////////////// RaiseTooManyValuesToUnpack.proto ///////////////
22 static CYTHON_INLINE
void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected
);
24 /////////////// RaiseTooManyValuesToUnpack ///////////////
26 static CYTHON_INLINE
void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected
) {
27 PyErr_Format(PyExc_ValueError
,
28 "too many values to unpack (expected %" CYTHON_FORMAT_SSIZE_T
"d)", expected
);
31 /////////////// RaiseNeedMoreValuesToUnpack.proto ///////////////
33 static CYTHON_INLINE
void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index
);
35 /////////////// RaiseNeedMoreValuesToUnpack ///////////////
37 static CYTHON_INLINE
void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index
) {
38 PyErr_Format(PyExc_ValueError
,
39 "need more than %" CYTHON_FORMAT_SSIZE_T
"d value%.1s to unpack",
40 index
, (index
== 1) ? "" : "s");
43 /////////////// UnpackTupleError.proto ///////////////
45 static void __Pyx_UnpackTupleError(PyObject
*, Py_ssize_t index
); /*proto*/
47 /////////////// UnpackTupleError ///////////////
48 //@requires: RaiseNoneIterError
49 //@requires: RaiseNeedMoreValuesToUnpack
50 //@requires: RaiseTooManyValuesToUnpack
52 static void __Pyx_UnpackTupleError(PyObject
*t
, Py_ssize_t index
) {
54 __Pyx_RaiseNoneNotIterableError();
55 } else if (PyTuple_GET_SIZE(t
) < index
) {
56 __Pyx_RaiseNeedMoreValuesError(PyTuple_GET_SIZE(t
));
58 __Pyx_RaiseTooManyValuesError(index
);
62 /////////////// UnpackItemEndCheck.proto ///////////////
64 static int __Pyx_IternextUnpackEndCheck(PyObject
*retval
, Py_ssize_t expected
); /*proto*/
66 /////////////// UnpackItemEndCheck ///////////////
67 //@requires: RaiseTooManyValuesToUnpack
68 //@requires: IterFinish
70 static int __Pyx_IternextUnpackEndCheck(PyObject
*retval
, Py_ssize_t expected
) {
71 if (unlikely(retval
)) {
73 __Pyx_RaiseTooManyValuesError(expected
);
76 return __Pyx_IterFinish();
81 /////////////// UnpackTuple2.proto ///////////////
83 static CYTHON_INLINE
int __Pyx_unpack_tuple2(PyObject
* tuple
, PyObject
** value1
, PyObject
** value2
,
84 int is_tuple
, int has_known_size
, int decref_tuple
);
86 /////////////// UnpackTuple2 ///////////////
87 //@requires: UnpackItemEndCheck
88 //@requires: UnpackTupleError
89 //@requires: RaiseNeedMoreValuesToUnpack
91 static CYTHON_INLINE
int __Pyx_unpack_tuple2(PyObject
* tuple
, PyObject
** pvalue1
, PyObject
** pvalue2
,
92 int is_tuple
, int has_known_size
, int decref_tuple
) {
94 PyObject
*value1
= NULL
, *value2
= NULL
, *iter
= NULL
;
95 if (!is_tuple
&& unlikely(!PyTuple_Check(tuple
))) {
96 iternextfunc iternext
;
97 iter
= PyObject_GetIter(tuple
);
98 if (unlikely(!iter
)) goto bad
;
99 if (decref_tuple
) { Py_DECREF(tuple
); tuple
= NULL
; }
100 iternext
= Py_TYPE(iter
)->tp_iternext
;
101 value1
= iternext(iter
); if (unlikely(!value1
)) { index
= 0; goto unpacking_failed
; }
102 value2
= iternext(iter
); if (unlikely(!value2
)) { index
= 1; goto unpacking_failed
; }
103 if (!has_known_size
&& unlikely(__Pyx_IternextUnpackEndCheck(iternext(iter
), 2))) goto bad
;
106 if (!has_known_size
&& unlikely(PyTuple_GET_SIZE(tuple
) != 2)) {
107 __Pyx_UnpackTupleError(tuple
, 2);
110 #if CYTHON_COMPILING_IN_PYPY
111 value1
= PySequence_ITEM(tuple
, 0);
112 if (unlikely(!value1
)) goto bad
;
113 value2
= PySequence_ITEM(tuple
, 1);
114 if (unlikely(!value2
)) goto bad
;
116 value1
= PyTuple_GET_ITEM(tuple
, 0);
117 value2
= PyTuple_GET_ITEM(tuple
, 1);
121 if (decref_tuple
) { Py_DECREF(tuple
); }
127 if (!has_known_size
&& __Pyx_IterFinish() == 0)
128 __Pyx_RaiseNeedMoreValuesError(index
);
133 if (decref_tuple
) { Py_XDECREF(tuple
); }
137 /////////////// IterNext.proto ///////////////
139 #define __Pyx_PyIter_Next(obj) __Pyx_PyIter_Next2(obj, NULL)
140 static CYTHON_INLINE PyObject
*__Pyx_PyIter_Next2(PyObject
*, PyObject
*); /*proto*/
142 /////////////// IterNext ///////////////
144 // originally copied from Py3's builtin_next()
145 static CYTHON_INLINE PyObject
*__Pyx_PyIter_Next2(PyObject
* iterator
, PyObject
* defval
) {
147 iternextfunc iternext
= Py_TYPE(iterator
)->tp_iternext
;
148 #if CYTHON_COMPILING_IN_CPYTHON
149 if (unlikely(!iternext
)) {
151 if (unlikely(!iternext
) || unlikely(!PyIter_Check(iterator
))) {
153 PyErr_Format(PyExc_TypeError
,
154 "%.200s object is not an iterator", Py_TYPE(iterator
)->tp_name
);
157 next
= iternext(iterator
);
160 #if CYTHON_COMPILING_IN_CPYTHON
161 #if PY_VERSION_HEX >= 0x03010000 || (PY_MAJOR_VERSION < 3 && PY_VERSION_HEX >= 0x02070000)
162 if (unlikely(iternext
== &_PyObject_NextNotImplemented
))
167 PyObject
* exc_type
= PyErr_Occurred();
169 if (unlikely(exc_type
!= PyExc_StopIteration
) &&
170 !PyErr_GivenExceptionMatches(exc_type
, PyExc_StopIteration
))
177 if (!PyErr_Occurred())
178 PyErr_SetNone(PyExc_StopIteration
);
182 /////////////// IterFinish.proto ///////////////
184 static CYTHON_INLINE
int __Pyx_IterFinish(void); /*proto*/
186 /////////////// IterFinish ///////////////
188 // When PyIter_Next(iter) has returned NULL in order to signal termination,
189 // this function does the right cleanup and returns 0 on success. If it
190 // detects an error that occurred in the iterator, it returns -1.
192 static CYTHON_INLINE
int __Pyx_IterFinish(void) {
193 #if CYTHON_COMPILING_IN_CPYTHON
194 PyThreadState
*tstate
= PyThreadState_GET();
195 PyObject
* exc_type
= tstate
->curexc_type
;
196 if (unlikely(exc_type
)) {
197 if (likely(exc_type
== PyExc_StopIteration
) || PyErr_GivenExceptionMatches(exc_type
, PyExc_StopIteration
)) {
198 PyObject
*exc_value
, *exc_tb
;
199 exc_value
= tstate
->curexc_value
;
200 exc_tb
= tstate
->curexc_traceback
;
201 tstate
->curexc_type
= 0;
202 tstate
->curexc_value
= 0;
203 tstate
->curexc_traceback
= 0;
205 Py_XDECREF(exc_value
);
214 if (unlikely(PyErr_Occurred())) {
215 if (likely(PyErr_ExceptionMatches(PyExc_StopIteration
))) {
226 /////////////// DictGetItem.proto ///////////////
228 #if PY_MAJOR_VERSION >= 3
229 static PyObject
*__Pyx_PyDict_GetItem(PyObject
*d
, PyObject
* key
) {
231 value
= PyDict_GetItemWithError(d
, key
);
232 if (unlikely(!value
)) {
233 if (!PyErr_Occurred()) {
234 PyObject
* args
= PyTuple_Pack(1, key
);
236 PyErr_SetObject(PyExc_KeyError
, args
);
245 #define __Pyx_PyDict_GetItem(d, key) PyObject_GetItem(d, key)
248 /////////////// GetItemInt.proto ///////////////
250 #define __Pyx_GetItemInt(o, i, type, is_signed, to_py_func, is_list, wraparound, boundscheck) \
251 (__Pyx_fits_Py_ssize_t(i, type, is_signed) ? \
252 __Pyx_GetItemInt_Fast(o, (Py_ssize_t)i, is_list, wraparound, boundscheck) : \
253 (is_list ? (PyErr_SetString(PyExc_IndexError, "list index out of range"), (PyObject*)NULL) : \
254 __Pyx_GetItemInt_Generic(o, to_py_func(i))))
256 {{for type in
['List', 'Tuple']}}
257 #define __Pyx_GetItemInt_{{type}}(o, i, type, is_signed, to_py_func, is_list, wraparound, boundscheck) \
258 (__Pyx_fits_Py_ssize_t(i, type, is_signed) ? \
259 __Pyx_GetItemInt_{{type}}_Fast(o, (Py_ssize_t)i, wraparound, boundscheck) : \
260 (PyErr_SetString(PyExc_IndexError, "{{ type.lower() }} index out of range"), (PyObject*)NULL))
262 static CYTHON_INLINE PyObject
*__Pyx_GetItemInt_
{{type
}}_Fast(PyObject
*o
, Py_ssize_t i
,
263 int wraparound
, int boundscheck
);
266 static CYTHON_INLINE PyObject
*__Pyx_GetItemInt_Generic(PyObject
*o
, PyObject
* j
);
267 static CYTHON_INLINE PyObject
*__Pyx_GetItemInt_Fast(PyObject
*o
, Py_ssize_t i
,
268 int is_list
, int wraparound
, int boundscheck
);
270 /////////////// GetItemInt ///////////////
272 static CYTHON_INLINE PyObject
*__Pyx_GetItemInt_Generic(PyObject
*o
, PyObject
* j
) {
275 r
= PyObject_GetItem(o
, j
);
280 {{for type in
['List', 'Tuple']}}
281 static CYTHON_INLINE PyObject
*__Pyx_GetItemInt_
{{type
}}_Fast(PyObject
*o
, Py_ssize_t i
,
282 int wraparound
, int boundscheck
) {
283 #if CYTHON_COMPILING_IN_CPYTHON
284 if (wraparound
& unlikely(i
< 0)) i
+= Py
{{type
}}_GET_SIZE(o
);
285 if ((!boundscheck
) || likely((0 <= i
) & (i
< Py
{{type
}}_GET_SIZE(o
)))) {
286 PyObject
*r
= Py
{{type
}}_GET_ITEM(o
, i
);
290 return __Pyx_GetItemInt_Generic(o
, PyInt_FromSsize_t(i
));
292 return PySequence_GetItem(o
, i
);
297 static CYTHON_INLINE PyObject
*__Pyx_GetItemInt_Fast(PyObject
*o
, Py_ssize_t i
,
298 int is_list
, int wraparound
, int boundscheck
) {
299 #if CYTHON_COMPILING_IN_CPYTHON
300 if (is_list
|| PyList_CheckExact(o
)) {
301 Py_ssize_t n
= ((!wraparound
) | likely(i
>= 0)) ? i
: i
+ PyList_GET_SIZE(o
);
302 if ((!boundscheck
) || (likely((n
>= 0) & (n
< PyList_GET_SIZE(o
))))) {
303 PyObject
*r
= PyList_GET_ITEM(o
, n
);
308 else if (PyTuple_CheckExact(o
)) {
309 Py_ssize_t n
= ((!wraparound
) | likely(i
>= 0)) ? i
: i
+ PyTuple_GET_SIZE(o
);
310 if ((!boundscheck
) || likely((n
>= 0) & (n
< PyTuple_GET_SIZE(o
)))) {
311 PyObject
*r
= PyTuple_GET_ITEM(o
, n
);
316 // inlined PySequence_GetItem() + special cased length overflow
317 PySequenceMethods
*m
= Py_TYPE(o
)->tp_as_sequence
;
318 if (likely(m
&& m
->sq_item
)) {
319 if (wraparound
&& unlikely(i
< 0) && likely(m
->sq_length
)) {
320 Py_ssize_t l
= m
->sq_length(o
);
321 if (likely(l
>= 0)) {
324 // if length > max(Py_ssize_t), maybe the object can wrap around itself?
325 if (PyErr_ExceptionMatches(PyExc_OverflowError
))
331 return m
->sq_item(o
, i
);
335 if (is_list
|| PySequence_Check(o
)) {
336 return PySequence_GetItem(o
, i
);
339 return __Pyx_GetItemInt_Generic(o
, PyInt_FromSsize_t(i
));
342 /////////////// SetItemInt.proto ///////////////
344 #define __Pyx_SetItemInt(o, i, v, type, is_signed, to_py_func, is_list, wraparound, boundscheck) \
345 (__Pyx_fits_Py_ssize_t(i, type, is_signed) ? \
346 __Pyx_SetItemInt_Fast(o, (Py_ssize_t)i, v, is_list, wraparound, boundscheck) : \
347 (is_list ? (PyErr_SetString(PyExc_IndexError, "list assignment index out of range"), -1) : \
348 __Pyx_SetItemInt_Generic(o, to_py_func(i), v)))
350 static CYTHON_INLINE
int __Pyx_SetItemInt_Generic(PyObject
*o
, PyObject
*j
, PyObject
*v
);
351 static CYTHON_INLINE
int __Pyx_SetItemInt_Fast(PyObject
*o
, Py_ssize_t i
, PyObject
*v
,
352 int is_list
, int wraparound
, int boundscheck
);
354 /////////////// SetItemInt ///////////////
356 static CYTHON_INLINE
int __Pyx_SetItemInt_Generic(PyObject
*o
, PyObject
*j
, PyObject
*v
) {
359 r
= PyObject_SetItem(o
, j
, v
);
364 static CYTHON_INLINE
int __Pyx_SetItemInt_Fast(PyObject
*o
, Py_ssize_t i
, PyObject
*v
,
365 int is_list
, int wraparound
, int boundscheck
) {
366 #if CYTHON_COMPILING_IN_CPYTHON
367 if (is_list
|| PyList_CheckExact(o
)) {
368 Py_ssize_t n
= (!wraparound
) ? i
: ((likely(i
>= 0)) ? i
: i
+ PyList_GET_SIZE(o
));
369 if ((!boundscheck
) || likely((n
>= 0) & (n
< PyList_GET_SIZE(o
)))) {
370 PyObject
* old
= PyList_GET_ITEM(o
, n
);
372 PyList_SET_ITEM(o
, n
, v
);
377 // inlined PySequence_SetItem() + special cased length overflow
378 PySequenceMethods
*m
= Py_TYPE(o
)->tp_as_sequence
;
379 if (likely(m
&& m
->sq_ass_item
)) {
380 if (wraparound
&& unlikely(i
< 0) && likely(m
->sq_length
)) {
381 Py_ssize_t l
= m
->sq_length(o
);
382 if (likely(l
>= 0)) {
385 // if length > max(Py_ssize_t), maybe the object can wrap around itself?
386 if (PyErr_ExceptionMatches(PyExc_OverflowError
))
392 return m
->sq_ass_item(o
, i
, v
);
396 #if CYTHON_COMPILING_IN_PYPY
397 if (is_list
|| (PySequence_Check(o
) && !PyDict_Check(o
))) {
399 if (is_list
|| PySequence_Check(o
)) {
401 return PySequence_SetItem(o
, i
, v
);
404 return __Pyx_SetItemInt_Generic(o
, PyInt_FromSsize_t(i
), v
);
408 /////////////// DelItemInt.proto ///////////////
410 #define __Pyx_DelItemInt(o, i, type, is_signed, to_py_func, is_list, wraparound, boundscheck) \
411 (__Pyx_fits_Py_ssize_t(i, type, is_signed) ? \
412 __Pyx_DelItemInt_Fast(o, (Py_ssize_t)i, is_list, wraparound) : \
413 (is_list ? (PyErr_SetString(PyExc_IndexError, "list assignment index out of range"), -1) : \
414 __Pyx_DelItem_Generic(o, to_py_func(i))))
416 static CYTHON_INLINE
int __Pyx_DelItem_Generic(PyObject
*o
, PyObject
*j
);
417 static CYTHON_INLINE
int __Pyx_DelItemInt_Fast(PyObject
*o
, Py_ssize_t i
,
418 CYTHON_UNUSED
int is_list
, int wraparound
);
420 /////////////// DelItemInt ///////////////
422 static CYTHON_INLINE
int __Pyx_DelItem_Generic(PyObject
*o
, PyObject
*j
) {
425 r
= PyObject_DelItem(o
, j
);
430 static CYTHON_INLINE
int __Pyx_DelItemInt_Fast(PyObject
*o
, Py_ssize_t i
,
431 CYTHON_UNUSED
int is_list
, int wraparound
) {
432 #if CYTHON_COMPILING_IN_PYPY
433 if (is_list
|| PySequence_Check(o
)) {
434 return PySequence_DelItem(o
, i
);
437 // inlined PySequence_DelItem() + special cased length overflow
438 PySequenceMethods
*m
= Py_TYPE(o
)->tp_as_sequence
;
439 if (likely(m
&& m
->sq_ass_item
)) {
440 if (wraparound
&& unlikely(i
< 0) && likely(m
->sq_length
)) {
441 Py_ssize_t l
= m
->sq_length(o
);
442 if (likely(l
>= 0)) {
445 // if length > max(Py_ssize_t), maybe the object can wrap around itself?
446 if (PyErr_ExceptionMatches(PyExc_OverflowError
))
452 return m
->sq_ass_item(o
, i
, (PyObject
*)NULL
);
455 return __Pyx_DelItem_Generic(o
, PyInt_FromSsize_t(i
));
459 /////////////// SliceObject.proto ///////////////
461 // we pass pointer addresses to show the C compiler what is NULL and what isn't
462 {{if access
== 'Get'}}
463 static CYTHON_INLINE PyObject
* __Pyx_PyObject_GetSlice(
464 PyObject
* obj
, Py_ssize_t cstart
, Py_ssize_t cstop
,
465 PyObject
** py_start
, PyObject
** py_stop
, PyObject
** py_slice
,
466 int has_cstart
, int has_cstop
, int wraparound
);
468 #define __Pyx_PyObject_DelSlice(obj, cstart, cstop, py_start, py_stop, py_slice, has_cstart, has_cstop, wraparound) \
469 __Pyx_PyObject_SetSlice(obj, (PyObject*)NULL, cstart, cstop, py_start, py_stop, py_slice, has_cstart, has_cstop, wraparound)
471 // we pass pointer addresses to show the C compiler what is NULL and what isn't
472 static CYTHON_INLINE
int __Pyx_PyObject_SetSlice(
473 PyObject
* obj
, PyObject
* value
, Py_ssize_t cstart
, Py_ssize_t cstop
,
474 PyObject
** py_start
, PyObject
** py_stop
, PyObject
** py_slice
,
475 int has_cstart
, int has_cstop
, int wraparound
);
478 /////////////// SliceObject ///////////////
480 {{if access
== 'Get'}}
481 static CYTHON_INLINE PyObject
* __Pyx_PyObject_GetSlice(
482 PyObject
* obj
, Py_ssize_t cstart
, Py_ssize_t cstop
,
484 static CYTHON_INLINE
int __Pyx_PyObject_SetSlice(
485 PyObject
* obj
, PyObject
* value
, Py_ssize_t cstart
, Py_ssize_t cstop
,
487 PyObject
** _py_start
, PyObject
** _py_stop
, PyObject
** _py_slice
,
488 int has_cstart
, int has_cstop
, CYTHON_UNUSED
int wraparound
) {
489 #if CYTHON_COMPILING_IN_CPYTHON
490 PyMappingMethods
* mp
;
491 #if PY_MAJOR_VERSION < 3
492 PySequenceMethods
* ms
= Py_TYPE(obj
)->tp_as_sequence
;
493 if (likely(ms
&& ms
->sq_
{{if access
== 'Set'}}ass_
{{endif
}}slice
)) {
495 if (_py_start
&& (*_py_start
!= Py_None
)) {
496 cstart
= __Pyx_PyIndex_AsSsize_t(*_py_start
);
497 if ((cstart
== (Py_ssize_t
)-1) && PyErr_Occurred()) goto bad
;
502 if (_py_stop
&& (*_py_stop
!= Py_None
)) {
503 cstop
= __Pyx_PyIndex_AsSsize_t(*_py_stop
);
504 if ((cstop
== (Py_ssize_t
)-1) && PyErr_Occurred()) goto bad
;
506 cstop
= PY_SSIZE_T_MAX
;
508 if (wraparound
&& unlikely((cstart
< 0) | (cstop
< 0)) && likely(ms
->sq_length
)) {
509 Py_ssize_t l
= ms
->sq_length(obj
);
510 if (likely(l
>= 0)) {
513 if (cstop
< 0) cstop
= 0;
517 if (cstart
< 0) cstart
= 0;
520 // if length > max(Py_ssize_t), maybe the object can wrap around itself?
521 if (PyErr_ExceptionMatches(PyExc_OverflowError
))
527 {{if access
== 'Get'}}
528 return ms
->sq_slice(obj
, cstart
, cstop
);
530 return ms
->sq_ass_slice(obj
, cstart
, cstop
, value
);
535 mp
= Py_TYPE(obj
)->tp_as_mapping
;
536 {{if access
== 'Get'}}
537 if (likely(mp
&& mp
->mp_subscript
))
539 if (likely(mp
&& mp
->mp_ass_subscript
))
543 {{if access
== 'Get'}}PyObject
*{{else}}int{{endif
}} result
;
544 PyObject
*py_slice
, *py_start
, *py_stop
;
546 py_slice
= *_py_slice
;
548 PyObject
* owned_start
= NULL
;
549 PyObject
* owned_stop
= NULL
;
551 py_start
= *_py_start
;
554 owned_start
= py_start
= PyInt_FromSsize_t(cstart
);
555 if (unlikely(!py_start
)) goto bad
;
563 owned_stop
= py_stop
= PyInt_FromSsize_t(cstop
);
564 if (unlikely(!py_stop
)) {
565 Py_XDECREF(owned_start
);
571 py_slice
= PySlice_New(py_start
, py_stop
, Py_None
);
572 Py_XDECREF(owned_start
);
573 Py_XDECREF(owned_stop
);
574 if (unlikely(!py_slice
)) goto bad
;
576 #if CYTHON_COMPILING_IN_CPYTHON
577 {{if access
== 'Get'}}
578 result
= mp
->mp_subscript(obj
, py_slice
);
580 result
= PyObject_GetItem(obj
, py_slice
);
582 result
= mp
->mp_ass_subscript(obj
, py_slice
, value
);
584 result
= value
? PyObject_SetItem(obj
, py_slice
, value
) : PyObject_DelItem(obj
, py_slice
);
592 PyErr_Format(PyExc_TypeError
,
593 {{if access
== 'Get'}}
594 "'%.200s' object is unsliceable", Py_TYPE(obj
)->tp_name
);
596 "'%.200s' object does not support slice %.10s",
597 Py_TYPE(obj
)->tp_name
, value
? "assignment" : "deletion");
601 return {{if access
== 'Get'}}NULL
{{else}}-1{{endif
}};
605 /////////////// SliceTupleAndList.proto ///////////////
607 #if CYTHON_COMPILING_IN_CPYTHON
608 static CYTHON_INLINE PyObject
* __Pyx_PyList_GetSlice(PyObject
* src
, Py_ssize_t start
, Py_ssize_t stop
);
609 static CYTHON_INLINE PyObject
* __Pyx_PyTuple_GetSlice(PyObject
* src
, Py_ssize_t start
, Py_ssize_t stop
);
611 #define __Pyx_PyList_GetSlice(seq, start, stop) PySequence_GetSlice(seq, start, stop)
612 #define __Pyx_PyTuple_GetSlice(seq, start, stop) PySequence_GetSlice(seq, start, stop)
615 /////////////// SliceTupleAndList ///////////////
617 #if CYTHON_COMPILING_IN_CPYTHON
618 static CYTHON_INLINE
void __Pyx_crop_slice(Py_ssize_t
* _start
, Py_ssize_t
* _stop
, Py_ssize_t
* _length
) {
619 Py_ssize_t start
= *_start
, stop
= *_stop
, length
= *_length
;
628 else if (stop
> length
)
631 *_length
= stop
- start
;
636 static CYTHON_INLINE
void __Pyx_copy_object_array(PyObject
** CYTHON_RESTRICT src
, PyObject
** CYTHON_RESTRICT dest
, Py_ssize_t length
) {
639 for (i
= 0; i
< length
; i
++) {
640 v
= dest
[i
] = src
[i
];
645 {{for type in
['List', 'Tuple']}}
646 static CYTHON_INLINE PyObject
* __Pyx_Py
{{type
}}_GetSlice(
647 PyObject
* src
, Py_ssize_t start
, Py_ssize_t stop
) {
649 Py_ssize_t length
= Py
{{type
}}_GET_SIZE(src
);
650 __Pyx_crop_slice(&start
, &stop
, &length
);
651 if (unlikely(length
<= 0))
652 return Py
{{type
}}_New(0);
654 dest
= Py
{{type
}}_New(length
);
657 __Pyx_copy_object_array(
658 ((Py
{{type
}}Object
*)src
)->ob_item
+ start
,
659 ((Py
{{type
}}Object
*)dest
)->ob_item
,
667 /////////////// CalculateMetaclass.proto ///////////////
669 static PyObject
*__Pyx_CalculateMetaclass(PyTypeObject
*metaclass
, PyObject
*bases
);
671 /////////////// CalculateMetaclass ///////////////
673 static PyObject
*__Pyx_CalculateMetaclass(PyTypeObject
*metaclass
, PyObject
*bases
) {
674 Py_ssize_t i
, nbases
= PyTuple_GET_SIZE(bases
);
675 for (i
=0; i
< nbases
; i
++) {
676 PyTypeObject
*tmptype
;
677 PyObject
*tmp
= PyTuple_GET_ITEM(bases
, i
);
678 tmptype
= Py_TYPE(tmp
);
679 #if PY_MAJOR_VERSION < 3
680 if (tmptype
== &PyClass_Type
)
687 if (PyType_IsSubtype(metaclass
, tmptype
))
689 if (PyType_IsSubtype(tmptype
, metaclass
)) {
694 PyErr_SetString(PyExc_TypeError
,
695 "metaclass conflict: "
696 "the metaclass of a derived class "
697 "must be a (non-strict) subclass "
698 "of the metaclasses of all its bases");
702 #if PY_MAJOR_VERSION < 3
703 metaclass
= &PyClass_Type
;
705 metaclass
= &PyType_Type
;
708 // make owned reference
709 Py_INCREF((PyObject
*) metaclass
);
710 return (PyObject
*) metaclass
;
714 /////////////// FindInheritedMetaclass.proto ///////////////
716 static PyObject
*__Pyx_FindInheritedMetaclass(PyObject
*bases
); /*proto*/
718 /////////////// FindInheritedMetaclass ///////////////
719 //@requires: PyObjectGetAttrStr
720 //@requires: CalculateMetaclass
722 static PyObject
*__Pyx_FindInheritedMetaclass(PyObject
*bases
) {
724 if (PyTuple_Check(bases
) && PyTuple_GET_SIZE(bases
) > 0) {
725 PyTypeObject
*metatype
;
726 PyObject
*base
= PyTuple_GET_ITEM(bases
, 0);
727 #if PY_MAJOR_VERSION < 3
728 PyObject
* basetype
= __Pyx_PyObject_GetAttrStr(base
, PYIDENT("__class__"));
730 metatype
= (PyType_Check(basetype
)) ? ((PyTypeObject
*) basetype
) : NULL
;
733 metatype
= Py_TYPE(base
);
734 basetype
= (PyObject
*) metatype
;
738 metatype
= Py_TYPE(base
);
740 metaclass
= __Pyx_CalculateMetaclass(metatype
, bases
);
741 #if PY_MAJOR_VERSION < 3
745 // no bases => use default metaclass
746 #if PY_MAJOR_VERSION < 3
747 metaclass
= (PyObject
*) &PyClass_Type
;
749 metaclass
= (PyObject
*) &PyType_Type
;
751 Py_INCREF(metaclass
);
756 /////////////// Py3MetaclassGet.proto ///////////////
758 static PyObject
*__Pyx_Py3MetaclassGet(PyObject
*bases
, PyObject
*mkw
); /*proto*/
760 /////////////// Py3MetaclassGet ///////////////
761 //@requires: FindInheritedMetaclass
762 //@requires: CalculateMetaclass
764 static PyObject
*__Pyx_Py3MetaclassGet(PyObject
*bases
, PyObject
*mkw
) {
765 PyObject
*metaclass
= PyDict_GetItem(mkw
, PYIDENT("metaclass"));
767 Py_INCREF(metaclass
);
768 if (PyDict_DelItem(mkw
, PYIDENT("metaclass")) < 0) {
769 Py_DECREF(metaclass
);
772 if (PyType_Check(metaclass
)) {
773 PyObject
* orig
= metaclass
;
774 metaclass
= __Pyx_CalculateMetaclass((PyTypeObject
*) metaclass
, bases
);
779 return __Pyx_FindInheritedMetaclass(bases
);
782 /////////////// CreateClass.proto ///////////////
784 static PyObject
*__Pyx_CreateClass(PyObject
*bases
, PyObject
*dict
, PyObject
*name
,
785 PyObject
*qualname
, PyObject
*modname
); /*proto*/
787 /////////////// CreateClass ///////////////
788 //@requires: FindInheritedMetaclass
789 //@requires: CalculateMetaclass
791 static PyObject
*__Pyx_CreateClass(PyObject
*bases
, PyObject
*dict
, PyObject
*name
,
792 PyObject
*qualname
, PyObject
*modname
) {
796 if (PyDict_SetItem(dict
, PYIDENT("__module__"), modname
) < 0)
798 if (PyDict_SetItem(dict
, PYIDENT("__qualname__"), qualname
) < 0)
801 /* Python2 __metaclass__ */
802 metaclass
= PyDict_GetItem(dict
, PYIDENT("__metaclass__"));
804 Py_INCREF(metaclass
);
805 if (PyType_Check(metaclass
)) {
806 PyObject
* orig
= metaclass
;
807 metaclass
= __Pyx_CalculateMetaclass((PyTypeObject
*) metaclass
, bases
);
811 metaclass
= __Pyx_FindInheritedMetaclass(bases
);
813 if (unlikely(!metaclass
))
815 result
= PyObject_CallFunctionObjArgs(metaclass
, name
, bases
, dict
, NULL
);
816 Py_DECREF(metaclass
);
820 /////////////// Py3ClassCreate.proto ///////////////
822 static PyObject
*__Pyx_Py3MetaclassPrepare(PyObject
*metaclass
, PyObject
*bases
, PyObject
*name
, PyObject
*qualname
,
823 PyObject
*mkw
, PyObject
*modname
, PyObject
*doc
); /*proto*/
824 static PyObject
*__Pyx_Py3ClassCreate(PyObject
*metaclass
, PyObject
*name
, PyObject
*bases
, PyObject
*dict
,
825 PyObject
*mkw
, int calculate_metaclass
, int allow_py2_metaclass
); /*proto*/
827 /////////////// Py3ClassCreate ///////////////
828 //@requires: PyObjectGetAttrStr
829 //@requires: CalculateMetaclass
831 static PyObject
*__Pyx_Py3MetaclassPrepare(PyObject
*metaclass
, PyObject
*bases
, PyObject
*name
,
832 PyObject
*qualname
, PyObject
*mkw
, PyObject
*modname
, PyObject
*doc
) {
835 PyObject
*prep
= __Pyx_PyObject_GetAttrStr(metaclass
, PYIDENT("__prepare__"));
837 PyObject
*pargs
= PyTuple_Pack(2, name
, bases
);
838 if (unlikely(!pargs
)) {
842 ns
= PyObject_Call(prep
, pargs
, mkw
);
846 if (unlikely(!PyErr_ExceptionMatches(PyExc_AttributeError
)))
858 /* Required here to emulate assignment order */
859 if (unlikely(PyObject_SetItem(ns
, PYIDENT("__module__"), modname
) < 0)) goto bad
;
860 if (unlikely(PyObject_SetItem(ns
, PYIDENT("__qualname__"), qualname
) < 0)) goto bad
;
861 if (unlikely(doc
&& PyObject_SetItem(ns
, PYIDENT("__doc__"), doc
) < 0)) goto bad
;
868 static PyObject
*__Pyx_Py3ClassCreate(PyObject
*metaclass
, PyObject
*name
, PyObject
*bases
,
869 PyObject
*dict
, PyObject
*mkw
,
870 int calculate_metaclass
, int allow_py2_metaclass
) {
871 PyObject
*result
, *margs
;
872 PyObject
*owned_metaclass
= NULL
;
873 if (allow_py2_metaclass
) {
874 /* honour Python2 __metaclass__ for backward compatibility */
875 owned_metaclass
= PyObject_GetItem(dict
, PYIDENT("__metaclass__"));
876 if (owned_metaclass
) {
877 metaclass
= owned_metaclass
;
878 } else if (likely(PyErr_ExceptionMatches(PyExc_KeyError
))) {
884 if (calculate_metaclass
&& (!metaclass
|| PyType_Check(metaclass
))) {
885 metaclass
= __Pyx_CalculateMetaclass((PyTypeObject
*) metaclass
, bases
);
886 Py_XDECREF(owned_metaclass
);
887 if (unlikely(!metaclass
))
889 owned_metaclass
= metaclass
;
891 margs
= PyTuple_Pack(3, name
, bases
, dict
);
892 if (unlikely(!margs
)) {
895 result
= PyObject_Call(metaclass
, margs
, mkw
);
898 Py_XDECREF(owned_metaclass
);
902 /////////////// ExtTypeTest.proto ///////////////
904 static CYTHON_INLINE
int __Pyx_TypeTest(PyObject
*obj
, PyTypeObject
*type
); /*proto*/
906 /////////////// ExtTypeTest ///////////////
908 static CYTHON_INLINE
int __Pyx_TypeTest(PyObject
*obj
, PyTypeObject
*type
) {
909 if (unlikely(!type
)) {
910 PyErr_SetString(PyExc_SystemError
, "Missing type object");
913 if (likely(PyObject_TypeCheck(obj
, type
)))
915 PyErr_Format(PyExc_TypeError
, "Cannot convert %.200s to %.200s",
916 Py_TYPE(obj
)->tp_name
, type
->tp_name
);
920 /////////////// CallableCheck.proto ///////////////
922 #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
923 #define __Pyx_PyCallable_Check(obj) ((obj)->ob_type->tp_call != NULL)
925 #define __Pyx_PyCallable_Check(obj) PyCallable_Check(obj)
928 /////////////// PyDictContains.proto ///////////////
930 static CYTHON_INLINE
int __Pyx_PyDict_Contains(PyObject
* item
, PyObject
* dict
, int eq
) {
931 int result
= PyDict_Contains(dict
, item
);
932 return unlikely(result
< 0) ? result
: (result
== (eq
== Py_EQ
));
935 /////////////// PySequenceContains.proto ///////////////
937 static CYTHON_INLINE
int __Pyx_PySequence_Contains(PyObject
* item
, PyObject
* seq
, int eq
) {
938 int result
= PySequence_Contains(seq
, item
);
939 return unlikely(result
< 0) ? result
: (result
== (eq
== Py_EQ
));
942 /////////////// PyBoolOrNullFromLong.proto ///////////////
944 static CYTHON_INLINE PyObject
* __Pyx_PyBoolOrNull_FromLong(long b
) {
945 return unlikely(b
< 0) ? NULL
: __Pyx_PyBool_FromLong(b
);
948 /////////////// GetBuiltinName.proto ///////////////
950 static PyObject
*__Pyx_GetBuiltinName(PyObject
*name
); /*proto*/
952 /////////////// GetBuiltinName ///////////////
953 //@requires: PyObjectGetAttrStr
954 //@substitute: naming
956 static PyObject
*__Pyx_GetBuiltinName(PyObject
*name
) {
957 PyObject
* result
= __Pyx_PyObject_GetAttrStr($builtins_cname
, name
);
958 if (unlikely(!result
)) {
959 PyErr_Format(PyExc_NameError
,
960 #if PY_MAJOR_VERSION >= 3
961 "name '%U' is not defined", name
);
963 "name '%.200s' is not defined", PyString_AS_STRING(name
));
969 /////////////// GetNameInClass.proto ///////////////
971 static PyObject
*__Pyx_GetNameInClass(PyObject
*nmspace
, PyObject
*name
); /*proto*/
973 /////////////// GetNameInClass ///////////////
974 //@requires: PyObjectGetAttrStr
975 //@requires: GetModuleGlobalName
977 static PyObject
*__Pyx_GetNameInClass(PyObject
*nmspace
, PyObject
*name
) {
979 result
= __Pyx_PyObject_GetAttrStr(nmspace
, name
);
981 result
= __Pyx_GetModuleGlobalName(name
);
985 /////////////// GetModuleGlobalName.proto ///////////////
987 static CYTHON_INLINE PyObject
*__Pyx_GetModuleGlobalName(PyObject
*name
); /*proto*/
989 /////////////// GetModuleGlobalName ///////////////
990 //@requires: GetBuiltinName
991 //@substitute: naming
993 static CYTHON_INLINE PyObject
*__Pyx_GetModuleGlobalName(PyObject
*name
) {
995 #if CYTHON_COMPILING_IN_CPYTHON
996 result
= PyDict_GetItem($moddict_cname
, name
);
1001 result
= PyObject_GetItem($moddict_cname
, name
);
1005 result
= __Pyx_GetBuiltinName(name
);
1010 //////////////////// GetAttr.proto ////////////////////
1012 static CYTHON_INLINE PyObject
*__Pyx_GetAttr(PyObject
*, PyObject
*); /*proto*/
1014 //////////////////// GetAttr ////////////////////
1015 //@requires: PyObjectGetAttrStr
1017 static CYTHON_INLINE PyObject
*__Pyx_GetAttr(PyObject
*o
, PyObject
*n
) {
1018 #if CYTHON_COMPILING_IN_CPYTHON
1019 #if PY_MAJOR_VERSION >= 3
1020 if (likely(PyUnicode_Check(n
)))
1022 if (likely(PyString_Check(n
)))
1024 return __Pyx_PyObject_GetAttrStr(o
, n
);
1026 return PyObject_GetAttr(o
, n
);
1029 /////////////// PyObjectLookupSpecial.proto ///////////////
1030 //@requires: PyObjectGetAttrStr
1032 #if CYTHON_COMPILING_IN_CPYTHON && (PY_VERSION_HEX >= 0x03020000 || PY_MAJOR_VERSION < 3 && PY_VERSION_HEX >= 0x02070000)
1033 // looks like calling _PyType_Lookup() isn't safe in Py<=2.6/3.1
1034 static CYTHON_INLINE PyObject
* __Pyx_PyObject_LookupSpecial(PyObject
* obj
, PyObject
* attr_name
) {
1036 PyTypeObject
*tp
= Py_TYPE(obj
);
1037 #if PY_MAJOR_VERSION < 3
1038 if (unlikely(PyInstance_Check(obj
)))
1039 return __Pyx_PyObject_GetAttrStr(obj
, attr_name
);
1041 // adapted from CPython's special_lookup() in ceval.c
1042 res
= _PyType_Lookup(tp
, attr_name
);
1044 descrgetfunc f
= Py_TYPE(res
)->tp_descr_get
;
1048 res
= f(res
, obj
, (PyObject
*)tp
);
1051 PyErr_SetObject(PyExc_AttributeError
, attr_name
);
1056 #define __Pyx_PyObject_LookupSpecial(o,n) __Pyx_PyObject_GetAttrStr(o,n)
1059 /////////////// PyObjectGetAttrStr.proto ///////////////
1061 #if CYTHON_COMPILING_IN_CPYTHON
1062 static CYTHON_INLINE PyObject
* __Pyx_PyObject_GetAttrStr(PyObject
* obj
, PyObject
* attr_name
) {
1063 PyTypeObject
* tp
= Py_TYPE(obj
);
1064 if (likely(tp
->tp_getattro
))
1065 return tp
->tp_getattro(obj
, attr_name
);
1066 #if PY_MAJOR_VERSION < 3
1067 if (likely(tp
->tp_getattr
))
1068 return tp
->tp_getattr(obj
, PyString_AS_STRING(attr_name
));
1070 return PyObject_GetAttr(obj
, attr_name
);
1073 #define __Pyx_PyObject_GetAttrStr(o,n) PyObject_GetAttr(o,n)
1076 /////////////// PyObjectSetAttrStr.proto ///////////////
1078 #if CYTHON_COMPILING_IN_CPYTHON
1079 #define __Pyx_PyObject_DelAttrStr(o,n) __Pyx_PyObject_SetAttrStr(o,n,NULL)
1080 static CYTHON_INLINE
int __Pyx_PyObject_SetAttrStr(PyObject
* obj
, PyObject
* attr_name
, PyObject
* value
) {
1081 PyTypeObject
* tp
= Py_TYPE(obj
);
1082 if (likely(tp
->tp_setattro
))
1083 return tp
->tp_setattro(obj
, attr_name
, value
);
1084 #if PY_MAJOR_VERSION < 3
1085 if (likely(tp
->tp_setattr
))
1086 return tp
->tp_setattr(obj
, PyString_AS_STRING(attr_name
), value
);
1088 return PyObject_SetAttr(obj
, attr_name
, value
);
1091 #define __Pyx_PyObject_DelAttrStr(o,n) PyObject_DelAttr(o,n)
1092 #define __Pyx_PyObject_SetAttrStr(o,n,v) PyObject_SetAttr(o,n,v)
1095 /////////////// PyObjectCallMethod.proto ///////////////
1096 //@requires: PyObjectGetAttrStr
1097 //@requires: PyObjectCall
1098 //@substitute: naming
1100 static PyObject
* __Pyx_PyObject_CallMethodTuple(PyObject
* obj
, PyObject
* method_name
, PyObject
* args
) {
1101 PyObject
*method
, *result
= NULL
;
1102 if (unlikely(!args
)) return NULL
;
1103 method
= __Pyx_PyObject_GetAttrStr(obj
, method_name
);
1104 if (unlikely(!method
)) goto bad
;
1105 result
= __Pyx_PyObject_Call(method
, args
, NULL
);
1112 #define __Pyx_PyObject_CallMethod3(obj, name, arg1, arg2, arg3) \
1113 __Pyx_PyObject_CallMethodTuple(obj, name, PyTuple_Pack(3, arg1, arg2, arg3))
1114 #define __Pyx_PyObject_CallMethod2(obj, name, arg1, arg2) \
1115 __Pyx_PyObject_CallMethodTuple(obj, name, PyTuple_Pack(2, arg1, arg2))
1116 #define __Pyx_PyObject_CallMethod1(obj, name, arg1) \
1117 __Pyx_PyObject_CallMethodTuple(obj, name, PyTuple_Pack(1, arg1))
1118 #define __Pyx_PyObject_CallMethod0(obj, name) \
1119 __Pyx_PyObject_CallMethodTuple(obj, name, (Py_INCREF($empty_tuple), $empty_tuple))
1122 /////////////// tp_new.proto ///////////////
1124 #define __Pyx_tp_new(type_obj, args) __Pyx_tp_new_kwargs(type_obj, args, NULL)
1125 static CYTHON_INLINE PyObject
* __Pyx_tp_new_kwargs(PyObject
* type_obj
, PyObject
* args
, PyObject
* kwargs
) {
1126 return (PyObject
*) (((PyTypeObject
*)type_obj
)->tp_new((PyTypeObject
*)type_obj
, args
, kwargs
));
1130 /////////////// PyObjectCall.proto ///////////////
1132 #if CYTHON_COMPILING_IN_CPYTHON
1133 static CYTHON_INLINE PyObject
* __Pyx_PyObject_Call(PyObject
*func
, PyObject
*arg
, PyObject
*kw
); /*proto*/
1135 #define __Pyx_PyObject_Call(func, arg, kw) PyObject_Call(func, arg, kw)
1138 /////////////// PyObjectCall ///////////////
1140 #if CYTHON_COMPILING_IN_CPYTHON
1141 static CYTHON_INLINE PyObject
* __Pyx_PyObject_Call(PyObject
*func
, PyObject
*arg
, PyObject
*kw
) {
1143 ternaryfunc call
= func
->ob_type
->tp_call
;
1145 if (unlikely(!call
))
1146 return PyObject_Call(func
, arg
, kw
);
1147 #if PY_VERSION_HEX >= 0x02060000
1148 if (unlikely(Py_EnterRecursiveCall((char*)" while calling a Python object")))
1151 result
= (*call
)(func
, arg
, kw
);
1152 #if PY_VERSION_HEX >= 0x02060000
1153 Py_LeaveRecursiveCall();
1155 if (unlikely(!result
) && unlikely(!PyErr_Occurred())) {
1158 "NULL result without error in PyObject_Call");