ObjectHandling.c 49.2 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_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_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
#if CYTHON_COMPILING_IN_CPYTHON
728
        PyObject *base = PyTuple_GET_ITEM(bases, 0);
729 730 731
#else
        PyObject *base = PySequence_ITEM(bases, 0);
#endif
732 733 734 735 736
#if PY_MAJOR_VERSION < 3
        PyObject* basetype = __Pyx_PyObject_GetAttrStr(base, PYIDENT("__class__"));
        if (basetype) {
            metatype = (PyType_Check(basetype)) ? ((PyTypeObject*) basetype) : NULL;
        } else {
737
            PyErr_Clear();
738 739 740
            metatype = Py_TYPE(base);
            basetype = (PyObject*) metatype;
            Py_INCREF(basetype);
741
        }
742 743 744 745
#else
        metatype = Py_TYPE(base);
#endif
        metaclass = __Pyx_CalculateMetaclass(metatype, bases);
746 747 748
#if !CYTHON_COMPILING_IN_CPYTHON
        Py_DECREF(base);
#endif
749 750 751
#if PY_MAJOR_VERSION < 3
        Py_DECREF(basetype);
#endif
752
    } else {
753 754
        // no bases => use default metaclass
#if PY_MAJOR_VERSION < 3
755 756 757
        metaclass = (PyObject *) &PyClass_Type;
#else
        metaclass = (PyObject *) &PyType_Type;
758
#endif
759 760
        Py_INCREF(metaclass);
    }
761 762 763 764 765 766 767 768
    return metaclass;
}

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

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

/////////////// Py3MetaclassGet ///////////////
769 770
//@requires: FindInheritedMetaclass
//@requires: CalculateMetaclass
771 772

static PyObject *__Pyx_Py3MetaclassGet(PyObject *bases, PyObject *mkw) {
773
    PyObject *metaclass = mkw ? PyDict_GetItem(mkw, PYIDENT("metaclass")) : NULL;
774 775
    if (metaclass) {
        Py_INCREF(metaclass);
776
        if (PyDict_DelItem(mkw, PYIDENT("metaclass")) < 0) {
777 778 779
            Py_DECREF(metaclass);
            return NULL;
        }
780 781 782 783 784
        if (PyType_Check(metaclass)) {
            PyObject* orig = metaclass;
            metaclass = __Pyx_CalculateMetaclass((PyTypeObject*) metaclass, bases);
            Py_DECREF(orig);
        }
785 786
        return metaclass;
    }
787
    return __Pyx_FindInheritedMetaclass(bases);
788 789 790 791 792
}

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

static PyObject *__Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *name,
793
                                   PyObject *qualname, PyObject *modname); /*proto*/
794 795

/////////////// CreateClass ///////////////
796 797
//@requires: FindInheritedMetaclass
//@requires: CalculateMetaclass
798 799

static PyObject *__Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *name,
800
                                   PyObject *qualname, PyObject *modname) {
801 802 803
    PyObject *result;
    PyObject *metaclass;

804
    if (PyDict_SetItem(dict, PYIDENT("__module__"), modname) < 0)
805
        return NULL;
806
    if (PyDict_SetItem(dict, PYIDENT("__qualname__"), qualname) < 0)
807
        return NULL;
808 809

    /* Python2 __metaclass__ */
810
    metaclass = PyDict_GetItem(dict, PYIDENT("__metaclass__"));
811 812
    if (metaclass) {
        Py_INCREF(metaclass);
813 814 815 816 817
        if (PyType_Check(metaclass)) {
            PyObject* orig = metaclass;
            metaclass = __Pyx_CalculateMetaclass((PyTypeObject*) metaclass, bases);
            Py_DECREF(orig);
        }
818
    } else {
819
        metaclass = __Pyx_FindInheritedMetaclass(bases);
820
    }
821 822
    if (unlikely(!metaclass))
        return NULL;
823 824 825 826 827 828 829
    result = PyObject_CallFunctionObjArgs(metaclass, name, bases, dict, NULL);
    Py_DECREF(metaclass);
    return result;
}

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

830 831 832 833
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*/
834 835

/////////////// Py3ClassCreate ///////////////
836
//@requires: PyObjectGetAttrStr
837
//@requires: CalculateMetaclass
838 839

static PyObject *__Pyx_Py3MetaclassPrepare(PyObject *metaclass, PyObject *bases, PyObject *name,
840
                                           PyObject *qualname, PyObject *mkw, PyObject *modname, PyObject *doc) {
841
    PyObject *ns;
842 843 844 845 846 847 848 849 850
    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);
851
            Py_DECREF(prep);
852 853 854 855 856 857
            Py_DECREF(pargs);
        } else {
            if (unlikely(!PyErr_ExceptionMatches(PyExc_AttributeError)))
                return NULL;
            PyErr_Clear();
            ns = PyDict_New();
858
        }
859 860
    } else {
        ns = PyDict_New();
861 862
    }

863
    if (unlikely(!ns))
864 865 866
        return NULL;

    /* Required here to emulate assignment order */
867 868 869
    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;
870
    return ns;
