ObjectHandling.c 49 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * General object operations and protocol implementations,
 * including their specialisations for certain builtins.
 *
 * Optional optimisations for builtins are in Optimize.c.
 *
 * Required replacements of builtins are in Builtins.c.
 */

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/////////////// RaiseNoneIterError.proto ///////////////

static CYTHON_INLINE void __Pyx_RaiseNoneNotIterableError(void);

/////////////// RaiseNoneIterError ///////////////

static CYTHON_INLINE void __Pyx_RaiseNoneNotIterableError(void) {
    PyErr_SetString(PyExc_TypeError, "'NoneType' object is not iterable");
}

/////////////// RaiseTooManyValuesToUnpack.proto ///////////////

static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected);

/////////////// RaiseTooManyValuesToUnpack ///////////////

static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected) {
27 28
    PyErr_Format(PyExc_ValueError,
                 "too many values to unpack (expected %" CYTHON_FORMAT_SSIZE_T "d)", expected);
29 30 31 32 33 34 35 36 37
}

/////////////// RaiseNeedMoreValuesToUnpack.proto ///////////////

static CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index);

/////////////// RaiseNeedMoreValuesToUnpack ///////////////

static CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index) {
38 39
    PyErr_Format(PyExc_ValueError,
                 "need more than %" CYTHON_FORMAT_SSIZE_T "d value%.1s to unpack",
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
                 index, (index == 1) ? "" : "s");
}

/////////////// UnpackTupleError.proto ///////////////

static void __Pyx_UnpackTupleError(PyObject *, Py_ssize_t index); /*proto*/

/////////////// UnpackTupleError ///////////////
//@requires: RaiseNoneIterError
//@requires: RaiseNeedMoreValuesToUnpack
//@requires: RaiseTooManyValuesToUnpack

static void __Pyx_UnpackTupleError(PyObject *t, Py_ssize_t index) {
    if (t == Py_None) {
      __Pyx_RaiseNoneNotIterableError();
    } else if (PyTuple_GET_SIZE(t) < index) {
      __Pyx_RaiseNeedMoreValuesError(PyTuple_GET_SIZE(t));
    } else {
      __Pyx_RaiseTooManyValuesError(index);
    }
}

/////////////// UnpackItemEndCheck.proto ///////////////

static int __Pyx_IternextUnpackEndCheck(PyObject *retval, Py_ssize_t expected); /*proto*/

/////////////// UnpackItemEndCheck ///////////////
//@requires: RaiseTooManyValuesToUnpack
68
//@requires: IterFinish
69 70 71 72 73 74

static int __Pyx_IternextUnpackEndCheck(PyObject *retval, Py_ssize_t expected) {
    if (unlikely(retval)) {
        Py_DECREF(retval);
        __Pyx_RaiseTooManyValuesError(expected);
        return -1;
75 76
    } else {
        return __Pyx_IterFinish();
77 78 79 80 81 82 83 84 85 86 87 88
    }
    return 0;
}

/////////////// UnpackTuple2.proto ///////////////

static CYTHON_INLINE int __Pyx_unpack_tuple2(PyObject* tuple, PyObject** value1, PyObject** value2,
                                             int is_tuple, int has_known_size, int decref_tuple);

/////////////// UnpackTuple2 ///////////////
//@requires: UnpackItemEndCheck
//@requires: UnpackTupleError
89
//@requires: RaiseNeedMoreValuesToUnpack
90 91 92

static CYTHON_INLINE int __Pyx_unpack_tuple2(PyObject* tuple, PyObject** pvalue1, PyObject** pvalue2,
                                             int is_tuple, int has_known_size, int decref_tuple) {
93
    Py_ssize_t index;
94 95
    PyObject *value1 = NULL, *value2 = NULL, *iter = NULL;
    if (!is_tuple && unlikely(!PyTuple_Check(tuple))) {
96
        iternextfunc iternext;
97 98
        iter = PyObject_GetIter(tuple);
        if (unlikely(!iter)) goto bad;
99
        if (decref_tuple) { Py_DECREF(tuple); tuple = NULL; }
100 101 102
        iternext = Py_TYPE(iter)->tp_iternext;
        value1 = iternext(iter); if (unlikely(!value1)) { index = 0; goto unpacking_failed; }
        value2 = iternext(iter); if (unlikely(!value2)) { index = 1; goto unpacking_failed; }
103
        if (!has_known_size && unlikely(__Pyx_IternextUnpackEndCheck(iternext(iter), 2))) goto bad;
104 105 106 107 108 109
        Py_DECREF(iter);
    } else {
        if (!has_known_size && unlikely(PyTuple_GET_SIZE(tuple) != 2)) {
            __Pyx_UnpackTupleError(tuple, 2);
            goto bad;
        }
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 115
        if (unlikely(!value2)) goto bad;
#else
116 117
        value1 = PyTuple_GET_ITEM(tuple, 0);
        value2 = PyTuple_GET_ITEM(tuple, 1);
118 119
        Py_INCREF(value1);
        Py_INCREF(value2);
120
#endif
121
        if (decref_tuple) { Py_DECREF(tuple); }
122 123 124 125
    }
    *pvalue1 = value1;
    *pvalue2 = value2;
    return 0;
126 127 128
unpacking_failed:
    if (!has_known_size && __Pyx_IterFinish() == 0)
        __Pyx_RaiseNeedMoreValuesError(index);
129 130 131 132
bad:
    Py_XDECREF(iter);
    Py_XDECREF(value1);
    Py_XDECREF(value2);
133
    if (decref_tuple) { Py_XDECREF(tuple); }
134 135
    return -1;
}
136

137 138
/////////////// IterNext.proto ///////////////

139
#define __Pyx_PyIter_Next(obj) __Pyx_PyIter_Next2(obj, NULL)
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
static CYTHON_INLINE PyObject *__Pyx_PyIter_Next2(PyObject *, PyObject *); /*proto*/

/////////////// IterNext ///////////////

// originally copied from Py3's builtin_next()
static CYTHON_INLINE PyObject *__Pyx_PyIter_Next2(PyObject* iterator, PyObject* defval) {
    PyObject* next;
    iternextfunc iternext = Py_TYPE(iterator)->tp_iternext;
#if CYTHON_COMPILING_IN_CPYTHON
    if (unlikely(!iternext)) {
#else
    if (unlikely(!iternext) || unlikely(!PyIter_Check(iterator))) {
#endif
        PyErr_Format(PyExc_TypeError,
            "%.200s object is not an iterator", Py_TYPE(iterator)->tp_name);
        return NULL;
    }
    next = iternext(iterator);
    if (likely(next))
        return next;
#if CYTHON_COMPILING_IN_CPYTHON
161
#if PY_VERSION_HEX >= 0x02070000
162
    if (unlikely(iternext == &_PyObject_NextNotImplemented))
163 164 165
        return NULL;
#endif
#endif
166
    if (defval) {
167 168 169 170 171 172 173 174 175 176
        PyObject* exc_type = PyErr_Occurred();
        if (exc_type) {
            if (unlikely(exc_type != PyExc_StopIteration) &&
                    !PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))
                return NULL;
            PyErr_Clear();
        }
        Py_INCREF(defval);
        return defval;
    }
177 178 179
    if (!PyErr_Occurred())
        PyErr_SetNone(PyExc_StopIteration);
    return NULL;
180 181
}

182 183
/////////////// IterFinish.proto ///////////////

184
static CYTHON_INLINE int __Pyx_IterFinish(void); /*proto*/
185 186 187 188 189 190 191

/////////////// IterFinish ///////////////

// When PyIter_Next(iter) has returned NULL in order to signal termination,
// this function does the right cleanup and returns 0 on success.  If it
// detects an error that occurred in the iterator, it returns -1.

