ObjectHandling.c 66.9 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
    }
    return 0;
}

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

83 84 85 86 87 88 89 90 91 92 93
#define __Pyx_unpack_tuple2(tuple, value1, value2, is_tuple, has_known_size, decref_tuple) \
    (likely(is_tuple || PyTuple_Check(tuple)) ? \
        (likely(has_known_size || PyTuple_GET_SIZE(tuple) == 2) ? \
            __Pyx_unpack_tuple2_exact(tuple, value1, value2, decref_tuple) : \
            (__Pyx_UnpackTupleError(tuple, 2), -1)) : \
        __Pyx_unpack_tuple2_generic(tuple, value1, value2, has_known_size, decref_tuple))

static CYTHON_INLINE int __Pyx_unpack_tuple2_exact(
    PyObject* tuple, PyObject** value1, PyObject** value2, int decref_tuple);
static int __Pyx_unpack_tuple2_generic(
    PyObject* tuple, PyObject** value1, PyObject** value2, int has_known_size, int decref_tuple);
94 95 96 97

/////////////// UnpackTuple2 ///////////////
//@requires: UnpackItemEndCheck
//@requires: UnpackTupleError
98
//@requires: RaiseNeedMoreValuesToUnpack
99

100 101 102
static CYTHON_INLINE int __Pyx_unpack_tuple2_exact(
        PyObject* tuple, PyObject** pvalue1, PyObject** pvalue2, int decref_tuple) {
    PyObject *value1 = NULL, *value2 = NULL;
103
#if CYTHON_COMPILING_IN_PYPY
104 105
    value1 = PySequence_ITEM(tuple, 0);  if (unlikely(!value1)) goto bad;
    value2 = PySequence_ITEM(tuple, 1);  if (unlikely(!value2)) goto bad;
106
#else
107 108
    value1 = PyTuple_GET_ITEM(tuple, 0);  Py_INCREF(value1);
    value2 = PyTuple_GET_ITEM(tuple, 1);  Py_INCREF(value2);
109
#endif
110 111
    if (decref_tuple) {
        Py_DECREF(tuple);
112
    }
113

114 115 116
    *pvalue1 = value1;
    *pvalue2 = value2;
    return 0;
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
#if CYTHON_COMPILING_IN_PYPY
bad:
    Py_XDECREF(value1);
    Py_XDECREF(value2);
    if (decref_tuple) { Py_XDECREF(tuple); }
    return -1;
#endif
}

static int __Pyx_unpack_tuple2_generic(PyObject* tuple, PyObject** pvalue1, PyObject** pvalue2,
                                       int has_known_size, int decref_tuple) {
    Py_ssize_t index;
    PyObject *value1 = NULL, *value2 = NULL, *iter = NULL;
    iternextfunc iternext;

    iter = PyObject_GetIter(tuple);
    if (unlikely(!iter)) goto bad;
    if (decref_tuple) { Py_DECREF(tuple); tuple = NULL; }

    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; }
    if (!has_known_size && unlikely(__Pyx_IternextUnpackEndCheck(iternext(iter), 2))) goto bad;

    Py_DECREF(iter);
    *pvalue1 = value1;
    *pvalue2 = value2;
    return 0;

146 147 148
unpacking_failed:
    if (!has_known_size && __Pyx_IterFinish() == 0)
        __Pyx_RaiseNeedMoreValuesError(index);
149 150 151 152
bad:
    Py_XDECREF(iter);
    Py_XDECREF(value1);
    Py_XDECREF(value2);
153
    if (decref_tuple) { Py_XDECREF(tuple); }
154 155
    return -1;
}
156

157

158 159
/////////////// IterNext.proto ///////////////

160
#define __Pyx_PyIter_Next(obj) __Pyx_PyIter_Next2(obj, NULL)
161 162 163
static CYTHON_INLINE PyObject *__Pyx_PyIter_Next2(PyObject *, PyObject *); /*proto*/

/////////////// IterNext ///////////////
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
//@requires: Exceptions.c::PyThreadStateGet
//@requires: Exceptions.c::PyErrFetchRestore

static PyObject *__Pyx_PyIter_Next2Default(PyObject* defval) {
    PyObject* exc_type;
    __Pyx_PyThreadState_declare
    __Pyx_PyThreadState_assign
    exc_type = __Pyx_PyErr_Occurred();
    if (unlikely(exc_type)) {
        if (unlikely(!__Pyx_PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration)))
            return NULL;
        if (defval) {
            __Pyx_PyErr_Clear();
            Py_INCREF(defval);
        }
        return defval;
    }
    if (defval) {
        Py_INCREF(defval);
        return defval;
    }
185
    __Pyx_PyErr_SetNone(PyExc_StopIteration);
186 187 188 189 190 191 192
    return NULL;
}

static void __Pyx_PyIter_Next_ErrorNoIterator(PyObject *iterator) {
    PyErr_Format(PyExc_TypeError,
        "%.200s object is not an iterator", Py_TYPE(iterator)->tp_name);
}
193 194 195 196

// originally copied from Py3's builtin_next()
static CYTHON_INLINE PyObject *__Pyx_PyIter_Next2(PyObject* iterator, PyObject* defval) {
    PyObject* next;
197
    // we always do a quick slot check because always PyIter_Check() is so wasteful
198
    iternextfunc iternext = Py_TYPE(iterator)->tp_iternext;
199
    if (likely(iternext)) {
200
#if CYTHON_USE_TYPE_SLOTS
201 202 203 204 205 206 207
        next = iternext(iterator);
        if (likely(next))
            return next;
        #if PY_VERSION_HEX >= 0x02070000
        if (unlikely(iternext == &_PyObject_NextNotImplemented))
            return NULL;
        #endif
208
#else
209 210 211 212
        // note: PyIter_Next() crashes if the slot is NULL in CPython
        next = PyIter_Next(iterator);
        if (likely(next))
            return next;
213
#endif
214 215
    } else if (CYTHON_USE_TYPE_SLOTS || !PyIter_Check(iterator)) {
        __Pyx_PyIter_Next_ErrorNoIterator(iterator);
216 217
        return NULL;
    }
218
    return __Pyx_PyIter_Next2Default(defval);
219 220
}

221 222
/////////////// IterFinish.proto ///////////////

223
static CYTHON_INLINE int __Pyx_IterFinish(void); /*proto*/
224 225 226 227 228 229 230

/////////////// 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.

231
static CYTHON_INLINE int __Pyx_IterFinish(void) {
232
#if CYTHON_FAST_THREAD_STATE
233
    PyThreadState *tstate = __Pyx_PyThreadState_Current;
234 235
    PyObject* exc_type = tstate->curexc_type;
    if (unlikely(exc_type)) {
236
        if (likely(__Pyx_PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) {
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
            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
253 254 255 256 257 258 259 260 261
    if (unlikely(PyErr_Occurred())) {
        if (likely(PyErr_ExceptionMatches(PyExc_StopIteration))) {
            PyErr_Clear();
            return 0;
        } else {
            return -1;
        }
    }
    return 0;
262
#endif
263
}
264

265
/////////////// DictGetItem.proto ///////////////
266 267

#if PY_MAJOR_VERSION >= 3 && !CYTHON_COMPILING_IN_PYPY
268 269 270
static PyObject *__Pyx_PyDict_GetItem(PyObject *d, PyObject* key);/*proto*/

#define __Pyx_PyObject_Dict_GetItem(obj, name) \
271 272 273 274 275
    (likely(PyDict_CheckExact(obj)) ? \
     __Pyx_PyDict_GetItem(obj, name) : PyObject_GetItem(obj, name))

#else
#define __Pyx_PyDict_GetItem(d, key) PyObject_GetItem(d, key)
276
#define __Pyx_PyObject_Dict_GetItem(obj, name)  PyObject_GetItem(obj, name)
277 278 279 280
#endif

/////////////// DictGetItem ///////////////

281
#if PY_MAJOR_VERSION >= 3 && !CYTHON_COMPILING_IN_PYPY
282 283 284 285
static PyObject *__Pyx_PyDict_GetItem(PyObject *d, PyObject* key) {
    PyObject *value;
    value = PyDict_GetItemWithError(d, key);
    if (unlikely(!value)) {
286 287 288 289 290 291
        if (!PyErr_Occurred()) {
            PyObject* args = PyTuple_Pack(1, key);
            if (likely(args))
                PyErr_SetObject(PyExc_KeyError, args);
            Py_XDECREF(args);
        }
292 293 294 295 296 297 298
        return NULL;
    }
    Py_INCREF(value);
    return value;
}
#endif

299 300
/////////////// GetItemInt.proto ///////////////

301 302 303
#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
304
    (is_list ? (PyErr_SetString(PyExc_IndexError, "list index out of range"), (PyObject*)NULL) : \
305
               __Pyx_GetItemInt_Generic(o, to_py_func(i))))
306 307

{{for type in ['List', 'Tuple']}}
308 309 310
#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
311
    (PyErr_SetString(PyExc_IndexError, "{{ type.lower() }} index out of range"), (PyObject*)NULL))
312 313 314 315 316

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

317
static PyObject *__Pyx_GetItemInt_Generic(PyObject *o, PyObject* j);
318 319 320 321 322
static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i,
                                                     int is_list, int wraparound, int boundscheck);

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