871 872 873
bad:
    Py_DECREF(ns);
    return NULL;
874 875 876
}

static PyObject *__Pyx_Py3ClassCreate(PyObject *metaclass, PyObject *name, PyObject *bases,
877 878 879
                                      PyObject *dict, PyObject *mkw,
                                      int calculate_metaclass, int allow_py2_metaclass) {
    PyObject *result, *margs;
880
    PyObject *owned_metaclass = NULL;
881 882
    if (allow_py2_metaclass) {
        /* honour Python2 __metaclass__ for backward compatibility */
883 884 885
        owned_metaclass = PyObject_GetItem(dict, PYIDENT("__metaclass__"));
        if (owned_metaclass) {
            metaclass = owned_metaclass;
886 887 888 889 890 891
        } else if (likely(PyErr_ExceptionMatches(PyExc_KeyError))) {
            PyErr_Clear();
        } else {
            return NULL;
        }
    }
892 893 894 895 896 897
    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;
898 899 900 901 902 903 904 905
    }
    margs = PyTuple_Pack(3, name, bases, dict);
    if (unlikely(!margs)) {
        result = NULL;
    } else {
        result = PyObject_Call(metaclass, margs, mkw);
        Py_DECREF(margs);
    }
906
    Py_XDECREF(owned_metaclass);
907 908
    return result;
}
909 910 911 912 913 914 915 916 917

/////////////// 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)) {
918
        PyErr_SetString(PyExc_SystemError, "Missing type object");
919 920 921 922 923 924 925 926
        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;
}
927 928 929

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

930
#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3
931 932 933 934
#define __Pyx_PyCallable_Check(obj)   ((obj)->ob_type->tp_call != NULL)
#else
#define __Pyx_PyCallable_Check(obj)   PyCallable_Check(obj)
#endif
935 936 937

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

938
static CYTHON_INLINE int __Pyx_PyDict_ContainsTF(PyObject* item, PyObject* dict, int eq) {
939 940 941 942 943 944
    int result = PyDict_Contains(dict, item);
    return unlikely(result < 0) ? result : (result == (eq == Py_EQ));
}

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

945
static CYTHON_INLINE int __Pyx_PySequence_ContainsTF(PyObject* item, PyObject* seq, int eq) {
946 947 948 949 950 951 952 953 954
    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);
}
955

956
/////////////// GetBuiltinName.proto ///////////////
957

958
static PyObject *__Pyx_GetBuiltinName(PyObject *name); /*proto*/
959

960
/////////////// GetBuiltinName ///////////////
961 962 963
//@requires: PyObjectGetAttrStr
//@substitute: naming

964 965 966 967
static PyObject *__Pyx_GetBuiltinName(PyObject *name) {
    PyObject* result = __Pyx_PyObject_GetAttrStr($builtins_cname, name);
    if (unlikely(!result)) {
        PyErr_Format(PyExc_NameError,
968
#if PY_MAJOR_VERSION >= 3
969
            "name '%U' is not defined", name);
970
#else
971
            "name '%.200s' is not defined", PyString_AS_STRING(name));
972 973 974 975 976
#endif
    }
    return result;
}

977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
/////////////// 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;
}

993 994 995 996 997
/////////////// GetModuleGlobalName.proto ///////////////

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

/////////////// GetModuleGlobalName ///////////////
998
//@requires: GetBuiltinName
999 1000 1001 1002 1003 1004
//@substitute: naming

static CYTHON_INLINE PyObject *__Pyx_GetModuleGlobalName(PyObject *name) {
    PyObject *result;
#if CYTHON_COMPILING_IN_CPYTHON
    result = PyDict_GetItem($moddict_cname, name);
1005
    if (likely(result)) {
1006 1007 1008 1009 1010 1011 1012
        Py_INCREF(result);
    } else {
#else
    result = PyObject_GetItem($moddict_cname, name);
    if (!result) {
        PyErr_Clear();
#endif
1013
        result = __Pyx_GetBuiltinName(name);
1014 1015 1016 1017
    }
    return result;
}

1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
//////////////////// 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);
}

1037 1038 1039
/////////////// PyObjectLookupSpecial.proto ///////////////
//@requires: PyObjectGetAttrStr

1040
#if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x02070000
1041
// looks like calling _PyType_Lookup() isn't safe in Py<=2.6/3.1
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
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
1064
#define __Pyx_PyObject_LookupSpecial(o,n) __Pyx_PyObject_GetAttrStr(o,n)
1065 1066
#endif

1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
/////////////// 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

1084 1085 1086
/////////////// PyObjectSetAttrStr.proto ///////////////

#if CYTHON_COMPILING_IN_CPYTHON
1087
#define __Pyx_PyObject_DelAttrStr(o,n) __Pyx_PyObject_SetAttrStr(o,n,NULL)
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
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
1099
#define __Pyx_PyObject_DelAttrStr(o,n)   PyObject_DelAttr(o,n)
1100 1101 1102
#define __Pyx_PyObject_SetAttrStr(o,n,v) PyObject_SetAttr(o,n,v)
#endif

1103 1104 1105 1106 1107 1108

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

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