192
static CYTHON_INLINE int __Pyx_IterFinish(void) {
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
#if CYTHON_COMPILING_IN_CPYTHON
    PyThreadState *tstate = PyThreadState_GET();
    PyObject* exc_type = tstate->curexc_type;
    if (unlikely(exc_type)) {
        if (likely(exc_type == PyExc_StopIteration) || PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration)) {
            PyObject *exc_value, *exc_tb;
            exc_value = tstate->curexc_value;
            exc_tb = tstate->curexc_traceback;
            tstate->curexc_type = 0;
            tstate->curexc_value = 0;
            tstate->curexc_traceback = 0;
            Py_DECREF(exc_type);
            Py_XDECREF(exc_value);
            Py_XDECREF(exc_tb);
            return 0;
        } else {
            return -1;
        }
    }
    return 0;
#else
214 215 216 217 218 219 220 221 222
    if (unlikely(PyErr_Occurred())) {
        if (likely(PyErr_ExceptionMatches(PyExc_StopIteration))) {
            PyErr_Clear();
            return 0;
        } else {
            return -1;
        }
    }
    return 0;
223
#endif
224
}
225

226 227
/////////////// DictGetItem.proto ///////////////

228
#if PY_MAJOR_VERSION >= 3 && !CYTHON_COMPILING_IN_PYPY
229 230 231 232
static PyObject *__Pyx_PyDict_GetItem(PyObject *d, PyObject* key) {
    PyObject *value;
    value = PyDict_GetItemWithError(d, key);
    if (unlikely(!value)) {
233 234 235 236 237 238
        if (!PyErr_Occurred()) {
            PyObject* args = PyTuple_Pack(1, key);
            if (likely(args))
                PyErr_SetObject(PyExc_KeyError, args);
            Py_XDECREF(args);
        }
239 240 241 242 243 244 245 246 247
        return NULL;
    }
    Py_INCREF(value);
    return value;
}
#else
    #define __Pyx_PyDict_GetItem(d, key) PyObject_GetItem(d, key)
#endif

248 249
/////////////// GetItemInt.proto ///////////////

250 251 252
#define __Pyx_GetItemInt(o, i, type, is_signed, to_py_func, is_list, wraparound, boundscheck) \
    (__Pyx_fits_Py_ssize_t(i, type, is_signed) ? \
    __Pyx_GetItemInt_Fast(o, (Py_ssize_t)i, is_list, wraparound, boundscheck) : \
Stefan Behnel's avatar
Stefan Behnel committed
253
    (is_list ? (PyErr_SetString(PyExc_IndexError, "list index out of range"), (PyObject*)NULL) : \
254
               __Pyx_GetItemInt_Generic(o, to_py_func(i))))
255 256

{{for type in ['List', 'Tuple']}}
257 258 259
#define __Pyx_GetItemInt_{{type}}(o, i, type, is_signed, to_py_func, is_list, wraparound, boundscheck) \
    (__Pyx_fits_Py_ssize_t(i, type, is_signed) ? \
    __Pyx_GetItemInt_{{type}}_Fast(o, (Py_ssize_t)i, wraparound, boundscheck) : \
Stefan Behnel's avatar
Stefan Behnel committed
260
    (PyErr_SetString(PyExc_IndexError, "{{ type.lower() }} index out of range"), (PyObject*)NULL))
261 262 263 264 265 266 267 268 269 270 271

static CYTHON_INLINE PyObject *__Pyx_GetItemInt_{{type}}_Fast(PyObject *o, Py_ssize_t i,
                                                              int wraparound, int boundscheck);
{{endfor}}

static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Generic(PyObject *o, PyObject* j);
static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i,
                                                     int is_list, int wraparound, int boundscheck);

/////////////// GetItemInt ///////////////

272 273 274 275 276 277 278 279 280
static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Generic(PyObject *o, PyObject* j) {
    PyObject *r;
    if (!j) return NULL;
    r = PyObject_GetItem(o, j);
    Py_DECREF(j);
    return r;
}

{{for type in ['List', 'Tuple']}}
281
static CYTHON_INLINE PyObject *__Pyx_GetItemInt_{{type}}_Fast(PyObject *o, Py_ssize_t i,
282 283
                                                              CYTHON_NCP_UNUSED int wraparound,
                                                              CYTHON_NCP_UNUSED int boundscheck) {
284
#if CYTHON_COMPILING_IN_CPYTHON
285 286
    if (wraparound & unlikely(i < 0)) i += Py{{type}}_GET_SIZE(o);
    if ((!boundscheck) || likely((0 <= i) & (i < Py{{type}}_GET_SIZE(o)))) {
287 288 289 290 291 292 293 294 295 296 297
        PyObject *r = Py{{type}}_GET_ITEM(o, i);
        Py_INCREF(r);
        return r;
    }
    return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i));
#else
    return PySequence_GetItem(o, i);
#endif
}
{{endfor}}

298 299 300
static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i, int is_list,
                                                     CYTHON_NCP_UNUSED int wraparound,
                                                     CYTHON_NCP_UNUSED int boundscheck) {
301
#if CYTHON_COMPILING_IN_CPYTHON
302 303 304
    if (is_list || PyList_CheckExact(o)) {
        Py_ssize_t n = ((!wraparound) | likely(i >= 0)) ? i : i + PyList_GET_SIZE(o);
        if ((!boundscheck) || (likely((n >= 0) & (n < PyList_GET_SIZE(o))))) {
305 306 307 308 309 310
            PyObject *r = PyList_GET_ITEM(o, n);
            Py_INCREF(r);
            return r;
        }
    }
    else if (PyTuple_CheckExact(o)) {
311 312
        Py_ssize_t n = ((!wraparound) | likely(i >= 0)) ? i : i + PyTuple_GET_SIZE(o);
        if ((!boundscheck) || likely((n >= 0) & (n < PyTuple_GET_SIZE(o)))) {
313 314 315 316
            PyObject *r = PyTuple_GET_ITEM(o, n);
            Py_INCREF(r);
            return r;
        }
317 318
    } else {
        // inlined PySequence_GetItem() + special cased length overflow
319 320
        PySequenceMethods *m = Py_TYPE(o)->tp_as_sequence;
        if (likely(m && m->sq_item)) {
321
            if (wraparound && unlikely(i < 0) && likely(m->sq_length)) {
322
                Py_ssize_t l = m->sq_length(o);
323 324 325 326 327 328 329 330 331
                if (likely(l >= 0)) {
                    i += l;
                } else {
                    // if length > max(Py_ssize_t), maybe the object can wrap around itself?
                    if (PyErr_ExceptionMatches(PyExc_OverflowError))
                        PyErr_Clear();
                    else
                        return NULL;
                }
332 333 334 335 336
            }
            return m->sq_item(o, i);
        }
    }
#else
337
    if (is_list || PySequence_Check(o)) {
338 339 340 341 342 343
        return PySequence_GetItem(o, i);
    }
#endif
    return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i));
}

344 345
/////////////// SetItemInt.proto ///////////////

346 347 348
#define __Pyx_SetItemInt(o, i, v, type, is_signed, to_py_func, is_list, wraparound, boundscheck) \
    (__Pyx_fits_Py_ssize_t(i, type, is_signed) ? \
    __Pyx_SetItemInt_Fast(o, (Py_ssize_t)i, v, is_list, wraparound, boundscheck) : \
349 350
    (is_list ? (PyErr_SetString(PyExc_IndexError, "list assignment index out of range"), -1) : \
               __Pyx_SetItemInt_Generic(o, to_py_func(i), v)))
351

352 353 354 355 356 357
static CYTHON_INLINE int __Pyx_SetItemInt_Generic(PyObject *o, PyObject *j, PyObject *v);
static CYTHON_INLINE int __Pyx_SetItemInt_Fast(PyObject *o, Py_ssize_t i, PyObject *v,
                                               int is_list, int wraparound, int boundscheck);

/////////////// SetItemInt ///////////////

358 359 360 361 362 363 364 365
static CYTHON_INLINE int __Pyx_SetItemInt_Generic(PyObject *o, PyObject *j, PyObject *v) {
    int r;
    if (!j) return -1;
    r = PyObject_SetItem(o, j, v);
    Py_DECREF(j);
    return r;
}