323
static PyObject *__Pyx_GetItemInt_Generic(PyObject *o, PyObject* j) {
324 325 326 327 328 329 330 331
    PyObject *r;
    if (!j) return NULL;
    r = PyObject_GetItem(o, j);
    Py_DECREF(j);
    return r;
}

{{for type in ['List', 'Tuple']}}
332
static CYTHON_INLINE PyObject *__Pyx_GetItemInt_{{type}}_Fast(PyObject *o, Py_ssize_t i,
333 334
                                                              CYTHON_NCP_UNUSED int wraparound,
                                                              CYTHON_NCP_UNUSED int boundscheck) {
335
#if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
336 337 338 339 340 341
    Py_ssize_t wrapped_i = i;
    if (wraparound & unlikely(i < 0)) {
        wrapped_i += Py{{type}}_GET_SIZE(o);
    }
    if ((!boundscheck) || likely((0 <= wrapped_i) & (wrapped_i < Py{{type}}_GET_SIZE(o)))) {
        PyObject *r = Py{{type}}_GET_ITEM(o, wrapped_i);
342 343 344 345 346 347 348 349 350 351
        Py_INCREF(r);
        return r;
    }
    return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i));
#else
    return PySequence_GetItem(o, i);
#endif
}
{{endfor}}

352 353 354
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) {
355
#if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS && CYTHON_USE_TYPE_SLOTS
356 357 358
    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))))) {
359 360 361 362 363 364
            PyObject *r = PyList_GET_ITEM(o, n);
            Py_INCREF(r);
            return r;
        }
    }
    else if (PyTuple_CheckExact(o)) {
365 366
        Py_ssize_t n = ((!wraparound) | likely(i >= 0)) ? i : i + PyTuple_GET_SIZE(o);
        if ((!boundscheck) || likely((n >= 0) & (n < PyTuple_GET_SIZE(o)))) {
367 368 369 370
            PyObject *r = PyTuple_GET_ITEM(o, n);
            Py_INCREF(r);
            return r;
        }
371 372
    } else {
        // inlined PySequence_GetItem() + special cased length overflow
373 374
        PySequenceMethods *m = Py_TYPE(o)->tp_as_sequence;
        if (likely(m && m->sq_item)) {
375
            if (wraparound && unlikely(i < 0) && likely(m->sq_length)) {
376
                Py_ssize_t l = m->sq_length(o);
377 378 379 380
                if (likely(l >= 0)) {
                    i += l;
                } else {
                    // if length > max(Py_ssize_t), maybe the object can wrap around itself?
381
                    if (!PyErr_ExceptionMatches(PyExc_OverflowError))
382
                        return NULL;
383
                    PyErr_Clear();
384
                }
385 386 387 388 389
            }
            return m->sq_item(o, i);
        }
    }
#else
390
    if (is_list || PySequence_Check(o)) {
391 392 393 394 395 396
        return PySequence_GetItem(o, i);
    }
#endif
    return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i));
}

397 398
/////////////// SetItemInt.proto ///////////////

399 400 401
#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) : \
402 403
    (is_list ? (PyErr_SetString(PyExc_IndexError, "list assignment index out of range"), -1) : \
               __Pyx_SetItemInt_Generic(o, to_py_func(i), v)))
404

405
static int __Pyx_SetItemInt_Generic(PyObject *o, PyObject *j, PyObject *v);
406 407 408 409 410
static CYTHON_INLINE int __Pyx_SetItemInt_Fast(PyObject *o, Py_ssize_t i, PyObject *v,
                                               int is_list, int wraparound, int boundscheck);

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

411
static int __Pyx_SetItemInt_Generic(PyObject *o, PyObject *j, PyObject *v) {
412 413 414 415 416 417 418
    int r;
    if (!j) return -1;
    r = PyObject_SetItem(o, j, v);
    Py_DECREF(j);
    return r;
}

419 420
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) {
421
#if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS && CYTHON_USE_TYPE_SLOTS
422 423 424
    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)))) {
425 426 427 428 429 430
            PyObject* old = PyList_GET_ITEM(o, n);
            Py_INCREF(v);
            PyList_SET_ITEM(o, n, v);
            Py_DECREF(old);
            return 1;
        }
431 432
    } else {
        // inlined PySequence_SetItem() + special cased length overflow
433 434
        PySequenceMethods *m = Py_TYPE(o)->tp_as_sequence;
        if (likely(m && m->sq_ass_item)) {
435
            if (wraparound && unlikely(i < 0) && likely(m->sq_length)) {
436
                Py_ssize_t l = m->sq_length(o);
437 438 439 440
                if (likely(l >= 0)) {
                    i += l;
                } else {
                    // if length > max(Py_ssize_t), maybe the object can wrap around itself?
441
                    if (!PyErr_ExceptionMatches(PyExc_OverflowError))
442
                        return -1;
443
                    PyErr_Clear();
444
                }
445 446 447 448 449 450
            }
            return m->sq_ass_item(o, i, v);
        }
    }
#else
#if CYTHON_COMPILING_IN_PYPY
451
    if (is_list || (PySequence_Check(o) && !PyDict_Check(o))) {
452
#else
453
    if (is_list || PySequence_Check(o)) {
454 455 456 457 458 459 460
#endif
        return PySequence_SetItem(o, i, v);
    }
#endif
    return __Pyx_SetItemInt_Generic(o, PyInt_FromSsize_t(i), v);
}

461

462 463
/////////////// DelItemInt.proto ///////////////

464 465 466
#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) : \
467 468
    (is_list ? (PyErr_SetString(PyExc_IndexError, "list assignment index out of range"), -1) : \
               __Pyx_DelItem_Generic(o, to_py_func(i))))
469

470
static int __Pyx_DelItem_Generic(PyObject *o, PyObject *j);
471
static CYTHON_INLINE int __Pyx_DelItemInt_Fast(PyObject *o, Py_ssize_t i,
472
                                               int is_list, int wraparound);
473 474 475

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

476
static int __Pyx_DelItem_Generic(PyObject *o, PyObject *j) {
477 478 479 480 481 482 483
    int r;
    if (!j) return -1;
    r = PyObject_DelItem(o, j);
    Py_DECREF(j);
    return r;
}

484
static CYTHON_INLINE int __Pyx_DelItemInt_Fast(PyObject *o, Py_ssize_t i,
485
                                               CYTHON_UNUSED int is_list, CYTHON_NCP_UNUSED int wraparound) {
486
#if !CYTHON_USE_TYPE_SLOTS
487
    if (is_list || PySequence_Check(o)) {
488 489 490
        return PySequence_DelItem(o, i);
    }
#else
491
    // inlined PySequence_DelItem() + special cased length overflow
492 493
    PySequenceMethods *m = Py_TYPE(o)->tp_as_sequence;
    if (likely(m && m->sq_ass_item)) {
494
        if (wraparound && unlikely(i < 0) && likely(m->sq_length)) {
495
            Py_ssize_t l = m->sq_length(o);
Stefan Behnel's avatar
Stefan Behnel committed
496 497 498 499
            if (likely(l >= 0)) {
                i += l;
            } else {
                // if length > max(Py_ssize_t), maybe the object can wrap around itself?
500
                if (!PyErr_ExceptionMatches(PyExc_OverflowError))
Stefan Behnel's avatar
Stefan Behnel committed
501
                    return -1;
502
                PyErr_Clear();
Stefan Behnel's avatar
Stefan Behnel committed
503
            }
504 505 506 507 508 509 510
        }
        return m->sq_ass_item(o, i, (PyObject *)NULL);
    }
#endif
    return __Pyx_DelItem_Generic(o, PyInt_FromSsize_t(i));
}