/////////////// PyObjectCallMethod0 ///////////////
1109
//@requires: PyObjectGetAttrStr
1110 1111
//@requires: PyObjectCallOneArg
//@requires: PyObjectCallNoArg
1112

1113
static PyObject* __Pyx_PyObject_CallMethod0(PyObject* obj, PyObject* method_name) {
1114
    PyObject *method, *result = NULL;
1115
    method = __Pyx_PyObject_GetAttrStr(obj, method_name);
1116
    if (unlikely(!method)) goto bad;
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
#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);
1129 1130 1131 1132 1133
    Py_DECREF(method);
bad:
    return result;
}

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 1170 1171 1172 1173 1174 1175 1176

/////////////// 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
1177
static PyObject* __Pyx_PyObject_CallMethod2(PyObject* obj, PyObject* method_name, PyObject* arg1, PyObject* arg2); /*proto*/
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 1213 1214 1215 1216 1217 1218 1219

/////////////// 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;
}
1220 1221 1222 1223


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

1224 1225
#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
1226
    return (PyObject*) (((PyTypeObject*)type_obj)->tp_new((PyTypeObject*)type_obj, args, kwargs));
1227
}
Stefan Behnel's avatar
Stefan Behnel committed
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246


/////////////// 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
1247
    if (unlikely(Py_EnterRecursiveCall((char*)" while calling a Python object")))
Stefan Behnel's avatar
Stefan Behnel committed
1248 1249 1250 1251 1252 1253 1254
        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");
1255 1256 1257 1258 1259 1260
    }
    return result;
}
#endif


1261
/////////////// PyObjectCallMethO.proto ///////////////
1262

1263 1264 1265
#if CYTHON_COMPILING_IN_CPYTHON
static CYTHON_INLINE PyObject* __Pyx_PyObject_CallMethO(PyObject *func, PyObject *arg); /*proto*/
#endif
1266

1267
/////////////// PyObjectCallMethO ///////////////
1268 1269

#if CYTHON_COMPILING_IN_CPYTHON
1270
static CYTHON_INLINE PyObject* __Pyx_PyObject_CallMethO(PyObject *func, PyObject *arg) {
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
    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;
}
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
#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
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
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;
}

1310 1311
static CYTHON_INLINE PyObject* __Pyx_PyObject_CallOneArg(PyObject *func, PyObject *arg) {
#ifdef __Pyx_CyFunction_USED
1312 1313 1314
    if (likely(PyCFunction_Check(func) || PyObject_TypeCheck(func, __pyx_CyFunctionType))) {
#else
    if (likely(PyCFunction_Check(func))) {
1315
#endif
1316 1317 1318 1319 1320
        if (likely(PyCFunction_GET_FLAGS(func) & METH_O)) {
            // fast and simple case that we are optimising for
            return __Pyx_PyObject_CallMethO(func, arg);
        }
    }
1321
    return __Pyx__PyObject_CallOneArg(func, arg);
1322
}
1323 1324 1325 1326 1327 1328 1329
#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

1330

1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
/////////////// 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 ///////////////
1342
//@requires: PyObjectCallMethO
1343 1344 1345 1346 1347
//@requires: PyObjectCall
//@substitute: naming

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


/////////////// 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
1369 1370
#define __Pyx_PyNumber_MatrixMultiply(x,y)         __Pyx__PyNumber_MatrixMultiply(x, y, "@")
static PyObject* __Pyx__PyNumber_MatrixMultiply(PyObject* x, PyObject* y, const char* op_name);
1371 1372 1373 1374 1375
static PyObject* __Pyx_PyNumber_InPlaceMatrixMultiply(PyObject* x, PyObject* y);
#endif

/////////////// MatrixMultiply ///////////////
//@requires: PyObjectGetAttrStr
1376
//@requires: PyObjectCallOneArg
1377 1378

#if PY_VERSION_HEX < 0x03050000
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
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;
}

1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
#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();                                                  \
    }                                                                   \
}

1423
static PyObject* __Pyx__PyNumber_MatrixMultiply(PyObject* x, PyObject* y, const char* op_name) {
1424
    int right_is_subtype = PyObject_IsSubclass((PyObject*)Py_TYPE(y), (PyObject*)Py_TYPE(x));
1425 1426
    if (unlikely(right_is_subtype == -1))
        return NULL;
1427 1428 1429 1430
    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__"))
1431
    }
1432 1433 1434
    __Pyx_TryMatrixMethod(x, y, PYIDENT("__matmul__"))
    if (!right_is_subtype) {
        __Pyx_TryMatrixMethod(y, x, PYIDENT("__rmatmul__"))
1435
    }
1436 1437 1438 1439 1440 1441
    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;
1442 1443 1444
}

static PyObject* __Pyx_PyNumber_InPlaceMatrixMultiply(PyObject* x, PyObject* y) {
1445
    __Pyx_TryMatrixMethod(x, y, PYIDENT("__imatmul__"))
1446
    return __Pyx__PyNumber_MatrixMultiply(x, y, "@=");
1447
}
1448 1449

#undef __Pyx_TryMatrixMethod
1450
#endif