366 367
static CYTHON_INLINE int __Pyx_SetItemInt_Fast(PyObject *o, Py_ssize_t i, PyObject *v, int is_list,
                                               CYTHON_NCP_UNUSED int wraparound, CYTHON_NCP_UNUSED int boundscheck) {
368
#if CYTHON_COMPILING_IN_CPYTHON
369 370 371
    if (is_list || PyList_CheckExact(o)) {
        Py_ssize_t n = (!wraparound) ? i : ((likely(i >= 0)) ? i : i + PyList_GET_SIZE(o));
        if ((!boundscheck) || likely((n >= 0) & (n < PyList_GET_SIZE(o)))) {
372 373 374 375 376 377
            PyObject* old = PyList_GET_ITEM(o, n);
            Py_INCREF(v);
            PyList_SET_ITEM(o, n, v);
            Py_DECREF(old);
            return 1;
        }
378 379
    } else {
        // inlined PySequence_SetItem() + special cased length overflow
380 381
        PySequenceMethods *m = Py_TYPE(o)->tp_as_sequence;
        if (likely(m && m->sq_ass_item)) {
382
            if (wraparound && unlikely(i < 0) && likely(m->sq_length)) {
383
                Py_ssize_t l = m->sq_length(o);
384 385 386 387 388 389 390 391 392
                if (likely(l >= 0)) {
                    i += l;
                } else {
                    // if length > max(Py_ssize_t), maybe the object can wrap around itself?
                    if (PyErr_ExceptionMatches(PyExc_OverflowError))
                        PyErr_Clear();
                    else
                        return -1;
                }
393 394 395 396 397 398
            }
            return m->sq_ass_item(o, i, v);
        }
    }
#else
#if CYTHON_COMPILING_IN_PYPY
399
    if (is_list || (PySequence_Check(o) && !PyDict_Check(o))) {
400
#else
401
    if (is_list || PySequence_Check(o)) {
402 403 404 405 406 407 408
#endif
        return PySequence_SetItem(o, i, v);
    }
#endif
    return __Pyx_SetItemInt_Generic(o, PyInt_FromSsize_t(i), v);
}

409

410 411
/////////////// DelItemInt.proto ///////////////

412 413 414
#define __Pyx_DelItemInt(o, i, type, is_signed, to_py_func, is_list, wraparound, boundscheck) \
    (__Pyx_fits_Py_ssize_t(i, type, is_signed) ? \
    __Pyx_DelItemInt_Fast(o, (Py_ssize_t)i, is_list, wraparound) : \
415 416
    (is_list ? (PyErr_SetString(PyExc_IndexError, "list assignment index out of range"), -1) : \
               __Pyx_DelItem_Generic(o, to_py_func(i))))
417

418 419
static CYTHON_INLINE int __Pyx_DelItem_Generic(PyObject *o, PyObject *j);
static CYTHON_INLINE int __Pyx_DelItemInt_Fast(PyObject *o, Py_ssize_t i,
420
                                               int is_list, int wraparound);
421 422 423

/////////////// DelItemInt ///////////////

424 425 426 427 428 429 430 431
static CYTHON_INLINE int __Pyx_DelItem_Generic(PyObject *o, PyObject *j) {
    int r;
    if (!j) return -1;
    r = PyObject_DelItem(o, j);
    Py_DECREF(j);
    return r;
}

432
static CYTHON_INLINE int __Pyx_DelItemInt_Fast(PyObject *o, Py_ssize_t i,
433
                                               CYTHON_NCP_UNUSED int is_list, CYTHON_NCP_UNUSED int wraparound) {
434
#if CYTHON_COMPILING_IN_PYPY
435
    if (is_list || PySequence_Check(o)) {
436 437 438
        return PySequence_DelItem(o, i);
    }
#else
439
    // inlined PySequence_DelItem() + special cased length overflow
440 441
    PySequenceMethods *m = Py_TYPE(o)->tp_as_sequence;
    if (likely(m && m->sq_ass_item)) {
442
        if (wraparound && unlikely(i < 0) && likely(m->sq_length)) {
443
            Py_ssize_t l = m->sq_length(o);
Stefan Behnel's avatar
Stefan Behnel committed
444 445 446 447 448 449 450 451 452
            if (likely(l >= 0)) {
                i += l;
            } else {
                // if length > max(Py_ssize_t), maybe the object can wrap around itself?
                if (PyErr_ExceptionMatches(PyExc_OverflowError))
                    PyErr_Clear();
                else
                    return -1;
            }
453 454 455 456 457 458 459
        }
        return m->sq_ass_item(o, i, (PyObject *)NULL);
    }
#endif
    return __Pyx_DelItem_Generic(o, PyInt_FromSsize_t(i));
}

460

461
/////////////// SliceObject.proto ///////////////
462 463

// we pass pointer addresses to show the C compiler what is NULL and what isn't
464 465
{{if access == 'Get'}}
static CYTHON_INLINE PyObject* __Pyx_PyObject_GetSlice(
466 467
        PyObject* obj, Py_ssize_t cstart, Py_ssize_t cstop,
        PyObject** py_start, PyObject** py_stop, PyObject** py_slice,
468
        int has_cstart, int has_cstop, int wraparound);
469
{{else}}
470 471
#define __Pyx_PyObject_DelSlice(obj, cstart, cstop, py_start, py_stop, py_slice, has_cstart, has_cstop, wraparound) \
    __Pyx_PyObject_SetSlice(obj, (PyObject*)NULL, cstart, cstop, py_start, py_stop, py_slice, has_cstart, has_cstop, wraparound)
472 473

// we pass pointer addresses to show the C compiler what is NULL and what isn't
474
static CYTHON_INLINE int __Pyx_PyObject_SetSlice(
475 476
        PyObject* obj, PyObject* value, Py_ssize_t cstart, Py_ssize_t cstop,
        PyObject** py_start, PyObject** py_stop, PyObject** py_slice,
477
        int has_cstart, int has_cstop, int wraparound);
478
{{endif}}
479

480
/////////////// SliceObject ///////////////
481

482
{{if access == 'Get'}}
483
static CYTHON_INLINE PyObject* __Pyx_PyObject_GetSlice(PyObject* obj,
484
{{else}}
485
static CYTHON_INLINE int __Pyx_PyObject_SetSlice(PyObject* obj, PyObject* value,
486
{{endif}}
487
        Py_ssize_t cstart, Py_ssize_t cstop,
488
        PyObject** _py_start, PyObject** _py_stop, PyObject** _py_slice,
489
        int has_cstart, int has_cstop, CYTHON_NCP_UNUSED int wraparound) {
Stefan Behnel's avatar
Stefan Behnel committed
490
#if CYTHON_COMPILING_IN_CPYTHON
491 492 493
    PyMappingMethods* mp;
#if PY_MAJOR_VERSION < 3
    PySequenceMethods* ms = Py_TYPE(obj)->tp_as_sequence;
494
    if (likely(ms && ms->sq_{{if access == 'Set'}}ass_{{endif}}slice)) {
495
        if (!has_cstart) {
496
            if (_py_start && (*_py_start != Py_None)) {
497
                cstart = __Pyx_PyIndex_AsSsize_t(*_py_start);
498
                if ((cstart == (Py_ssize_t)-1) && PyErr_Occurred()) goto bad;
499 500 501 502
            } else
                cstart = 0;
        }
        if (!has_cstop) {
503
            if (_py_stop && (*_py_stop != Py_None)) {
504
                cstop = __Pyx_PyIndex_AsSsize_t(*_py_stop);
505
                if ((cstop == (Py_ssize_t)-1) && PyErr_Occurred()) goto bad;
506 507 508
            } else
                cstop = PY_SSIZE_T_MAX;
        }
509
        if (wraparound && unlikely((cstart < 0) | (cstop < 0)) && likely(ms->sq_length)) {
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
            Py_ssize_t l = ms->sq_length(obj);
            if (likely(l >= 0)) {
                if (cstop < 0) {
                    cstop += l;
                    if (cstop < 0) cstop = 0;
                }
                if (cstart < 0) {
                    cstart += l;
                    if (cstart < 0) cstart = 0;
                }
            } else {
                // if length > max(Py_ssize_t), maybe the object can wrap around itself?
                if (PyErr_ExceptionMatches(PyExc_OverflowError))
                    PyErr_Clear();
                else
525
                    goto bad;
526 527
            }
        }
528 529 530
{{if access == 'Get'}}
        return ms->sq_slice(obj, cstart, cstop);
{{else}}
531
        return ms->sq_ass_slice(obj, cstart, cstop, value);
532
{{endif}}
533 534
    }
#endif
535

536
    mp = Py_TYPE(obj)->tp_as_mapping;
537
{{if access == 'Get'}}
Stefan Behnel's avatar
Stefan Behnel committed
538
    if (likely(mp && mp->mp_subscript))
539
{{else}}
Stefan Behnel's avatar
Stefan Behnel committed
540
    if (likely(mp && mp->mp_ass_subscript))
541
{{endif}}
Stefan Behnel's avatar
Stefan Behnel committed
542 543 544
#endif
    {
        {{if access == 'Get'}}PyObject*{{else}}int{{endif}} result;
545
        PyObject *py_slice, *py_start, *py_stop;
546 547 548 549 550 551 552 553 554 555
        if (_py_slice) {
            py_slice = *_py_slice;
        } else {
            PyObject* owned_start = NULL;
            PyObject* owned_stop = NULL;
            if (_py_start) {
                py_start = *_py_start;
            } else {
                if (has_cstart) {
                    owned_start = py_start = PyInt_FromSsize_t(cstart);
556
                    if (unlikely(!py_start)) goto bad;
557 558 559 560 561 562 563 564 565 566
                } else
                    py_start = Py_None;
            }
            if (_py_stop) {
                py_stop = *_py_stop;
            } else {
                if (has_cstop) {
                    owned_stop = py_stop = PyInt_FromSsize_t(cstop);
                    if (unlikely(!py_stop)) {
                        Py_XDECREF(owned_start);
567
                        goto bad;
568 569 570 571 572 573 574
                    }
                } else
                    py_stop = Py_None;
            }
            py_slice = PySlice_New(py_start, py_stop, Py_None);
            Py_XDECREF(owned_start);
            Py_XDECREF(owned_stop);
575
            if (unlikely(!py_slice)) goto bad;
576
        }
Stefan Behnel's avatar
Stefan Behnel committed
577
#if CYTHON_COMPILING_IN_CPYTHON
578 579
{{if access == 'Get'}}
        result = mp->mp_subscript(obj, py_slice);
Stefan Behnel's avatar
Stefan Behnel committed
580 581
#else
        result = PyObject_GetItem(obj, py_slice);
582
{{else}}
583
        result = mp->mp_ass_subscript(obj, py_slice, value);
Stefan Behnel's avatar
Stefan Behnel committed
584 585
#else
        result = value ? PyObject_SetItem(obj, py_slice, value) : PyObject_DelItem(obj, py_slice);
586
{{endif}}
Stefan Behnel's avatar
Stefan Behnel committed
587
#endif
588 589 590 591 592 593
        if (!_py_slice) {
            Py_DECREF(py_slice);
        }
        return result;
    }
    PyErr_Format(PyExc_TypeError,
594
{{if access == 'Get'}}
595
        "'%.200s' object is unsliceable", Py_TYPE(obj)->tp_name);
596
{{else}}
597
        "'%.200s' object does not support slice %.10s",
598
        Py_TYPE(obj)->tp_name, value ? "assignment" : "deletion");
599 600 601 602
{{endif}}

bad:
    return {{if access == 'Get'}}NULL{{else}}-1{{endif}};
603 604
}

605

606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
/////////////// SliceTupleAndList.proto ///////////////

#if CYTHON_COMPILING_IN_CPYTHON
static CYTHON_INLINE PyObject* __Pyx_PyList_GetSlice(PyObject* src, Py_ssize_t start, Py_ssize_t stop);
static CYTHON_INLINE PyObject* __Pyx_PyTuple_GetSlice(PyObject* src, Py_ssize_t start, Py_ssize_t stop);
#else
#define __Pyx_PyList_GetSlice(seq, start, stop)   PySequence_GetSlice(seq, start, stop)
#define __Pyx_PyTuple_GetSlice(seq, start, stop)  PySequence_GetSlice(seq, start, stop)
#endif

/////////////// SliceTupleAndList ///////////////

#if CYTHON_COMPILING_IN_CPYTHON
static CYTHON_INLINE void __Pyx_crop_slice(Py_ssize_t* _start, Py_ssize_t* _stop, Py_ssize_t* _length) {
    Py_ssize_t start = *_start, stop = *_stop, length = *_length;
    if (start < 0) {
        start += length;
        if (start < 0)
            start = 0;
    }

    if (stop < 0)
        stop += length;
    else if (stop > length)
        stop = length;

    *_length = stop - start;
    *_start = start;
    *_stop = stop;
}

Stefan Behnel's avatar
Stefan Behnel committed
637
static CYTHON_INLINE void __Pyx_copy_object_array(PyObject** CYTHON_RESTRICT src, PyObject** CYTHON_RESTRICT dest, Py_ssize_t length) {
638 639 640 641 642 643 644 645
    PyObject *v;
    Py_ssize_t i;
    for (i = 0; i < length; i++) {
        v = dest[i] = src[i];
        Py_INCREF(v);
    }
}

646 647
{{for type in ['List', 'Tuple']}}
static CYTHON_INLINE PyObject* __Pyx_Py{{type}}_GetSlice(
648 649
            PyObject* src, Py_ssize_t start, Py_ssize_t stop) {
    PyObject* dest;
650
    Py_ssize_t length = Py{{type}}_GET_SIZE(src);
651 652
    __Pyx_crop_slice(&start, &stop, &length);
    if (unlikely(length <= 0))
653
        return Py{{type}}_New(0);
654

655
    dest = Py{{type}}_New(length);
656 657
    if (unlikely(!dest))
        return NULL;
Stefan Behnel's avatar
Stefan Behnel committed
658
    __Pyx_copy_object_array(
659 660
        ((Py{{type}}Object*)src)->ob_item + start,
        ((Py{{type}}Object*)dest)->ob_item,
661 662 663
        length);
    return dest;
}
664
{{endfor}}
665 666
#endif

667

668
/////////////// CalculateMetaclass.proto ///////////////
669

670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
static PyObject *__Pyx_CalculateMetaclass(PyTypeObject *metaclass, PyObject *bases);

/////////////// CalculateMetaclass ///////////////

static PyObject *__Pyx_CalculateMetaclass(PyTypeObject *metaclass, PyObject *bases) {
    Py_ssize_t i, nbases = PyTuple_GET_SIZE(bases);
    for (i=0; i < nbases; i++) {
        PyTypeObject *tmptype;
        PyObject *tmp = PyTuple_GET_ITEM(bases, i);
        tmptype = Py_TYPE(tmp);
#if PY_MAJOR_VERSION < 3
        if (tmptype == &PyClass_Type)
            continue;
#endif
        if (!metaclass) {
            metaclass = tmptype;
            continue;
        }
        if (PyType_IsSubtype(metaclass, tmptype))
            continue;
        if (PyType_IsSubtype(tmptype, metaclass)) {
            metaclass = tmptype;
            continue;
        }
        // else:
        PyErr_SetString(PyExc_TypeError,
                        "metaclass conflict: "
                        "the metaclass of a derived class "
                        "must be a (non-strict) subclass "
                        "of the metaclasses of all its bases");
        return NULL;
    }
    if (!metaclass) {
#if PY_MAJOR_VERSION < 3
        metaclass = &PyClass_Type;
#else
        metaclass = &PyType_Type;
#endif
    }
    // make owned reference
    Py_INCREF((PyObject*) metaclass);
    return (PyObject*) metaclass;
}


/////////////// FindInheritedMetaclass.proto ///////////////

static PyObject *__Pyx_FindInheritedMetaclass(PyObject *bases); /*proto*/

/////////////// FindInheritedMetaclass ///////////////
720
//@requires: PyObjectGetAttrStr
721
//@requires: CalculateMetaclass
722

723
static PyObject *__Pyx_FindInheritedMetaclass(PyObject *bases) {
724 725
    PyObject *metaclass;
    if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
726
        PyTypeObject *metatype;
727
        PyObject *base = PyTuple_GET_ITEM(bases, 0);
728 729 730 731 732
#if PY_MAJOR_VERSION < 3
        PyObject* basetype = __Pyx_PyObject_GetAttrStr(base, PYIDENT("__class__"));
        if (basetype) {
            metatype = (PyType_Check(basetype)) ? ((PyTypeObject*) basetype) : NULL;
        } else {
733
            PyErr_Clear();
734 735 736
            metatype = Py_TYPE(base);
            basetype = (PyObject*) metatype;
            Py_INCREF(basetype);
737
        }
738 739 740 741 742 743 744
#else
        metatype = Py_TYPE(base);
#endif
        metaclass = __Pyx_CalculateMetaclass(metatype, bases);
#if PY_MAJOR_VERSION < 3
        Py_DECREF(basetype);
#endif
745
    } else {
746 747
        // no bases => use default metaclass
#if PY_MAJOR_VERSION < 3
748 749 750
        metaclass = (PyObject *) &PyClass_Type;
#else
        metaclass = (PyObject *) &PyType_Type;
751
#endif
752 753
        Py_INCREF(metaclass);
    }
754 755 756 757 758 759 760 761
    return metaclass;
}