511

512
/////////////// SliceObject.proto ///////////////
513 514

// we pass pointer addresses to show the C compiler what is NULL and what isn't
515 516
{{if access == 'Get'}}
static CYTHON_INLINE PyObject* __Pyx_PyObject_GetSlice(
517 518
        PyObject* obj, Py_ssize_t cstart, Py_ssize_t cstop,
        PyObject** py_start, PyObject** py_stop, PyObject** py_slice,
519
        int has_cstart, int has_cstop, int wraparound);
520
{{else}}
521 522
#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)
523 524

// we pass pointer addresses to show the C compiler what is NULL and what isn't
525
static CYTHON_INLINE int __Pyx_PyObject_SetSlice(
526 527
        PyObject* obj, PyObject* value, Py_ssize_t cstart, Py_ssize_t cstop,
        PyObject** py_start, PyObject** py_stop, PyObject** py_slice,
528
        int has_cstart, int has_cstop, int wraparound);
529
{{endif}}
530

531
/////////////// SliceObject ///////////////
532

533
{{if access == 'Get'}}
534
static CYTHON_INLINE PyObject* __Pyx_PyObject_GetSlice(PyObject* obj,
535
{{else}}
536
static CYTHON_INLINE int __Pyx_PyObject_SetSlice(PyObject* obj, PyObject* value,
537
{{endif}}
538
        Py_ssize_t cstart, Py_ssize_t cstop,
539
        PyObject** _py_start, PyObject** _py_stop, PyObject** _py_slice,
540
        int has_cstart, int has_cstop, CYTHON_UNUSED int wraparound) {
541
#if CYTHON_USE_TYPE_SLOTS
542 543 544
    PyMappingMethods* mp;
#if PY_MAJOR_VERSION < 3
    PySequenceMethods* ms = Py_TYPE(obj)->tp_as_sequence;
545
    if (likely(ms && ms->sq_{{if access == 'Set'}}ass_{{endif}}slice)) {
546
        if (!has_cstart) {
547
            if (_py_start && (*_py_start != Py_None)) {
548
                cstart = __Pyx_PyIndex_AsSsize_t(*_py_start);
549
                if ((cstart == (Py_ssize_t)-1) && PyErr_Occurred()) goto bad;
550 551 552 553
            } else
                cstart = 0;
        }
        if (!has_cstop) {
554
            if (_py_stop && (*_py_stop != Py_None)) {
555
                cstop = __Pyx_PyIndex_AsSsize_t(*_py_stop);
556
                if ((cstop == (Py_ssize_t)-1) && PyErr_Occurred()) goto bad;
557 558 559
            } else
                cstop = PY_SSIZE_T_MAX;
        }
560
        if (wraparound && unlikely((cstart < 0) | (cstop < 0)) && likely(ms->sq_length)) {
561 562 563 564 565 566 567 568 569 570 571 572
            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?
573
                if (!PyErr_ExceptionMatches(PyExc_OverflowError))
574
                    goto bad;
575
                PyErr_Clear();
576 577
            }
        }
578 579 580
{{if access == 'Get'}}
        return ms->sq_slice(obj, cstart, cstop);
{{else}}
581
        return ms->sq_ass_slice(obj, cstart, cstop, value);
582
{{endif}}
583 584
    }
#endif
585

586
    mp = Py_TYPE(obj)->tp_as_mapping;
587
{{if access == 'Get'}}
Stefan Behnel's avatar
Stefan Behnel committed
588
    if (likely(mp && mp->mp_subscript))
589
{{else}}
Stefan Behnel's avatar
Stefan Behnel committed
590
    if (likely(mp && mp->mp_ass_subscript))
591
{{endif}}
Stefan Behnel's avatar
Stefan Behnel committed
592 593 594
#endif
    {
        {{if access == 'Get'}}PyObject*{{else}}int{{endif}} result;
595
        PyObject *py_slice, *py_start, *py_stop;
596 597 598 599 600 601 602 603 604 605
        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);
606
                    if (unlikely(!py_start)) goto bad;
607 608 609 610 611 612 613 614 615 616
                } 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);
617
                        goto bad;
618 619 620 621 622 623 624
                    }
                } else
                    py_stop = Py_None;
            }
            py_slice = PySlice_New(py_start, py_stop, Py_None);
            Py_XDECREF(owned_start);
            Py_XDECREF(owned_stop);
625
            if (unlikely(!py_slice)) goto bad;
626
        }
627
#if CYTHON_USE_TYPE_SLOTS
628 629
{{if access == 'Get'}}
        result = mp->mp_subscript(obj, py_slice);
Stefan Behnel's avatar
Stefan Behnel committed
630 631
#else
        result = PyObject_GetItem(obj, py_slice);
632
{{else}}
633
        result = mp->mp_ass_subscript(obj, py_slice, value);
Stefan Behnel's avatar
Stefan Behnel committed
634 635
#else
        result = value ? PyObject_SetItem(obj, py_slice, value) : PyObject_DelItem(obj, py_slice);
636
{{endif}}
Stefan Behnel's avatar
Stefan Behnel committed
637
#endif
638 639 640 641 642 643
        if (!_py_slice) {
            Py_DECREF(py_slice);
        }
        return result;
    }
    PyErr_Format(PyExc_TypeError,
644
{{if access == 'Get'}}
645
        "'%.200s' object is unsliceable", Py_TYPE(obj)->tp_name);
646
{{else}}
647
        "'%.200s' object does not support slice %.10s",
648
        Py_TYPE(obj)->tp_name, value ? "assignment" : "deletion");
649 650 651 652
{{endif}}

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

655

656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
/////////////// 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
687
static CYTHON_INLINE void __Pyx_copy_object_array(PyObject** CYTHON_RESTRICT src, PyObject** CYTHON_RESTRICT dest, Py_ssize_t length) {
688 689 690 691 692 693 694 695
    PyObject *v;
    Py_ssize_t i;
    for (i = 0; i < length; i++) {
        v = dest[i] = src[i];
        Py_INCREF(v);
    }
}

696 697
{{for type in ['List', 'Tuple']}}
static CYTHON_INLINE PyObject* __Pyx_Py{{type}}_GetSlice(
698 699
            PyObject* src, Py_ssize_t start, Py_ssize_t stop) {
    PyObject* dest;
700
    Py_ssize_t length = Py{{type}}_GET_SIZE(src);
701 702
    __Pyx_crop_slice(&start, &stop, &length);
    if (unlikely(length <= 0))
703
        return Py{{type}}_New(0);
704

705
    dest = Py{{type}}_New(length);
706 707
    if (unlikely(!dest))
        return NULL;
Stefan Behnel's avatar
Stefan Behnel committed
708
    __Pyx_copy_object_array(
709 710
        ((Py{{type}}Object*)src)->ob_item + start,
        ((Py{{type}}Object*)dest)->ob_item,
711 712 713
        length);
    return dest;
}
714
{{endfor}}
715 716
#endif

717

718
/////////////// CalculateMetaclass.proto ///////////////
719

720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
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 ///////////////
770
//@requires: PyObjectGetAttrStr
771
//@requires: CalculateMetaclass
772

773
static PyObject *__Pyx_FindInheritedMetaclass(PyObject *bases) {
774 775
    PyObject *metaclass;
    if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
776
        PyTypeObject *metatype;
777
#if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
778
        PyObject *base = PyTuple_GET_ITEM(bases, 0);
779 780 781
#else
        PyObject *base = PySequence_ITEM(bases, 0);
#endif
782 783 784 785 786
#if PY_MAJOR_VERSION < 3
        PyObject* basetype = __Pyx_PyObject_GetAttrStr(base, PYIDENT("__class__"));
        if (basetype) {
            metatype = (PyType_Check(basetype)) ? ((PyTypeObject*) basetype) : NULL;
        } else {
787
            PyErr_Clear();
788 789 790
            metatype = Py_TYPE(base);
            basetype = (PyObject*) metatype;
            Py_INCREF(basetype);
791
        }
792 793 794 795
#else
        metatype = Py_TYPE(base);
#endif
        metaclass = __Pyx_CalculateMetaclass(metatype, bases);
796
#if !(CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS)
797 798
        Py_DECREF(base);
#endif
799 800 801
#if PY_MAJOR_VERSION < 3
        Py_DECREF(basetype);
#endif
802
    } else {
803 804
        // no bases => use default metaclass
#if PY_MAJOR_VERSION < 3
805 806 807
        metaclass = (PyObject *) &PyClass_Type;
#else
        metaclass = (PyObject *) &PyType_Type;
808
#endif
809 810
        Py_INCREF(metaclass);
    }
811 812 813 814 815 816 817 818
    return metaclass;
}

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

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

/////////////// Py3MetaclassGet ///////////////
819 820
//@requires: FindInheritedMetaclass
//@requires: CalculateMetaclass
821 822

static PyObject *__Pyx_Py3MetaclassGet(PyObject *bases, PyObject *mkw) {
823
    PyObject *metaclass = mkw ? __Pyx_PyDict_GetItemStr(mkw, PYIDENT("metaclass")) : NULL;
824 825
    if (metaclass) {
        Py_INCREF(metaclass);
826
        if (PyDict_DelItem(mkw, PYIDENT("metaclass")) < 0) {
827 828 829
            Py_DECREF(metaclass);
            return NULL;
        }
830 831 832 833 834
        if (PyType_Check(metaclass)) {
            PyObject* orig = metaclass;
            metaclass = __Pyx_CalculateMetaclass((PyTypeObject*) metaclass, bases);
            Py_DECREF(orig);
        }
835 836
        return metaclass;
    }
837
    return __Pyx_FindInheritedMetaclass(bases);
838 839 840 841 842
}

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

static PyObject *__Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *name,
843
                                   PyObject *qualname, PyObject *modname); /*proto*/
844 845

/////////////// CreateClass ///////////////
846 847
//@requires: FindInheritedMetaclass
//@requires: CalculateMetaclass
848 849

static PyObject *__Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *name,
850
                                   PyObject *qualname, PyObject *modname) {
851 852 853
    PyObject *result;
    PyObject *metaclass;

854
    if (PyDict_SetItem(dict, PYIDENT("__module__"), modname) < 0)
855
        return NULL;
856
    if (PyDict_SetItem(dict, PYIDENT("__qualname__"), qualname) < 0)
857
        return NULL;
858 859

    /* Python2 __metaclass__ */
860
    metaclass = __Pyx_PyDict_GetItemStr(dict, PYIDENT("__metaclass__"));
861 862
    if (metaclass) {
        Py_INCREF(metaclass);
863 864 865 866 867
        if (PyType_Check(metaclass)) {
            PyObject* orig = metaclass;
            metaclass = __Pyx_CalculateMetaclass((PyTypeObject*) metaclass, bases);
            Py_DECREF(orig);
        }
868
    } else {
869
        metaclass = __Pyx_FindInheritedMetaclass(bases);
870
    }
871 872
    if (unlikely(!metaclass))
        return NULL;
873 874 875 876 877 878 879
    result = PyObject_CallFunctionObjArgs(metaclass, name, bases, dict, NULL);
    Py_DECREF(metaclass);
    return result;
}

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

880 881 882 883
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*/
884 885

/////////////// Py3ClassCreate ///////////////
886
//@requires: PyObjectGetAttrStr
887
//@requires: CalculateMetaclass
888 889

static PyObject *__Pyx_Py3MetaclassPrepare(PyObject *metaclass, PyObject *bases, PyObject *name,
890
                                           PyObject *qualname, PyObject *mkw, PyObject *modname, PyObject *doc) {
891
    PyObject *ns;
892 893 894 895 896 897 898 899 900
    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);
901
            Py_DECREF(prep);
902 903
            Py_DECREF(pargs);
        } else {
904
            if (unlikely(!PyErr_ExceptionMatches(PyExc_AttributeError)))
905 906 907
                return NULL;
            PyErr_Clear();
            ns = PyDict_New();
908
        }
909 910
    } else {
        ns = PyDict_New();
911 912
    }

913
    if (unlikely(!ns))
914 915 916
        return NULL;

    /* Required here to emulate assignment order */
917 918 919
    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;
920
    return ns;
921 922 923
bad:
    Py_DECREF(ns);
    return NULL;
924 925 926
}

static PyObject *__Pyx_Py3ClassCreate(PyObject *metaclass, PyObject *name, PyObject *bases,
927 928 929
                                      PyObject *dict, PyObject *mkw,
                                      int calculate_metaclass, int allow_py2_metaclass) {
    PyObject *result, *margs;
930
    PyObject *owned_metaclass = NULL;
931 932
    if (allow_py2_metaclass) {
        /* honour Python2 __metaclass__ for backward compatibility */
933 934 935
        owned_metaclass = PyObject_GetItem(dict, PYIDENT("__metaclass__"));
        if (owned_metaclass) {
            metaclass = owned_metaclass;
936
        } else if (likely(PyErr_ExceptionMatches(PyExc_KeyError))) {
937 938 939 940 941
            PyErr_Clear();
        } else {
            return NULL;
        }
    }
942 943 944 945 946 947
    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;
948 949 950 951 952 953 954 955
    }
    margs = PyTuple_Pack(3, name, bases, dict);
    if (unlikely(!margs)) {
        result = NULL;
    } else {
        result = PyObject_Call(metaclass, margs, mkw);
        Py_DECREF(margs);
    }
956
    Py_XDECREF(owned_metaclass);
957 958
    return result;
}
959 960 961 962 963 964 965 966 967

/////////////// 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)) {
968
        PyErr_SetString(PyExc_SystemError, "Missing type object");
969 970
        return 0;
    }
971
    if (likely(__Pyx_TypeCheck(obj, type)))
972 973 974 975 976
        return 1;
    PyErr_Format(PyExc_TypeError, "Cannot convert %.200s to %.200s",
                 Py_TYPE(obj)->tp_name, type->tp_name);
    return 0;
}
977 978 979

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

980
#if CYTHON_USE_TYPE_SLOTS && PY_MAJOR_VERSION >= 3
981 982 983 984
#define __Pyx_PyCallable_Check(obj)   ((obj)->ob_type->tp_call != NULL)
#else
#define __Pyx_PyCallable_Check(obj)   PyCallable_Check(obj)
#endif
985 986 987

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

988
static CYTHON_INLINE int __Pyx_PyDict_ContainsTF(PyObject* item, PyObject* dict, int eq) {
989 990 991 992 993 994
    int result = PyDict_Contains(dict, item);
    return unlikely(result < 0) ? result : (result == (eq == Py_EQ));
}

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

995
static CYTHON_INLINE int __Pyx_PySequence_ContainsTF(PyObject* item, PyObject* seq, int eq) {
996 997 998 999 1000 1001 1002 1003 1004
    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);
}
1005

1006
/////////////// GetBuiltinName.proto ///////////////
1007

1008
static PyObject *__Pyx_GetBuiltinName(PyObject *name); /*proto*/
1009

1010
/////////////// GetBuiltinName ///////////////
1011 1012 1013
//@requires: PyObjectGetAttrStr
//@substitute: naming

1014 1015 1016 1017
static PyObject *__Pyx_GetBuiltinName(PyObject *name) {
    PyObject* result = __Pyx_PyObject_GetAttrStr($builtins_cname, name);
    if (unlikely(!result)) {
        PyErr_Format(PyExc_NameError,
1018
#if PY_MAJOR_VERSION >= 3
1019
            "name '%U' is not defined", name);
1020
#else
1021
            "name '%.200s' is not defined", PyString_AS_STRING(name));
1022 1023 1024 1025 1026
#endif
    }
    return result;
}

1027 1028 1029 1030 1031 1032 1033
/////////////// GetNameInClass.proto ///////////////

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

/////////////// GetNameInClass ///////////////
//@requires: PyObjectGetAttrStr
//@requires: GetModuleGlobalName
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
//@requires: Exceptions.c::PyThreadStateGet
//@requires: Exceptions.c::PyErrFetchRestore
//@requires: Exceptions.c::PyErrExceptionMatches

static PyObject *__Pyx_GetGlobalNameAfterAttributeLookup(PyObject *name) {
    __Pyx_PyThreadState_declare
    __Pyx_PyThreadState_assign
    if (unlikely(!__Pyx_PyErr_ExceptionMatches(PyExc_AttributeError)))
        return NULL;
    __Pyx_PyErr_Clear();
    return __Pyx_GetModuleGlobalName(name);
}
1046 1047 1048 1049