/////////////// Py3MetaclassGet.proto ///////////////

static PyObject *__Pyx_Py3MetaclassGet(PyObject *bases, PyObject *mkw); /*proto*/

/////////////// Py3MetaclassGet ///////////////
762 763
//@requires: FindInheritedMetaclass
//@requires: CalculateMetaclass
764 765

static PyObject *__Pyx_Py3MetaclassGet(PyObject *bases, PyObject *mkw) {
766
    PyObject *metaclass = PyDict_GetItem(mkw, PYIDENT("metaclass"));
767 768
    if (metaclass) {
        Py_INCREF(metaclass);
769
        if (PyDict_DelItem(mkw, PYIDENT("metaclass")) < 0) {
770 771 772
            Py_DECREF(metaclass);
            return NULL;
        }
773 774 775 776 777
        if (PyType_Check(metaclass)) {
            PyObject* orig = metaclass;
            metaclass = __Pyx_CalculateMetaclass((PyTypeObject*) metaclass, bases);
            Py_DECREF(orig);
        }
778 779
        return metaclass;
    }
780
    return __Pyx_FindInheritedMetaclass(bases);
781 782 783 784 785
}

/////////////// CreateClass.proto ///////////////

static PyObject *__Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *name,
786
                                   PyObject *qualname, PyObject *modname); /*proto*/
787 788

/////////////// CreateClass ///////////////
789 790
//@requires: FindInheritedMetaclass
//@requires: CalculateMetaclass
791 792

static PyObject *__Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *name,
793
                                   PyObject *qualname, PyObject *modname) {
794 795 796
    PyObject *result;
    PyObject *metaclass;

797
    if (PyDict_SetItem(dict, PYIDENT("__module__"), modname) < 0)
798
        return NULL;
799
    if (PyDict_SetItem(dict, PYIDENT("__qualname__"), qualname) < 0)
800
        return NULL;
801 802

    /* Python2 __metaclass__ */
803
    metaclass = PyDict_GetItem(dict, PYIDENT("__metaclass__"));
804 805
    if (metaclass) {
        Py_INCREF(metaclass);
806 807 808 809 810
        if (PyType_Check(metaclass)) {
            PyObject* orig = metaclass;
            metaclass = __Pyx_CalculateMetaclass((PyTypeObject*) metaclass, bases);
            Py_DECREF(orig);
        }
811
    } else {
812
        metaclass = __Pyx_FindInheritedMetaclass(bases);
813
    }
814 815
    if (unlikely(!metaclass))
        return NULL;
816 817 818 819 820 821 822
    result = PyObject_CallFunctionObjArgs(metaclass, name, bases, dict, NULL);
    Py_DECREF(metaclass);
    return result;
}

/////////////// Py3ClassCreate.proto ///////////////

823 824 825 826
static PyObject *__Pyx_Py3MetaclassPrepare(PyObject *metaclass, PyObject *bases, PyObject *name, PyObject *qualname,
                                           PyObject *mkw, PyObject *modname, PyObject *doc); /*proto*/
static PyObject *__Pyx_Py3ClassCreate(PyObject *metaclass, PyObject *name, PyObject *bases, PyObject *dict,
                                      PyObject *mkw, int calculate_metaclass, int allow_py2_metaclass); /*proto*/
827 828

/////////////// Py3ClassCreate ///////////////
829
//@requires: PyObjectGetAttrStr
830
//@requires: CalculateMetaclass
831 832

static PyObject *__Pyx_Py3MetaclassPrepare(PyObject *metaclass, PyObject *bases, PyObject *name,
833
                                           PyObject *qualname, PyObject *mkw, PyObject *modname, PyObject *doc) {
834
    PyObject *ns;
835 836 837 838 839 840 841 842 843
    if (metaclass) {
        PyObject *prep = __Pyx_PyObject_GetAttrStr(metaclass, PYIDENT("__prepare__"));
        if (prep) {
            PyObject *pargs = PyTuple_Pack(2, name, bases);
            if (unlikely(!pargs)) {
                Py_DECREF(prep);
                return NULL;
            }
            ns = PyObject_Call(prep, pargs, mkw);
844
            Py_DECREF(prep);
845 846 847 848 849 850
            Py_DECREF(pargs);
        } else {
            if (unlikely(!PyErr_ExceptionMatches(PyExc_AttributeError)))
                return NULL;
            PyErr_Clear();
            ns = PyDict_New();
851
        }
852 853
    } else {
        ns = PyDict_New();
854 855
    }

856
    if (unlikely(!ns))
857 858 859
        return NULL;

    /* Required here to emulate assignment order */
860 861 862
    if (unlikely(PyObject_SetItem(ns, PYIDENT("__module__"), modname) < 0)) goto bad;
    if (unlikely(PyObject_SetItem(ns, PYIDENT("__qualname__"), qualname) < 0)) goto bad;
    if (unlikely(doc && PyObject_SetItem(ns, PYIDENT("__doc__"), doc) < 0)) goto bad;
863
    return ns;
864 865 866
bad:
    Py_DECREF(ns);
    return NULL;
867 868 869
}

static PyObject *__Pyx_Py3ClassCreate(PyObject *metaclass, PyObject *name, PyObject *bases,
870 871 872
                                      PyObject *dict, PyObject *mkw,
                                      int calculate_metaclass, int allow_py2_metaclass) {
    PyObject *result, *margs;
873
    PyObject *owned_metaclass = NULL;
874 875
    if (allow_py2_metaclass) {
        /* honour Python2 __metaclass__ for backward compatibility */
876 877 878
        owned_metaclass = PyObject_GetItem(dict, PYIDENT("__metaclass__"));
        if (owned_metaclass) {
            metaclass = owned_metaclass;
879 880 881 882 883 884
        } else if (likely(PyErr_ExceptionMatches(PyExc_KeyError))) {
            PyErr_Clear();
        } else {
            return NULL;
        }
    }
885 886 887 888 889 890
    if (calculate_metaclass && (!metaclass || PyType_Check(metaclass))) {
        metaclass = __Pyx_CalculateMetaclass((PyTypeObject*) metaclass, bases);
        Py_XDECREF(owned_metaclass);
        if (unlikely(!metaclass))
            return NULL;
        owned_metaclass = metaclass;
891 892 893 894 895 896 897 898
    }
    margs = PyTuple_Pack(3, name, bases, dict);
    if (unlikely(!margs)) {
        result = NULL;
    } else {
        result = PyObject_Call(metaclass, margs, mkw);
        Py_DECREF(margs);
    }
899
    Py_XDECREF(owned_metaclass);
900 901
    return result;
}
902 903 904 905 906 907 908 909 910