static PyObject *__Pyx_GetNameInClass(PyObject *nmspace, PyObject *name) {
    PyObject *result;
    result = __Pyx_PyObject_GetAttrStr(nmspace, name);
1050 1051 1052
    if (!result) {
        result = __Pyx_GetGlobalNameAfterAttributeLookup(name);
    }
1053 1054 1055
    return result;
}

1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070

/////////////// SetNameInClass.proto ///////////////

#if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x030500A1
// Identifier names are always interned and have a pre-calculated hash value.
#define __Pyx_SetNameInClass(ns, name, value) \
    (likely(PyDict_CheckExact(ns)) ? _PyDict_SetItem_KnownHash(ns, name, value, ((PyASCIIObject *) name)->hash) : PyObject_SetItem(ns, name, value))
#elif CYTHON_COMPILING_IN_CPYTHON
#define __Pyx_SetNameInClass(ns, name, value) \
    (likely(PyDict_CheckExact(ns)) ? PyDict_SetItem(ns, name, value) : PyObject_SetItem(ns, name, value))
#else
#define __Pyx_SetNameInClass(ns, name, value)  PyObject_SetItem(ns, name, value)
#endif


1071 1072 1073 1074 1075
/////////////// GetModuleGlobalName.proto ///////////////

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

/////////////// GetModuleGlobalName ///////////////
1076
//@requires: GetBuiltinName
1077 1078 1079 1080
//@substitute: naming

static CYTHON_INLINE PyObject *__Pyx_GetModuleGlobalName(PyObject *name) {
    PyObject *result;
1081
#if !CYTHON_AVOID_BORROWED_REFS
1082 1083 1084 1085 1086 1087 1088 1089 1090
#if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x030500A1
    // Identifier names are always interned and have a pre-calculated hash value.
    result = _PyDict_GetItem_KnownHash($moddict_cname, name, ((PyASCIIObject *) name)->hash);
    if (likely(result)) {
        Py_INCREF(result);
    } else if (unlikely(PyErr_Occurred())) {
        result = NULL;
    } else {
#else
1091
    result = PyDict_GetItem($moddict_cname, name);
1092
    if (likely(result)) {
1093 1094
        Py_INCREF(result);
    } else {
1095
#endif
1096 1097 1098 1099 1100
#else
    result = PyObject_GetItem($moddict_cname, name);
    if (!result) {
        PyErr_Clear();
#endif
1101
        result = __Pyx_GetBuiltinName(name);
1102 1103 1104 1105
    }
    return result;
}

1106 1107 1108 1109 1110 1111 1112 1113
//////////////////// GetAttr.proto ////////////////////

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

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

static CYTHON_INLINE PyObject *__Pyx_GetAttr(PyObject *o, PyObject *n) {
1114
#if CYTHON_USE_TYPE_SLOTS
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
#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);
}

1125 1126 1127
/////////////// PyObjectLookupSpecial.proto ///////////////
//@requires: PyObjectGetAttrStr

1128
#if CYTHON_USE_PYTYPE_LOOKUP && CYTHON_USE_TYPE_SLOTS
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
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
1151
#define __Pyx_PyObject_LookupSpecial(o,n) __Pyx_PyObject_GetAttrStr(o,n)
1152 1153
#endif

1154 1155
/////////////// PyObjectGetAttrStr.proto ///////////////

1156
#if CYTHON_USE_TYPE_SLOTS
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
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

1171 1172
/////////////// PyObjectSetAttrStr.proto ///////////////

1173
#if CYTHON_USE_TYPE_SLOTS
1174
#define __Pyx_PyObject_DelAttrStr(o,n) __Pyx_PyObject_SetAttrStr(o,n,NULL)
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
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
1186
#define __Pyx_PyObject_DelAttrStr(o,n)   PyObject_DelAttr(o,n)
1187 1188 1189
#define __Pyx_PyObject_SetAttrStr(o,n,v) PyObject_SetAttr(o,n,v)
#endif

1190

1191 1192 1193 1194 1195
/////////////// UnpackUnboundCMethod.proto ///////////////

typedef struct {
    PyObject *type;
    PyObject **method_name;
1196 1197 1198 1199
    // "func" is set on first access (direct C function pointer)
    PyCFunction func;
    // "method" is set on first access (fallback)
    PyObject *method;
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
    int flag;
} __Pyx_CachedCFunction;

/////////////// UnpackUnboundCMethod ///////////////
//@requires: PyObjectGetAttrStr

static int __Pyx_TryUnpackUnboundCMethod(__Pyx_CachedCFunction* target) {
    PyObject *method;
    method = __Pyx_PyObject_GetAttrStr(target->type, *target->method_name);
    if (unlikely(!method))
        return -1;
    target->method = method;
#if CYTHON_COMPILING_IN_CPYTHON
    #if PY_MAJOR_VERSION >= 3
1214
    // method dscriptor type isn't exported in Py2.x, cannot easily check the type there
1215
    if (likely(__Pyx_TypeCheck(method, &PyMethodDescr_Type)))
1216 1217 1218
    #endif
    {
        PyMethodDescrObject *descr = (PyMethodDescrObject*) method;
1219
        target->func = descr->d_method->ml_meth;
1220
        target->flag = descr->d_method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
1221 1222 1223 1224 1225 1226 1227
    }
#endif
    return 0;
}


/////////////// CallUnboundCMethod0.proto ///////////////
1228
//@substitute: naming
1229 1230 1231 1232

static PyObject* __Pyx__CallUnboundCMethod0(__Pyx_CachedCFunction* cfunc, PyObject* self); /*proto*/
#if CYTHON_COMPILING_IN_CPYTHON
#define __Pyx_CallUnboundCMethod0(cfunc, self)  \
1233 1234 1235
    ((likely((cfunc)->func)) ? \
        (likely((cfunc)->flag == METH_NOARGS) ?  (*((cfunc)->func))(self, NULL) : \
         (likely((cfunc)->flag == (METH_VARARGS | METH_KEYWORDS)) ?  ((*(PyCFunctionWithKeywords)(cfunc)->func)(self, $empty_tuple, NULL)) : \
1236
             ((cfunc)->flag == METH_VARARGS ?  (*((cfunc)->func))(self, $empty_tuple) : \
1237 1238 1239 1240 1241 1242 1243
              (PY_VERSION_HEX >= 0x030600B1 && (cfunc)->flag == METH_FASTCALL ? \
                (PY_VERSION_HEX >= 0x030700A0 ? \
                    (*(__Pyx_PyCFunctionFast)(cfunc)->func)(self, &PyTuple_GET_ITEM($empty_tuple, 0), 0) : \
                    (*(__Pyx_PyCFunctionFastWithKeywords)(cfunc)->func)(self, &PyTuple_GET_ITEM($empty_tuple, 0), 0, NULL)) : \
              (PY_VERSION_HEX >= 0x030700A0 && (cfunc)->flag == (METH_FASTCALL | METH_KEYWORDS) ? \
                    (*(__Pyx_PyCFunctionFastWithKeywords)(cfunc)->func)(self, &PyTuple_GET_ITEM($empty_tuple, 0), 0, NULL) : \
                    __Pyx__CallUnboundCMethod0(cfunc, self)))))) : \
1244
        __Pyx__CallUnboundCMethod0(cfunc, self))
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
#else
#define __Pyx_CallUnboundCMethod0(cfunc, self)  __Pyx__CallUnboundCMethod0(cfunc, self)
#endif

/////////////// CallUnboundCMethod0 ///////////////
//@requires: UnpackUnboundCMethod
//@requires: PyObjectCall

static PyObject* __Pyx__CallUnboundCMethod0(__Pyx_CachedCFunction* cfunc, PyObject* self) {
    PyObject *args, *result = NULL;
    if (unlikely(!cfunc->method) && unlikely(__Pyx_TryUnpackUnboundCMethod(cfunc) < 0)) return NULL;
1256
#if CYTHON_ASSUME_SAFE_MACROS
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
    args = PyTuple_New(1);
    if (unlikely(!args)) goto bad;
    Py_INCREF(self);
    PyTuple_SET_ITEM(args, 0, self);
#else
    args = PyTuple_Pack(1, self);
    if (unlikely(!args)) goto bad;
#endif
    result = __Pyx_PyObject_Call(cfunc->method, args, NULL);
    Py_DECREF(args);
bad:
    return result;
}


/////////////// CallUnboundCMethod1.proto ///////////////

static PyObject* __Pyx__CallUnboundCMethod1(__Pyx_CachedCFunction* cfunc, PyObject* self, PyObject* arg); /*proto*/

#if CYTHON_COMPILING_IN_CPYTHON
#define __Pyx_CallUnboundCMethod1(cfunc, self, arg)  \
1278
    ((likely((cfunc)->func && (cfunc)->flag == METH_O)) ? (*((cfunc)->func))(self, arg) : \
1279
        ((PY_VERSION_HEX >= 0x030600B1 && (cfunc)->func && (cfunc)->flag == METH_FASTCALL) ? \
1280 1281 1282 1283 1284 1285
                (PY_VERSION_HEX >= 0x030700A0 ? \
                    (*(__Pyx_PyCFunctionFast)(cfunc)->func)(self, &arg, 1) : \
                    (*(__Pyx_PyCFunctionFastWithKeywords)(cfunc)->func)(self, &arg, 1, NULL)) : \
              (PY_VERSION_HEX >= 0x030700A0 && (cfunc)->func && (cfunc)->flag == (METH_FASTCALL | METH_KEYWORDS) ? \
                    (*(__Pyx_PyCFunctionFastWithKeywords)(cfunc)->func)(self, &arg, 1, NULL) : \
        __Pyx__CallUnboundCMethod1(cfunc, self, arg))))
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
#else
#define __Pyx_CallUnboundCMethod1(cfunc, self, arg)  __Pyx__CallUnboundCMethod1(cfunc, self, arg)
#endif

/////////////// CallUnboundCMethod1 ///////////////
//@requires: UnpackUnboundCMethod
//@requires: PyObjectCall

static PyObject* __Pyx__CallUnboundCMethod1(__Pyx_CachedCFunction* cfunc, PyObject* self, PyObject* arg){
    PyObject *args, *result = NULL;
    if (unlikely(!cfunc->method) && unlikely(__Pyx_TryUnpackUnboundCMethod(cfunc) < 0)) return NULL;
#if CYTHON_COMPILING_IN_CPYTHON
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
    if (cfunc->func && (cfunc->flag & METH_VARARGS)) {
        args = PyTuple_New(1);
        if (unlikely(!args)) goto bad;
        Py_INCREF(arg);
        PyTuple_SET_ITEM(args, 0, arg);
        if (cfunc->flag & METH_KEYWORDS)
            result = (*(PyCFunctionWithKeywords)cfunc->func)(self, args, NULL);
        else
            result = (*cfunc->func)(self, args);
    } else {
        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);
        result = __Pyx_PyObject_Call(cfunc->method, args, NULL);
    }
1316 1317 1318 1319
#else
    args = PyTuple_Pack(2, self, arg);
    if (unlikely(!args)) goto bad;
    result = __Pyx_PyObject_Call(cfunc->method, args, NULL);
1320
#endif
1321 1322 1323 1324 1325 1326
bad:
    Py_XDECREF(args);
    return result;
}


1327 1328 1329 1330 1331
/////////////// PyObjectCallMethod0.proto ///////////////

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

/////////////// PyObjectCallMethod0 ///////////////
1332
//@requires: PyObjectGetAttrStr
1333 1334
//@requires: PyObjectCallOneArg
//@requires: PyObjectCallNoArg
1335

1336
static PyObject* __Pyx_PyObject_CallMethod0(PyObject* obj, PyObject* method_name) {
1337
    PyObject *method, *result = NULL;
1338
    method = __Pyx_PyObject_GetAttrStr(obj, method_name);
1339
    if (unlikely(!method)) goto bad;
1340
#if CYTHON_UNPACK_METHODS
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
    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);
1352 1353 1354 1355 1356
    Py_DECREF(method);
bad:
    return result;
}