/////////////// ExtTypeTest.proto ///////////////

static CYTHON_INLINE int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type); /*proto*/

/////////////// ExtTypeTest ///////////////

static CYTHON_INLINE int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type) {
    if (unlikely(!type)) {
911
        PyErr_SetString(PyExc_SystemError, "Missing type object");
912 913 914 915 916 917 918 919
        return 0;
    }
    if (likely(PyObject_TypeCheck(obj, type)))
        return 1;
    PyErr_Format(PyExc_TypeError, "Cannot convert %.200s to %.200s",
                 Py_TYPE(obj)->tp_name, type->tp_name);
    return 0;
}
920 921 922

/////////////// CallableCheck.proto ///////////////

923
#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
924 925 926 927
#define __Pyx_PyCallable_Check(obj)   ((obj)->ob_type->tp_call != NULL)
#else
#define __Pyx_PyCallable_Check(obj)   PyCallable_Check(obj)
#endif
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947

/////////////// PyDictContains.proto ///////////////

static CYTHON_INLINE int __Pyx_PyDict_Contains(PyObject* item, PyObject* dict, int eq) {
    int result = PyDict_Contains(dict, item);
    return unlikely(result < 0) ? result : (result == (eq == Py_EQ));
}

/////////////// PySequenceContains.proto ///////////////

static CYTHON_INLINE int __Pyx_PySequence_Contains(PyObject* item, PyObject* seq, int eq) {
    int result = PySequence_Contains(seq, item);
    return unlikely(result < 0) ? result : (result == (eq == Py_EQ));
}

/////////////// PyBoolOrNullFromLong.proto ///////////////

static CYTHON_INLINE PyObject* __Pyx_PyBoolOrNull_FromLong(long b) {
    return unlikely(b < 0) ? NULL : __Pyx_PyBool_FromLong(b);
}
948

949
/////////////// GetBuiltinName.proto ///////////////
950

951
static PyObject *__Pyx_GetBuiltinName(PyObject *name); /*proto*/
952

953
/////////////// GetBuiltinName ///////////////
954 955 956
//@requires: PyObjectGetAttrStr
//@substitute: naming

957 958 959 960
static PyObject *__Pyx_GetBuiltinName(PyObject *name) {
    PyObject* result = __Pyx_PyObject_GetAttrStr($builtins_cname, name);
    if (unlikely(!result)) {
        PyErr_Format(PyExc_NameError,
961
#if PY_MAJOR_VERSION >= 3
962
            "name '%U' is not defined", name);
963
#else
964
            "name '%.200s' is not defined", PyString_AS_STRING(name));
965 966 967 968 969
#endif
    }
    return result;
}

970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
/////////////// GetNameInClass.proto ///////////////

static PyObject *__Pyx_GetNameInClass(PyObject *nmspace, PyObject *name); /*proto*/

/////////////// GetNameInClass ///////////////
//@requires: PyObjectGetAttrStr
//@requires: GetModuleGlobalName

static PyObject *__Pyx_GetNameInClass(PyObject *nmspace, PyObject *name) {
    PyObject *result;
    result = __Pyx_PyObject_GetAttrStr(nmspace, name);
    if (!result)
        result = __Pyx_GetModuleGlobalName(name);
    return result;
}

986 987 988 989 990
/////////////// GetModuleGlobalName.proto ///////////////

static CYTHON_INLINE PyObject *__Pyx_GetModuleGlobalName(PyObject *name); /*proto*/

/////////////// GetModuleGlobalName ///////////////
991
//@requires: GetBuiltinName
992 993 994 995 996 997
//@substitute: naming