1357 1358 1359 1360

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

static PyObject* __Pyx_PyObject_CallMethod1(PyObject* obj, PyObject* method_name, PyObject* arg); /*proto*/
1361
static PyObject* __Pyx__PyObject_CallMethod1(PyObject* method, PyObject* arg); /*proto*/
1362 1363 1364 1365

/////////////// PyObjectCallMethod1 ///////////////
//@requires: PyObjectGetAttrStr
//@requires: PyObjectCallOneArg
1366
//@requires: PyFunctionFastCall
1367
//@requires: PyCFunctionFastCall
1368

1369
static PyObject* __Pyx__PyObject_CallMethod1(PyObject* method, PyObject* arg) {
1370
    PyObject *result = NULL;
1371
#if CYTHON_UNPACK_METHODS
1372 1373 1374 1375 1376
    if (likely(PyMethod_Check(method))) {
        PyObject *self = PyMethod_GET_SELF(method);
        if (likely(self)) {
            PyObject *args;
            PyObject *function = PyMethod_GET_FUNCTION(method);
1377 1378 1379
            #if CYTHON_FAST_PYCALL
            if (PyFunction_Check(function)) {
                PyObject *args[2] = {self, arg};
1380
                result = __Pyx_PyFunction_FastCall(function, args, 2);
1381 1382 1383
                goto done;
            }
            #endif
1384 1385 1386 1387 1388 1389 1390
            #if CYTHON_FAST_PYCCALL
            if (__Pyx_PyFastCFunction_Check(function)) {
                PyObject *args[2] = {self, arg};
                result = __Pyx_PyCFunction_FastCall(function, args, 2);
                goto done;
            }
            #endif
1391
            args = PyTuple_New(2);
1392
            if (unlikely(!args)) goto done;
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
            Py_INCREF(self);
            PyTuple_SET_ITEM(args, 0, self);
            Py_INCREF(arg);
            PyTuple_SET_ITEM(args, 1, arg);
            Py_INCREF(function);
            result = __Pyx_PyObject_Call(function, args, NULL);
            Py_DECREF(args);
            Py_DECREF(function);
            return result;
        }
    }
#endif
    result = __Pyx_PyObject_CallOneArg(method, arg);
1406 1407
    // avoid "unused label" warning
    goto done;
1408 1409 1410 1411 1412
done:
    return result;
}

static PyObject* __Pyx_PyObject_CallMethod1(PyObject* obj, PyObject* method_name, PyObject* arg) {
1413
    PyObject *method, *result = NULL;
1414 1415 1416
    method = __Pyx_PyObject_GetAttrStr(obj, method_name);
    if (unlikely(!method)) goto done;
    result = __Pyx__PyObject_CallMethod1(method, arg);
1417
done:
1418 1419 1420 1421 1422 1423 1424
    Py_XDECREF(method);
    return result;
}


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

Stefan Behnel's avatar
Stefan Behnel committed
1425
static PyObject* __Pyx_PyObject_CallMethod2(PyObject* obj, PyObject* method_name, PyObject* arg1, PyObject* arg2); /*proto*/
1426 1427 1428 1429

/////////////// PyObjectCallMethod2 ///////////////
//@requires: PyObjectGetAttrStr
//@requires: PyObjectCall
1430
//@requires: PyFunctionFastCall
1431
//@requires: PyCFunctionFastCall
1432 1433 1434 1435

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);
1436
    if (unlikely(!method)) return NULL;
1437
#if CYTHON_UNPACK_METHODS
1438 1439 1440 1441
    if (likely(PyMethod_Check(method)) && likely(PyMethod_GET_SELF(method))) {
        PyObject *self, *function;
        self = PyMethod_GET_SELF(method);
        function = PyMethod_GET_FUNCTION(method);
1442 1443 1444
        #if CYTHON_FAST_PYCALL
        if (PyFunction_Check(function)) {
            PyObject *args[3] = {self, arg1, arg2};
1445
            result = __Pyx_PyFunction_FastCall(function, args, 3);
1446 1447 1448
            goto done;
        }
        #endif
1449 1450 1451 1452 1453 1454 1455
        #if CYTHON_FAST_PYCCALL
        if (__Pyx_PyFastCFunction_Check(function)) {
            PyObject *args[3] = {self, arg1, arg2};
            result = __Pyx_PyFunction_FastCall(function, args, 3);
            goto done;
        }
        #endif
1456
        args = PyTuple_New(3);
1457
        if (unlikely(!args)) goto done;
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
        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
1468 1469 1470 1471
#endif
#if CYTHON_FAST_PYCALL
    if (PyFunction_Check(method)) {
        PyObject *args[2] = {arg1, arg2};
1472
        result = __Pyx_PyFunction_FastCall(method, args, 2);
1473 1474
        goto done;
    } else
1475 1476 1477 1478 1479 1480 1481
#endif
#if CYTHON_FAST_PYCCALL
    if (__Pyx_PyFastCFunction_Check(method)) {
        PyObject *args[2] = {arg1, arg2};
        result = __Pyx_PyCFunction_FastCall(method, args, 2);
        goto done;
    } else
1482 1483 1484
#endif
    {
        args = PyTuple_New(2);
1485
        if (unlikely(!args)) goto done;
1486 1487 1488 1489 1490 1491 1492
        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);
1493 1494
done:
    Py_DECREF(method);
1495 1496
    return result;
}
1497 1498 1499 1500


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