static CYTHON_INLINE PyObject *__Pyx_GetModuleGlobalName(PyObject *name) {
    PyObject *result;
#if CYTHON_COMPILING_IN_CPYTHON
    result = PyDict_GetItem($moddict_cname, name);
998
    if (likely(result)) {
999 1000 1001 1002 1003 1004 1005
        Py_INCREF(result);
    } else {
#else
    result = PyObject_GetItem($moddict_cname, name);
    if (!result) {
        PyErr_Clear();
#endif
1006
        result = __Pyx_GetBuiltinName(name);
1007 1008 1009 1010
    }
    return result;
}

1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
//////////////////// GetAttr.proto ////////////////////

static CYTHON_INLINE PyObject *__Pyx_GetAttr(PyObject *, PyObject *); /*proto*/

//////////////////// GetAttr ////////////////////
//@requires: PyObjectGetAttrStr

static CYTHON_INLINE PyObject *__Pyx_GetAttr(PyObject *o, PyObject *n) {
#if CYTHON_COMPILING_IN_CPYTHON
#if PY_MAJOR_VERSION >= 3
    if (likely(PyUnicode_Check(n)))
#else
    if (likely(PyString_Check(n)))
#endif
        return __Pyx_PyObject_GetAttrStr(o, n);
#endif
    return PyObject_GetAttr(o, n);
}

1030 1031 1032
/////////////// PyObjectLookupSpecial.proto ///////////////
//@requires: PyObjectGetAttrStr

1033
#if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x02070000
1034
// looks like calling _PyType_Lookup() isn't safe in Py<=2.6/3.1
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
static CYTHON_INLINE PyObject* __Pyx_PyObject_LookupSpecial(PyObject* obj, PyObject* attr_name) {
    PyObject *res;
    PyTypeObject *tp = Py_TYPE(obj);
#if PY_MAJOR_VERSION < 3
    if (unlikely(PyInstance_Check(obj)))
        return __Pyx_PyObject_GetAttrStr(obj, attr_name);
#endif
    // adapted from CPython's special_lookup() in ceval.c
    res = _PyType_Lookup(tp, attr_name);
    if (likely(res)) {
        descrgetfunc f = Py_TYPE(res)->tp_descr_get;
        if (!f) {
            Py_INCREF(res);
        } else {
            res = f(res, obj, (PyObject *)tp);
        }
    } else {
        PyErr_SetObject(PyExc_AttributeError, attr_name);
    }
    return res;
}
#else
1057
#define __Pyx_PyObject_LookupSpecial(o,n) __Pyx_PyObject_GetAttrStr(o,n)
1058 1059
#endif

1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
/////////////// PyObjectGetAttrStr.proto ///////////////

#if CYTHON_COMPILING_IN_CPYTHON
static CYTHON_INLINE PyObject* __Pyx_PyObject_GetAttrStr(PyObject* obj, PyObject* attr_name) {
    PyTypeObject* tp = Py_TYPE(obj);
    if (likely(tp->tp_getattro))
        return tp->tp_getattro(obj, attr_name);
#if PY_MAJOR_VERSION < 3
    if (likely(tp->tp_getattr))
        return tp->tp_getattr(obj, PyString_AS_STRING(attr_name));
#endif
    return PyObject_GetAttr(obj, attr_name);
}
#else
#define __Pyx_PyObject_GetAttrStr(o,n) PyObject_GetAttr(o,n)
#endif

1077 1078 1079
/////////////// PyObjectSetAttrStr.proto ///////////////

#if CYTHON_COMPILING_IN_CPYTHON
1080
#define __Pyx_PyObject_DelAttrStr(o,n) __Pyx_PyObject_SetAttrStr(o,n,NULL)
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
static CYTHON_INLINE int __Pyx_PyObject_SetAttrStr(PyObject* obj, PyObject* attr_name, PyObject* value) {
    PyTypeObject* tp = Py_TYPE(obj);
    if (likely(tp->tp_setattro))
        return tp->tp_setattro(obj, attr_name, value);
#if PY_MAJOR_VERSION < 3
    if (likely(tp->tp_setattr))
        return tp->tp_setattr(obj, PyString_AS_STRING(attr_name), value);
#endif
    return PyObject_SetAttr(obj, attr_name, value);
}
#else
1092
#define __Pyx_PyObject_DelAttrStr(o,n)   PyObject_DelAttr(o,n)
1093 1094 1095
#define __Pyx_PyObject_SetAttrStr(o,n,v) PyObject_SetAttr(o,n,v)
#endif

1096 1097 1098 1099 1100 1101

/////////////// PyObjectCallMethod0.proto ///////////////

static PyObject* __Pyx_PyObject_CallMethod0(PyObject* obj, PyObject* method_name); /*proto*/

/////////////// PyObjectCallMethod0 ///////////////
1102
//@requires: PyObjectGetAttrStr
1103 1104
//@requires: PyObjectCallOneArg
//@requires: PyObjectCallNoArg
1105

1106
static PyObject* __Pyx_PyObject_CallMethod0(PyObject* obj, PyObject* method_name) {
1107
    PyObject *method, *result = NULL;
1108
    method = __Pyx_PyObject_GetAttrStr(obj, method_name);
1109
    if (unlikely(!method)) goto bad;
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
#if CYTHON_COMPILING_IN_CPYTHON
    if (likely(PyMethod_Check(method))) {
        PyObject *self = PyMethod_GET_SELF(method);
        if (likely(self)) {
            PyObject *function = PyMethod_GET_FUNCTION(method);
            result = __Pyx_PyObject_CallOneArg(function, self);
            Py_DECREF(method);
            return result;
        }
    }
#endif
    result = __Pyx_PyObject_CallNoArg(method);
1122 1123 1124 1125 1126
    Py_DECREF(method);
bad:
    return result;
}

1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169

/////////////// PyObjectCallMethod1.proto ///////////////

static PyObject* __Pyx_PyObject_CallMethod1(PyObject* obj, PyObject* method_name, PyObject* arg); /*proto*/

/////////////// PyObjectCallMethod1 ///////////////
//@requires: PyObjectGetAttrStr
//@requires: PyObjectCallOneArg

static PyObject* __Pyx_PyObject_CallMethod1(PyObject* obj, PyObject* method_name, PyObject* arg) {
    PyObject *method, *result = NULL;
    method = __Pyx_PyObject_GetAttrStr(obj, method_name);
    if (unlikely(!method)) goto bad;
#if CYTHON_COMPILING_IN_CPYTHON
    if (likely(PyMethod_Check(method))) {
        PyObject *self = PyMethod_GET_SELF(method);
        if (likely(self)) {
            PyObject *args;
            PyObject *function = PyMethod_GET_FUNCTION(method);
            args = PyTuple_New(2);
            if (unlikely(!args)) goto bad;
            Py_INCREF(self);
            PyTuple_SET_ITEM(args, 0, self);
            Py_INCREF(arg);
            PyTuple_SET_ITEM(args, 1, arg);
            Py_INCREF(function);
            Py_DECREF(method); method = NULL;
            result = __Pyx_PyObject_Call(function, args, NULL);
            Py_DECREF(args);
            Py_DECREF(function);
            return result;
        }
    }
#endif
    result = __Pyx_PyObject_CallOneArg(method, arg);
bad:
    Py_XDECREF(method);
    return result;
}


/////////////// PyObjectCallMethod2.proto ///////////////

Stefan Behnel's avatar
Stefan Behnel committed
1170
static PyObject* __Pyx_PyObject_CallMethod2(PyObject* obj, PyObject* method_name, PyObject* arg1, PyObject* arg2); /*proto*/
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212

/////////////// PyObjectCallMethod2 ///////////////
//@requires: PyObjectGetAttrStr
//@requires: PyObjectCall

static PyObject* __Pyx_PyObject_CallMethod2(PyObject* obj, PyObject* method_name, PyObject* arg1, PyObject* arg2) {
    PyObject *args, *method, *result = NULL;
    method = __Pyx_PyObject_GetAttrStr(obj, method_name);
#if CYTHON_COMPILING_IN_CPYTHON
    if (likely(PyMethod_Check(method)) && likely(PyMethod_GET_SELF(method))) {
        PyObject *self, *function;
        self = PyMethod_GET_SELF(method);
        function = PyMethod_GET_FUNCTION(method);
        args = PyTuple_New(3);
        if (unlikely(!args)) goto bad;
        Py_INCREF(self);
        PyTuple_SET_ITEM(args, 0, self);
        Py_INCREF(arg1);
        PyTuple_SET_ITEM(args, 1, arg1);
        Py_INCREF(arg2);
        PyTuple_SET_ITEM(args, 2, arg2);
        Py_INCREF(function);
        Py_DECREF(method);
        method = function;
    } else
#endif
    {
        args = PyTuple_New(2);
        if (unlikely(!args)) goto bad;
        Py_INCREF(arg1);
        PyTuple_SET_ITEM(args, 0, arg1);
        Py_INCREF(arg2);
        PyTuple_SET_ITEM(args, 1, arg2);
    }
    result = __Pyx_PyObject_Call(method, args, NULL);
    Py_DECREF(args);
    Py_DECREF(method);
    return result;
bad:
    Py_XDECREF(method);
    return result;
}
1213 1214 1215 1216


/////////////// tp_new.proto ///////////////

1217 1218
#define __Pyx_tp_new(type_obj, args) __Pyx_tp_new_kwargs(type_obj, args, NULL)
static CYTHON_INLINE PyObject* __Pyx_tp_new_kwargs(PyObject* type_obj, PyObject* args, PyObject* kwargs) {
Stefan Behnel's avatar
Stefan Behnel committed
1219
    return (PyObject*) (((PyTypeObject*)type_obj)->tp_new((PyTypeObject*)type_obj, args, kwargs));
1220
}
Stefan Behnel's avatar
Stefan Behnel committed
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239


/////////////// PyObjectCall.proto ///////////////

#if CYTHON_COMPILING_IN_CPYTHON
static CYTHON_INLINE PyObject* __Pyx_PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw); /*proto*/
#else
#define __Pyx_PyObject_Call(func, arg, kw) PyObject_Call(func, arg, kw)
#endif

/////////////// PyObjectCall ///////////////

#if CYTHON_COMPILING_IN_CPYTHON
static CYTHON_INLINE PyObject* __Pyx_PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw) {
    PyObject *result;
    ternaryfunc call = func->ob_type->tp_call;

    if (unlikely(!call))
        return PyObject_Call(func, arg, kw);
Stefan Behnel's avatar
Stefan Behnel committed
1240
    if (unlikely(Py_EnterRecursiveCall((char*)" while calling a Python object")))
Stefan Behnel's avatar
Stefan Behnel committed
1241 1242 1243 1244 1245 1246 1247
        return NULL;
    result = (*call)(func, arg, kw);
    Py_LeaveRecursiveCall();
    if (unlikely(!result) && unlikely(!PyErr_Occurred())) {
        PyErr_SetString(
            PyExc_SystemError,
            "NULL result without error in PyObject_Call");
1248 1249 1250 1251 1252 1253
    }
    return result;
}
#endif


1254
/////////////// PyObjectCallMethO.proto ///////////////
1255

1256 1257 1258
#if CYTHON_COMPILING_IN_CPYTHON
static CYTHON_INLINE PyObject* __Pyx_PyObject_CallMethO(PyObject *func, PyObject *arg); /*proto*/
#endif
1259

1260
/////////////// PyObjectCallMethO ///////////////
1261 1262

#if CYTHON_COMPILING_IN_CPYTHON
1263
static CYTHON_INLINE PyObject* __Pyx_PyObject_CallMethO(PyObject *func, PyObject *arg) {
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
    PyObject *self, *result;
    PyCFunction cfunc;
    cfunc = PyCFunction_GET_FUNCTION(func);
    self = PyCFunction_GET_SELF(func);

    if (unlikely(Py_EnterRecursiveCall((char*)" while calling a Python object")))
        return NULL;
    result = cfunc(self, arg);
    Py_LeaveRecursiveCall();
    if (unlikely(!result) && unlikely(!PyErr_Occurred())) {
        PyErr_SetString(
            PyExc_SystemError,
            "NULL result without error in PyObject_Call");
    }
    return result;
}
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
#endif