1501 1502
#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
1503
    return (PyObject*) (((PyTypeObject*)type_obj)->tp_new((PyTypeObject*)type_obj, args, kwargs));
1504
}
Stefan Behnel's avatar
Stefan Behnel committed
1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523


/////////////// 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
1524
    if (unlikely(Py_EnterRecursiveCall((char*)" while calling a Python object")))
Stefan Behnel's avatar
Stefan Behnel committed
1525 1526 1527 1528 1529 1530 1531
        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");
1532 1533 1534 1535 1536 1537
    }
    return result;
}
#endif


1538
/////////////// PyObjectCallMethO.proto ///////////////
1539

1540 1541 1542
#if CYTHON_COMPILING_IN_CPYTHON
static CYTHON_INLINE PyObject* __Pyx_PyObject_CallMethO(PyObject *func, PyObject *arg); /*proto*/
#endif
1543

1544
/////////////// PyObjectCallMethO ///////////////
1545 1546

#if CYTHON_COMPILING_IN_CPYTHON
1547
static CYTHON_INLINE PyObject* __Pyx_PyObject_CallMethO(PyObject *func, PyObject *arg) {
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
    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;
}
1564 1565 1566
#endif


1567 1568 1569
/////////////// PyFunctionFastCall.proto ///////////////

#if CYTHON_FAST_PYCALL
1570 1571 1572
#define __Pyx_PyFunction_FastCall(func, args, nargs) \
    __Pyx_PyFunction_FastCallDict((func), (args), (nargs), NULL)

1573 1574
// let's assume that the non-public C-API function might still change during the 3.6 beta phase
#if 1 || PY_VERSION_HEX < 0x030600B1
1575
static PyObject *__Pyx_PyFunction_FastCallDict(PyObject *func, PyObject **args, int nargs, PyObject *kwargs);
1576
#else
1577
#define __Pyx_PyFunction_FastCallDict(func, args, nargs, kwargs) _PyFunction_FastCallDict(func, args, nargs, kwargs)
1578 1579 1580 1581 1582 1583 1584 1585 1586
#endif
#endif

/////////////// PyFunctionFastCall ///////////////
// copied from CPython 3.6 ceval.c

#if CYTHON_FAST_PYCALL
#include "frameobject.h"

1587 1588
static PyObject* __Pyx_PyFunction_FastCallNoKw(PyCodeObject *co, PyObject **args, Py_ssize_t na,
                                               PyObject *globals) {
1589
    PyFrameObject *f;
1590
    PyThreadState *tstate = __Pyx_PyThreadState_Current;
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
    PyObject **fastlocals;
    Py_ssize_t i;
    PyObject *result;

    assert(globals != NULL);
    /* XXX Perhaps we should create a specialized
       PyFrame_New() that doesn't take locals, but does
       take builtins without sanity checking them.
       */
    assert(tstate != NULL);
    f = PyFrame_New(tstate, co, globals, NULL);
    if (f == NULL) {
        return NULL;
    }

    fastlocals = f->f_localsplus;

    for (i = 0; i < na; i++) {
        Py_INCREF(*args);
        fastlocals[i] = *args++;
    }
    result = PyEval_EvalFrameEx(f,0);

    ++tstate->recursion_depth;
    Py_DECREF(f);
    --tstate->recursion_depth;

    return result;
}


#if 1 || PY_VERSION_HEX < 0x030600B1
1623
static PyObject *__Pyx_PyFunction_FastCallDict(PyObject *func, PyObject **args, int nargs, PyObject *kwargs) {
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
    PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
    PyObject *globals = PyFunction_GET_GLOBALS(func);
    PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
    PyObject *closure;
#if PY_MAJOR_VERSION >= 3
    PyObject *kwdefs;
    //#if PY_VERSION_HEX >= 0x03050000
    //PyObject *name, *qualname;
    //#endif
#endif
1634
    PyObject *kwtuple, **k;
1635
    PyObject **d;
1636
    Py_ssize_t nd;
1637 1638 1639 1640 1641
    Py_ssize_t nk;
    PyObject *result;

    assert(kwargs == NULL || PyDict_Check(kwargs));
    nk = kwargs ? PyDict_Size(kwargs) : 0;
1642

1643
    if (Py_EnterRecursiveCall((char*)" while calling a Python object")) {
1644 1645
        return NULL;
    }
1646

1647
    if (
1648 1649 1650
#if PY_MAJOR_VERSION >= 3
            co->co_kwonlyargcount == 0 &&
#endif
1651
            likely(kwargs == NULL || nk == 0) &&
1652
            co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
        /* Fast paths */
        if (argdefs == NULL && co->co_argcount == nargs) {
            result = __Pyx_PyFunction_FastCallNoKw(co, args, nargs, globals);
            goto done;
        }
        else if (nargs == 0 && argdefs != NULL
                 && co->co_argcount == Py_SIZE(argdefs)) {
            /* function called with no arguments, but all parameters have
               a default value: use default values as arguments .*/
            args = &PyTuple_GET_ITEM(argdefs, 0);
            result =__Pyx_PyFunction_FastCallNoKw(co, args, Py_SIZE(argdefs), globals);
            goto done;
        }
    }

    if (kwargs != NULL) {
        Py_ssize_t pos, i;
        kwtuple = PyTuple_New(2 * nk);
        if (kwtuple == NULL) {
            result = NULL;
            goto done;
        }

        k = &PyTuple_GET_ITEM(kwtuple, 0);
        pos = i = 0;
        while (PyDict_Next(kwargs, &pos, &k[i], &k[i+1])) {
            Py_INCREF(k[i]);
            Py_INCREF(k[i+1]);
            i += 2;
        }
        nk = i / 2;
    }
    else {
        kwtuple = NULL;
        k = NULL;
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715
    }

    closure = PyFunction_GET_CLOSURE(func);
#if PY_MAJOR_VERSION >= 3
    kwdefs = PyFunction_GET_KW_DEFAULTS(func);
    //#if PY_VERSION_HEX >= 0x03050000
    //name = ((PyFunctionObject *)func) -> func_name;
    //qualname = ((PyFunctionObject *)func) -> func_qualname;
    //#endif
#endif

    if (argdefs != NULL) {
        d = &PyTuple_GET_ITEM(argdefs, 0);
        nd = Py_SIZE(argdefs);
    }
    else {
        d = NULL;
        nd = 0;
    }

    //#if PY_VERSION_HEX >= 0x03050000
    //return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
    //                                args, nargs,
    //                                NULL, 0,
    //                                d, nd, kwdefs,
    //                                closure, name, qualname);
    //#elif PY_MAJOR_VERSION >= 3
#if PY_MAJOR_VERSION >= 3
1716 1717 1718
    result = PyEval_EvalCodeEx((PyObject*)co, globals, (PyObject *)NULL,
                               args, nargs,
                               k, (int)nk,
1719
                               d, (int)nd, kwdefs, closure);
1720
#else
1721 1722 1723
    result = PyEval_EvalCodeEx(co, globals, (PyObject *)NULL,
                               args, nargs,
                               k, (int)nk,
1724
                               d, (int)nd, closure);
1725
#endif
1726 1727 1728 1729 1730
    Py_XDECREF(kwtuple);

done:
    Py_LeaveRecursiveCall();
    return result;
1731
}
1732 1733
#endif  /* CPython < 3.6 */
#endif  /* CYTHON_FAST_PYCALL */
1734 1735


1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
/////////////// PyCFunctionFastCall.proto ///////////////

#if CYTHON_FAST_PYCCALL
static CYTHON_INLINE PyObject *__Pyx_PyCFunction_FastCall(PyObject *func, PyObject **args, Py_ssize_t nargs);
#else
#define __Pyx_PyCFunction_FastCall(func, args, nargs)  (assert(0), NULL)
#endif

/////////////// PyCFunctionFastCall ///////////////

#if CYTHON_FAST_PYCCALL
static CYTHON_INLINE PyObject * __Pyx_PyCFunction_FastCall(PyObject *func_obj, PyObject **args, Py_ssize_t nargs) {
    PyCFunctionObject *func = (PyCFunctionObject*)func_obj;
    PyCFunction meth = PyCFunction_GET_FUNCTION(func);
    PyObject *self = PyCFunction_GET_SELF(func);
1751
    int flags = PyCFunction_GET_FLAGS(func);
1752 1753

    assert(PyCFunction_Check(func));
1754
    assert(METH_FASTCALL == (flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST | METH_KEYWORDS)));
1755 1756 1757 1758 1759 1760 1761 1762
    assert(nargs >= 0);
    assert(nargs == 0 || args != NULL);

    /* _PyCFunction_FastCallDict() must not be called with an exception set,
       because it may clear it (directly or indirectly) and so the
       caller loses its exception */
    assert(!PyErr_Occurred());

1763 1764 1765 1766 1767
    if ((PY_VERSION_HEX < 0x030700A0) || unlikely(flags & METH_KEYWORDS)) {
        return (*((__Pyx_PyCFunctionFastWithKeywords)meth)) (self, args, nargs, NULL);
    } else {
        return (*((__Pyx_PyCFunctionFast)meth)) (self, args, nargs);
    }
1768
}
1769
#endif  /* CYTHON_FAST_PYCCALL */
1770 1771


1772 1773 1774 1775 1776 1777 1778
/////////////// PyObjectCallOneArg.proto ///////////////

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

/////////////// PyObjectCallOneArg ///////////////
//@requires: PyObjectCallMethO
//@requires: PyObjectCall
1779
//@requires: PyFunctionFastCall
1780
//@requires: PyCFunctionFastCall
1781 1782

#if CYTHON_COMPILING_IN_CPYTHON
1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793
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;
}

1794
static CYTHON_INLINE PyObject* __Pyx_PyObject_CallOneArg(PyObject *func, PyObject *arg) {
1795 1796
#if CYTHON_FAST_PYCALL
    if (PyFunction_Check(func)) {
1797
        return __Pyx_PyFunction_FastCall(func, &arg, 1);
1798 1799
    }
#endif
1800 1801 1802 1803
    if (likely(PyCFunction_Check(func))) {
        if (likely(PyCFunction_GET_FLAGS(func) & METH_O)) {
            // fast and simple case that we are optimising for
            return __Pyx_PyObject_CallMethO(func, arg);
1804 1805 1806 1807
#if CYTHON_FAST_PYCCALL
        } else if (PyCFunction_GET_FLAGS(func) & METH_FASTCALL) {
            return __Pyx_PyCFunction_FastCall(func, &arg, 1);
#endif
1808 1809
        }
    }
1810
    return __Pyx__PyObject_CallOneArg(func, arg);
1811
}
1812 1813
#else
static CYTHON_INLINE PyObject* __Pyx_PyObject_CallOneArg(PyObject *func, PyObject *arg) {
1814 1815 1816 1817 1818 1819
    PyObject *result;
    PyObject *args = PyTuple_Pack(1, arg);
    if (unlikely(!args)) return NULL;
    result = __Pyx_PyObject_Call(func, args, NULL);
    Py_DECREF(args);
    return result;
1820 1821 1822
}
#endif

1823

1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
/////////////// 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 ///////////////
1835
//@requires: PyObjectCallMethO
1836
//@requires: PyObjectCall
1837
//@requires: PyFunctionFastCall
1838 1839 1840 1841
//@substitute: naming

#if CYTHON_COMPILING_IN_CPYTHON
static CYTHON_INLINE PyObject* __Pyx_PyObject_CallNoArg(PyObject *func) {
1842 1843
#if CYTHON_FAST_PYCALL
    if (PyFunction_Check(func)) {
1844
        return __Pyx_PyFunction_FastCall(func, NULL, 0);
1845 1846
    }
#endif
1847
#ifdef __Pyx_CyFunction_USED
1848
    if (likely(PyCFunction_Check(func) || __Pyx_TypeCheck(func, __pyx_CyFunctionType))) {
1849 1850
#else
    if (likely(PyCFunction_Check(func))) {
1851
#endif
1852 1853 1854 1855
        if (likely(PyCFunction_GET_FLAGS(func) & METH_NOARGS)) {
            // fast and simple case that we are optimising for
            return __Pyx_PyObject_CallMethO(func, NULL);
        }
1856
    }
1857
    return __Pyx_PyObject_Call(func, $empty_tuple, NULL);
Stefan Behnel's avatar
Stefan Behnel committed
1858 1859
}
#endif
1860 1861 1862 1863 1864 1865 1866 1867


/////////////// 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
1868 1869
#define __Pyx_PyNumber_MatrixMultiply(x,y)         __Pyx__PyNumber_MatrixMultiply(x, y, "@")
static PyObject* __Pyx__PyNumber_MatrixMultiply(PyObject* x, PyObject* y, const char* op_name);
1870 1871 1872 1873 1874
static PyObject* __Pyx_PyNumber_InPlaceMatrixMultiply(PyObject* x, PyObject* y);
#endif

/////////////// MatrixMultiply ///////////////
//@requires: PyObjectGetAttrStr
1875
//@requires: PyObjectCallOneArg
1876
//@requires: PyFunctionFastCall
1877
//@requires: PyCFunctionFastCall
1878 1879

#if PY_VERSION_HEX < 0x03050000
1880 1881 1882
static PyObject* __Pyx_PyObject_CallMatrixMethod(PyObject* method, PyObject* arg) {
    // NOTE: eats the method reference
    PyObject *result = NULL;
1883
#if CYTHON_UNPACK_METHODS
1884 1885 1886 1887 1888
    if (likely(PyMethod_Check(method))) {
        PyObject *self = PyMethod_GET_SELF(method);
        if (likely(self)) {
            PyObject *args;
            PyObject *function = PyMethod_GET_FUNCTION(method);
1889 1890 1891
            #if CYTHON_FAST_PYCALL
            if (PyFunction_Check(function)) {
                PyObject *args[2] = {self, arg};
1892
                result = __Pyx_PyFunction_FastCall(function, args, 2);
1893 1894 1895
                goto done;
            }
            #endif
1896 1897 1898 1899 1900 1901 1902
            #if CYTHON_FAST_PYCCALL
            if (__Pyx_PyFastCFunction_Check(function)) {
                PyObject *args[2] = {self, arg};
                result = __Pyx_PyCFunction_FastCall(function, args, 2);
                goto done;
            }
            #endif
1903
            args = PyTuple_New(2);
1904
            if (unlikely(!args)) goto done;
1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918
            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);
1919
done:
1920 1921 1922 1923
    Py_DECREF(method);
    return result;
}

1924 1925 1926 1927 1928 1929 1930 1931
#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 {                                                            \
1932
        if (!PyErr_ExceptionMatches(PyExc_AttributeError))              \
1933 1934 1935 1936 1937
            return NULL;                                                \
        PyErr_Clear();                                                  \
    }                                                                   \
}

1938
static PyObject* __Pyx__PyNumber_MatrixMultiply(PyObject* x, PyObject* y, const char* op_name) {
1939
    int right_is_subtype = PyObject_IsSubclass((PyObject*)Py_TYPE(y), (PyObject*)Py_TYPE(x));
1940 1941
    if (unlikely(right_is_subtype == -1))
        return NULL;
1942 1943 1944 1945
    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__"))
1946
    }
1947 1948 1949
    __Pyx_TryMatrixMethod(x, y, PYIDENT("__matmul__"))
    if (!right_is_subtype) {
        __Pyx_TryMatrixMethod(y, x, PYIDENT("__rmatmul__"))
1950
    }
1951 1952 1953 1954 1955 1956
    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;
1957 1958 1959
}

static PyObject* __Pyx_PyNumber_InPlaceMatrixMultiply(PyObject* x, PyObject* y) {
1960
    __Pyx_TryMatrixMethod(x, y, PYIDENT("__imatmul__"))
1961
    return __Pyx__PyNumber_MatrixMultiply(x, y, "@=");
1962
}
1963 1964

#undef __Pyx_TryMatrixMethod
1965
#endif