/////////////// PyObjectCallOneArg.proto ///////////////

static CYTHON_INLINE PyObject* __Pyx_PyObject_CallOneArg(PyObject *func, PyObject *arg); /*proto*/

/////////////// PyObjectCallOneArg ///////////////
//@requires: PyObjectCallMethO
//@requires: PyObjectCall

#if CYTHON_COMPILING_IN_CPYTHON
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
static PyObject* __Pyx__PyObject_CallOneArg(PyObject *func, PyObject *arg) {
    PyObject *result;
    PyObject *args = PyTuple_New(1);
    if (unlikely(!args)) return NULL;
    Py_INCREF(arg);
    PyTuple_SET_ITEM(args, 0, arg);
    result = __Pyx_PyObject_Call(func, args, NULL);
    Py_DECREF(args);
    return result;
}

1303 1304
static CYTHON_INLINE PyObject* __Pyx_PyObject_CallOneArg(PyObject *func, PyObject *arg) {
#ifdef __Pyx_CyFunction_USED
1305 1306 1307
    if (likely(PyCFunction_Check(func) || PyObject_TypeCheck(func, __pyx_CyFunctionType))) {
#else
    if (likely(PyCFunction_Check(func))) {
1308
#endif
1309 1310 1311 1312 1313
        if (likely(PyCFunction_GET_FLAGS(func) & METH_O)) {
            // fast and simple case that we are optimising for
            return __Pyx_PyObject_CallMethO(func, arg);
        }
    }
1314
    return __Pyx__PyObject_CallOneArg(func, arg);
1315
}
1316 1317 1318 1319 1320 1321 1322
#else
static CYTHON_INLINE PyObject* __Pyx_PyObject_CallOneArg(PyObject *func, PyObject *arg) {
    PyObject* args = PyTuple_Pack(1, arg);
    return (likely(args)) ? __Pyx_PyObject_Call(func, args, NULL) : NULL;
}
#endif

1323

1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
/////////////// PyObjectCallNoArg.proto ///////////////
//@requires: PyObjectCall
//@substitute: naming

#if CYTHON_COMPILING_IN_CPYTHON
static CYTHON_INLINE PyObject* __Pyx_PyObject_CallNoArg(PyObject *func); /*proto*/
#else
#define __Pyx_PyObject_CallNoArg(func) __Pyx_PyObject_Call(func, $empty_tuple, NULL)
#endif

/////////////// PyObjectCallNoArg ///////////////
1335
//@requires: PyObjectCallMethO
1336 1337 1338 1339 1340
//@requires: PyObjectCall
//@substitute: naming

#if CYTHON_COMPILING_IN_CPYTHON
static CYTHON_INLINE PyObject* __Pyx_PyObject_CallNoArg(PyObject *func) {
1341
#ifdef __Pyx_CyFunction_USED
1342 1343 1344
    if (likely(PyCFunction_Check(func) || PyObject_TypeCheck(func, __pyx_CyFunctionType))) {
#else
    if (likely(PyCFunction_Check(func))) {
1345
#endif
1346 1347 1348 1349
        if (likely(PyCFunction_GET_FLAGS(func) & METH_NOARGS)) {
            // fast and simple case that we are optimising for
            return __Pyx_PyObject_CallMethO(func, NULL);
        }
1350
    }
1351
    return __Pyx_PyObject_Call(func, $empty_tuple, NULL);
Stefan Behnel's avatar
Stefan Behnel committed
1352 1353
}
#endif
1354 1355 1356 1357 1358 1359 1360 1361


/////////////// MatrixMultiply.proto ///////////////

#if PY_VERSION_HEX >= 0x03050000
  #define __Pyx_PyNumber_MatrixMultiply(x,y)         PyNumber_MatrixMultiply(x,y)
  #define __Pyx_PyNumber_InPlaceMatrixMultiply(x,y)  PyNumber_InPlaceMatrixMultiply(x,y)
#else
1362 1363
#define __Pyx_PyNumber_MatrixMultiply(x,y)         __Pyx__PyNumber_MatrixMultiply(x, y, "@")
static PyObject* __Pyx__PyNumber_MatrixMultiply(PyObject* x, PyObject* y, const char* op_name);
1364 1365 1366 1367 1368
static PyObject* __Pyx_PyNumber_InPlaceMatrixMultiply(PyObject* x, PyObject* y);
#endif

/////////////// MatrixMultiply ///////////////
//@requires: PyObjectGetAttrStr
1369
//@requires: PyObjectCallOneArg
1370 1371

#if PY_VERSION_HEX < 0x03050000
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
static PyObject* __Pyx_PyObject_CallMatrixMethod(PyObject* method, PyObject* arg) {
    // NOTE: eats the method reference
    PyObject *result = NULL;
#if CYTHON_COMPILING_IN_CPYTHON
    if (likely(PyMethod_Check(method))) {
        PyObject *self = PyMethod_GET_SELF(method);
        if (likely(self)) {
            PyObject *args;
            PyObject *function = PyMethod_GET_FUNCTION(method);
            args = PyTuple_New(2);
            if (unlikely(!args)) goto bad;
            Py_INCREF(self);
            PyTuple_SET_ITEM(args, 0, self);
            Py_INCREF(arg);
            PyTuple_SET_ITEM(args, 1, arg);
            Py_INCREF(function);
            Py_DECREF(method); method = NULL;
            result = __Pyx_PyObject_Call(function, args, NULL);
            Py_DECREF(args);
            Py_DECREF(function);
            return result;
        }
    }
#endif
    result = __Pyx_PyObject_CallOneArg(method, arg);
bad:
    Py_DECREF(method);
    return result;
}

1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
#define __Pyx_TryMatrixMethod(x, y, py_method_name) {                   \
    PyObject *func = __Pyx_PyObject_GetAttrStr(x, py_method_name);      \
    if (func) {                                                         \
        PyObject *result = __Pyx_PyObject_CallMatrixMethod(func, y);    \
        if (result != Py_NotImplemented)                                \
            return result;                                              \
        Py_DECREF(result);                                              \
    } else {                                                            \
        if (!PyErr_ExceptionMatches(PyExc_AttributeError))              \
            return NULL;                                                \
        PyErr_Clear();                                                  \
    }                                                                   \
}

1416
static PyObject* __Pyx__PyNumber_MatrixMultiply(PyObject* x, PyObject* y, const char* op_name) {
1417 1418 1419 1420 1421
    int right_is_subtype = PyObject_IsSubclass((PyObject*)Py_TYPE(y), (PyObject*)Py_TYPE(x));
    if (right_is_subtype) {
        // to allow subtypes to override parent behaviour, try reversed operation first
        // see note at https://docs.python.org/3/reference/datamodel.html#emulating-numeric-types
        __Pyx_TryMatrixMethod(y, x, PYIDENT("__rmatmul__"))
1422
    }
1423 1424 1425
    __Pyx_TryMatrixMethod(x, y, PYIDENT("__matmul__"))
    if (!right_is_subtype) {
        __Pyx_TryMatrixMethod(y, x, PYIDENT("__rmatmul__"))
1426
    }
1427 1428 1429 1430 1431 1432
    PyErr_Format(PyExc_TypeError,
                 "unsupported operand type(s) for %.2s: '%.100s' and '%.100s'",
                 op_name,
                 Py_TYPE(x)->tp_name,
                 Py_TYPE(y)->tp_name);
    return NULL;
1433 1434 1435
}

static PyObject* __Pyx_PyNumber_InPlaceMatrixMultiply(PyObject* x, PyObject* y) {
1436
    __Pyx_TryMatrixMethod(x, y, PYIDENT("__imatmul__"))
1437
    return __Pyx__PyNumber_MatrixMultiply(x, y, "@=");
1438
}
1439 1440

#undef __Pyx_TryMatrixMethod
1441
#endif