CythonFunction.c 51.4 KB
Newer Older
1

2
//////////////////// CythonFunctionShared.proto ////////////////////
3

4
#define __Pyx_CyFunction_USED
5

6 7
#define __Pyx_CYFUNCTION_STATICMETHOD  0x01
#define __Pyx_CYFUNCTION_CLASSMETHOD   0x02
8
#define __Pyx_CYFUNCTION_CCLASS        0x04
9

10 11 12 13 14
#define __Pyx_CyFunction_GetClosure(f) \
    (((__pyx_CyFunctionObject *) (f))->func_closure)
#define __Pyx_CyFunction_GetClassObj(f) \
    (((__pyx_CyFunctionObject *) (f))->func_classobj)

15 16
#define __Pyx_CyFunction_Defaults(type, f) \
    ((type *)(((__pyx_CyFunctionObject *) (f))->defaults))
17 18
#define __Pyx_CyFunction_SetDefaultsGetter(f, g) \
    ((__pyx_CyFunctionObject *) (f))->defaults_getter = (g)
19

20

21 22
typedef struct {
    PyCFunctionObject func;
23 24 25
#if CYTHON_BACKPORT_VECTORCALL
    __pyx_vectorcallfunc func_vectorcall;
#endif
26
#if PY_VERSION_HEX < 0x030500A0
27
    PyObject *func_weakreflist;
28 29
#endif
    PyObject *func_dict;
30
    PyObject *func_name;
31
    PyObject *func_qualname;
32
    PyObject *func_doc;
33
    PyObject *func_globals;
34
    PyObject *func_code;
35
    PyObject *func_closure;
36 37
    // No-args super() class cell
    PyObject *func_classobj;
38

39
    // Dynamic default args and annotations
40 41
    void *defaults;
    int defaults_pyobjects;
42
    size_t defaults_size;  // used by FusedFunction for copying defaults
43
    int flags;
44

45
    // Defaults info
46 47
    PyObject *defaults_tuple;   /* Const defaults tuple */
    PyObject *defaults_kwdict;  /* Const kwonly defaults dict */
48
    PyObject *(*defaults_getter)(PyObject *);
49
    PyObject *func_annotations; /* function annotations dict */
50 51
} __pyx_CyFunctionObject;

52
#if !CYTHON_COMPILING_IN_LIMITED_API
53
static PyTypeObject *__pyx_CyFunctionType = 0;
54
#endif
55

56 57 58
#define __Pyx_CyFunction_Check(obj)  __Pyx_TypeCheck(obj, __pyx_CyFunctionType)
#define __Pyx_IsCyOrPyCFunction(obj)  __Pyx_TypeCheck2(obj, __pyx_CyFunctionType, &PyCFunction_Type)
#define __Pyx_CyFunction_CheckExact(obj)  __Pyx_IS_TYPE(obj, __pyx_CyFunctionType)
59

60
static PyObject *__Pyx_CyFunction_Init(__pyx_CyFunctionObject* op, PyMethodDef *ml,
61
                                      int flags, PyObject* qualname,
62
                                      PyObject *closure,
63
                                      PyObject *module, PyObject *globals,
64
                                      PyObject* code);
65

66 67 68
static CYTHON_INLINE void *__Pyx_CyFunction_InitDefaults(PyObject *m,
                                                         size_t size,
                                                         int pyobjects);
69 70
static CYTHON_INLINE void __Pyx_CyFunction_SetDefaultsTuple(PyObject *m,
                                                            PyObject *tuple);
71 72
static CYTHON_INLINE void __Pyx_CyFunction_SetDefaultsKwDict(PyObject *m,
                                                             PyObject *dict);
73 74
static CYTHON_INLINE void __Pyx_CyFunction_SetAnnotationsDict(PyObject *m,
                                                              PyObject *dict);
75 76


77
static int __pyx_CyFunction_init(void);
78

79 80 81 82 83 84 85 86 87 88 89
#if CYTHON_METH_FASTCALL
static PyObject * __Pyx_CyFunction_Vectorcall_NOARGS(PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
static PyObject * __Pyx_CyFunction_Vectorcall_O(PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
static PyObject * __Pyx_CyFunction_Vectorcall_FASTCALL_KEYWORDS(PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames);
#if CYTHON_BACKPORT_VECTORCALL
#define __Pyx_CyFunction_func_vectorcall(f) (((__pyx_CyFunctionObject*)f)->func_vectorcall)
#else
#define __Pyx_CyFunction_func_vectorcall(f) (((__pyx_CyFunctionObject*)f)->func.vectorcall)
#endif
#endif

90
//////////////////// CythonFunctionShared ////////////////////
Stefan Behnel's avatar
Stefan Behnel committed
91
//@substitute: naming
92
//@requires: CommonStructures.c::FetchCommonType
93
//@requires: ObjectHandling.c::PyMethodNew
94
//@requires: ObjectHandling.c::PyVectorcallFastCallDict
95

96 97
#include <structmember.h>

98 99 100
static PyObject *
__Pyx_CyFunction_get_doc(__pyx_CyFunctionObject *op, CYTHON_UNUSED void *closure)
{
101 102
    if (unlikely(op->func_doc == NULL)) {
        if (op->func.m_ml->ml_doc) {
103
#if PY_MAJOR_VERSION >= 3
104
            op->func_doc = PyUnicode_FromString(op->func.m_ml->ml_doc);
105
#else
106
            op->func_doc = PyString_FromString(op->func.m_ml->ml_doc);
107
#endif
108 109 110 111 112 113
            if (unlikely(op->func_doc == NULL))
                return NULL;
        } else {
            Py_INCREF(Py_None);
            return Py_None;
        }
114 115 116 117 118 119
    }
    Py_INCREF(op->func_doc);
    return op->func_doc;
}

static int
120
__Pyx_CyFunction_set_doc(__pyx_CyFunctionObject *op, PyObject *value, CYTHON_UNUSED void *context)
121
{
122 123 124 125
    if (value == NULL) {
        // Mark as deleted
        value = Py_None;
    }
126
    Py_INCREF(value);
127
    __Pyx_Py_XDECREF_SET(op->func_doc, value);
128 129 130 131
    return 0;
}

static PyObject *
132
__Pyx_CyFunction_get_name(__pyx_CyFunctionObject *op, CYTHON_UNUSED void *context)
133
{
134
    if (unlikely(op->func_name == NULL)) {
135 136 137 138 139
#if PY_MAJOR_VERSION >= 3
        op->func_name = PyUnicode_InternFromString(op->func.m_ml->ml_name);
#else
        op->func_name = PyString_InternFromString(op->func.m_ml->ml_name);
#endif
140 141
        if (unlikely(op->func_name == NULL))
            return NULL;
142 143 144 145 146 147
    }
    Py_INCREF(op->func_name);
    return op->func_name;
}

static int
148
__Pyx_CyFunction_set_name(__pyx_CyFunctionObject *op, PyObject *value, CYTHON_UNUSED void *context)
149 150
{
#if PY_MAJOR_VERSION >= 3
151
    if (unlikely(value == NULL || !PyUnicode_Check(value)))
152
#else
153
    if (unlikely(value == NULL || !PyString_Check(value)))
154
#endif
155
    {
156 157 158 159 160
        PyErr_SetString(PyExc_TypeError,
                        "__name__ must be set to a string object");
        return -1;
    }
    Py_INCREF(value);
161
    __Pyx_Py_XDECREF_SET(op->func_name, value);
162 163 164
    return 0;
}

165
static PyObject *
166
__Pyx_CyFunction_get_qualname(__pyx_CyFunctionObject *op, CYTHON_UNUSED void *context)
167 168 169 170 171 172
{
    Py_INCREF(op->func_qualname);
    return op->func_qualname;
}

static int
173
__Pyx_CyFunction_set_qualname(__pyx_CyFunctionObject *op, PyObject *value, CYTHON_UNUSED void *context)
174 175
{
#if PY_MAJOR_VERSION >= 3
176
    if (unlikely(value == NULL || !PyUnicode_Check(value)))
177
#else
178
    if (unlikely(value == NULL || !PyString_Check(value)))
179
#endif
180
    {
181 182 183 184 185
        PyErr_SetString(PyExc_TypeError,
                        "__qualname__ must be set to a string object");
        return -1;
    }
    Py_INCREF(value);
186
    __Pyx_Py_XDECREF_SET(op->func_qualname, value);
187 188 189
    return 0;
}

190 191 192 193 194
static PyObject *
__Pyx_CyFunction_get_self(__pyx_CyFunctionObject *m, CYTHON_UNUSED void *closure)
{
    PyObject *self;

195
    self = m->func_closure;
196 197 198 199 200 201 202
    if (self == NULL)
        self = Py_None;
    Py_INCREF(self);
    return self;
}

static PyObject *
203
__Pyx_CyFunction_get_dict(__pyx_CyFunctionObject *op, CYTHON_UNUSED void *context)
204
{
205
    if (unlikely(op->func_dict == NULL)) {
206
        op->func_dict = PyDict_New();
207
        if (unlikely(op->func_dict == NULL))
208 209 210 211 212 213 214
            return NULL;
    }
    Py_INCREF(op->func_dict);
    return op->func_dict;
}

static int
215
__Pyx_CyFunction_set_dict(__pyx_CyFunctionObject *op, PyObject *value, CYTHON_UNUSED void *context)
216
{
217
    if (unlikely(value == NULL)) {
218 219 220 221
        PyErr_SetString(PyExc_TypeError,
               "function's dictionary may not be deleted");
        return -1;
    }
222
    if (unlikely(!PyDict_Check(value))) {
223 224 225 226 227
        PyErr_SetString(PyExc_TypeError,
               "setting function's dictionary to a non-dict");
        return -1;
    }
    Py_INCREF(value);
228
    __Pyx_Py_XDECREF_SET(op->func_dict, value);
229 230 231 232
    return 0;
}

static PyObject *
233
__Pyx_CyFunction_get_globals(__pyx_CyFunctionObject *op, CYTHON_UNUSED void *context)
234
{
235 236
    Py_INCREF(op->func_globals);
    return op->func_globals;
237 238 239
}

static PyObject *
240
__Pyx_CyFunction_get_closure(CYTHON_UNUSED __pyx_CyFunctionObject *op, CYTHON_UNUSED void *context)
241 242 243 244 245 246
{
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
247
__Pyx_CyFunction_get_code(__pyx_CyFunctionObject *op, CYTHON_UNUSED void *context)
248 249 250 251 252 253
{
    PyObject* result = (op->func_code) ? op->func_code : Py_None;
    Py_INCREF(result);
    return result;
}

254 255
static int
__Pyx_CyFunction_init_defaults(__pyx_CyFunctionObject *op) {
256
    int result = 0;
257 258 259 260
    PyObject *res = op->defaults_getter((PyObject *) op);
    if (unlikely(!res))
        return -1;

261
    // Cache result
262
    #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
263 264 265 266
    op->defaults_tuple = PyTuple_GET_ITEM(res, 0);
    Py_INCREF(op->defaults_tuple);
    op->defaults_kwdict = PyTuple_GET_ITEM(res, 1);
    Py_INCREF(op->defaults_kwdict);
267 268 269 270 271 272 273 274
    #else
    op->defaults_tuple = PySequence_ITEM(res, 0);
    if (unlikely(!op->defaults_tuple)) result = -1;
    else {
        op->defaults_kwdict = PySequence_ITEM(res, 1);
        if (unlikely(!op->defaults_kwdict)) result = -1;
    }
    #endif
275
    Py_DECREF(res);
276
    return result;
277 278 279
}

static int
280
__Pyx_CyFunction_set_defaults(__pyx_CyFunctionObject *op, PyObject* value, CYTHON_UNUSED void *context) {
281 282 283
    if (!value) {
        // del => explicit None to prevent rebuilding
        value = Py_None;
284
    } else if (unlikely(value != Py_None && !PyTuple_Check(value))) {
285 286 287 288 289
        PyErr_SetString(PyExc_TypeError,
                        "__defaults__ must be set to a tuple object");
        return -1;
    }
    Py_INCREF(value);
290
    __Pyx_Py_XDECREF_SET(op->defaults_tuple, value);
291 292 293
    return 0;
}

294
static PyObject *
295
__Pyx_CyFunction_get_defaults(__pyx_CyFunctionObject *op, CYTHON_UNUSED void *context) {
296 297 298
    PyObject* result = op->defaults_tuple;
    if (unlikely(!result)) {
        if (op->defaults_getter) {
299
            if (unlikely(__Pyx_CyFunction_init_defaults(op) < 0)) return NULL;
300 301 302 303
            result = op->defaults_tuple;
        } else {
            result = Py_None;
        }
304
    }
305 306 307
    Py_INCREF(result);
    return result;
}
308

309
static int
310
__Pyx_CyFunction_set_kwdefaults(__pyx_CyFunctionObject *op, PyObject* value, CYTHON_UNUSED void *context) {
311 312 313
    if (!value) {
        // del => explicit None to prevent rebuilding
        value = Py_None;
314
    } else if (unlikely(value != Py_None && !PyDict_Check(value))) {
315 316 317 318 319
        PyErr_SetString(PyExc_TypeError,
                        "__kwdefaults__ must be set to a dict object");
        return -1;
    }
    Py_INCREF(value);
320
    __Pyx_Py_XDECREF_SET(op->defaults_kwdict, value);
321 322
    return 0;
}
323

324
static PyObject *
325
__Pyx_CyFunction_get_kwdefaults(__pyx_CyFunctionObject *op, CYTHON_UNUSED void *context) {
326 327 328
    PyObject* result = op->defaults_kwdict;
    if (unlikely(!result)) {
        if (op->defaults_getter) {
329
            if (unlikely(__Pyx_CyFunction_init_defaults(op) < 0)) return NULL;
330 331 332
            result = op->defaults_kwdict;
        } else {
            result = Py_None;
333 334
        }
    }
335 336
    Py_INCREF(result);
    return result;
337 338
}

339
static int
340
__Pyx_CyFunction_set_annotations(__pyx_CyFunctionObject *op, PyObject* value, CYTHON_UNUSED void *context) {
341 342
    if (!value || value == Py_None) {
        value = NULL;
343
    } else if (unlikely(!PyDict_Check(value))) {
344 345 346 347 348
        PyErr_SetString(PyExc_TypeError,
                        "__annotations__ must be set to a dict object");
        return -1;
    }
    Py_XINCREF(value);
349
    __Pyx_Py_XDECREF_SET(op->func_annotations, value);
350 351 352 353
    return 0;
}

static PyObject *
354
__Pyx_CyFunction_get_annotations(__pyx_CyFunctionObject *op, CYTHON_UNUSED void *context) {
355 356 357 358 359 360 361 362 363 364
    PyObject* result = op->func_annotations;
    if (unlikely(!result)) {
        result = PyDict_New();
        if (unlikely(!result)) return NULL;
        op->func_annotations = result;
    }
    Py_INCREF(result);
    return result;
}

365 366
//#if PY_VERSION_HEX >= 0x030400C1
//static PyObject *
367
//__Pyx_CyFunction_get_signature(__pyx_CyFunctionObject *op, CYTHON_UNUSED void *context) {
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
//    PyObject *inspect_module, *signature_class, *signature;
//    // from inspect import Signature
//    inspect_module = PyImport_ImportModuleLevelObject(PYIDENT("inspect"), NULL, NULL, NULL, 0);
//    if (unlikely(!inspect_module))
//        goto bad;
//    signature_class = __Pyx_PyObject_GetAttrStr(inspect_module, PYIDENT("Signature"));
//    Py_DECREF(inspect_module);
//    if (unlikely(!signature_class))
//        goto bad;
//    // return Signature.from_function(op)
//    signature = PyObject_CallMethodObjArgs(signature_class, PYIDENT("from_function"), op, NULL);
//    Py_DECREF(signature_class);
//    if (likely(signature))
//        return signature;
//bad:
//    // make sure we raise an AttributeError from this property on any errors
//    if (!PyErr_ExceptionMatches(PyExc_AttributeError))
//        PyErr_SetString(PyExc_AttributeError, "failed to calculate __signature__");
//    return NULL;
//}
//#endif
389

390 391 392 393 394
static PyGetSetDef __pyx_CyFunction_getsets[] = {
    {(char *) "func_doc", (getter)__Pyx_CyFunction_get_doc, (setter)__Pyx_CyFunction_set_doc, 0, 0},
    {(char *) "__doc__",  (getter)__Pyx_CyFunction_get_doc, (setter)__Pyx_CyFunction_set_doc, 0, 0},
    {(char *) "func_name", (getter)__Pyx_CyFunction_get_name, (setter)__Pyx_CyFunction_set_name, 0, 0},
    {(char *) "__name__", (getter)__Pyx_CyFunction_get_name, (setter)__Pyx_CyFunction_set_name, 0, 0},
395
    {(char *) "__qualname__", (getter)__Pyx_CyFunction_get_qualname, (setter)__Pyx_CyFunction_set_qualname, 0, 0},
396 397 398 399 400 401 402 403 404
    {(char *) "__self__", (getter)__Pyx_CyFunction_get_self, 0, 0, 0},
    {(char *) "func_dict", (getter)__Pyx_CyFunction_get_dict, (setter)__Pyx_CyFunction_set_dict, 0, 0},
    {(char *) "__dict__", (getter)__Pyx_CyFunction_get_dict, (setter)__Pyx_CyFunction_set_dict, 0, 0},
    {(char *) "func_globals", (getter)__Pyx_CyFunction_get_globals, 0, 0, 0},
    {(char *) "__globals__", (getter)__Pyx_CyFunction_get_globals, 0, 0, 0},
    {(char *) "func_closure", (getter)__Pyx_CyFunction_get_closure, 0, 0, 0},
    {(char *) "__closure__", (getter)__Pyx_CyFunction_get_closure, 0, 0, 0},
    {(char *) "func_code", (getter)__Pyx_CyFunction_get_code, 0, 0, 0},
    {(char *) "__code__", (getter)__Pyx_CyFunction_get_code, 0, 0, 0},
405 406 407
    {(char *) "func_defaults", (getter)__Pyx_CyFunction_get_defaults, (setter)__Pyx_CyFunction_set_defaults, 0, 0},
    {(char *) "__defaults__", (getter)__Pyx_CyFunction_get_defaults, (setter)__Pyx_CyFunction_set_defaults, 0, 0},
    {(char *) "__kwdefaults__", (getter)__Pyx_CyFunction_get_kwdefaults, (setter)__Pyx_CyFunction_set_kwdefaults, 0, 0},
408
    {(char *) "__annotations__", (getter)__Pyx_CyFunction_get_annotations, (setter)__Pyx_CyFunction_set_annotations, 0, 0},
409 410 411
//#if PY_VERSION_HEX >= 0x030400C1
//    {(char *) "__signature__", (getter)__Pyx_CyFunction_get_signature, 0, 0, 0},
//#endif
412 413 414 415
    {0, 0, 0, 0, 0}
};

static PyMemberDef __pyx_CyFunction_members[] = {
416
    {(char *) "__module__", T_OBJECT, offsetof(PyCFunctionObject, m_module), PY_WRITE_RESTRICTED, 0},
417 418 419 420 421 422 423 424
#if CYTHON_COMPILING_IN_LIMITED_API
    {(char *) "__dictoffset__", T_PYSSIZET, offsetof(__pyx_CyFunctionObject, func_dict), READONLY, 0},
#if PY_VERSION_HEX < 0x030500A0
    {(char *) "__weaklistoffset__", T_PYSSIZET, offsetof(__pyx_CyFunctionObject, func_weakreflist), READONLY, 0},
#else
    {(char *) "__weaklistoffset__", T_PYSSIZET, offsetof(PyCFunctionObject, m_weakreflist), READONLY, 0},
#endif
#endif
425 426 427 428 429 430 431
    {0, 0, 0,  0, 0}
};

static PyObject *
__Pyx_CyFunction_reduce(__pyx_CyFunctionObject *m, CYTHON_UNUSED PyObject *args)
{
#if PY_MAJOR_VERSION >= 3
432 433
    Py_INCREF(m->func_qualname);
    return m->func_qualname;
434 435 436 437 438 439
#else
    return PyString_FromString(m->func.m_ml->ml_name);
#endif
}

static PyMethodDef __pyx_CyFunction_methods[] = {
440
    {"__reduce__", (PyCFunction)__Pyx_CyFunction_reduce, METH_VARARGS, 0},
441 442 443 444
    {0, 0, 0, 0}
};


445 446 447 448 449 450
#if PY_VERSION_HEX < 0x030500A0
#define __Pyx_CyFunction_weakreflist(cyfunc) ((cyfunc)->func_weakreflist)
#else
#define __Pyx_CyFunction_weakreflist(cyfunc) ((cyfunc)->func.m_weakreflist)
#endif

451 452 453
static PyObject *__Pyx_CyFunction_Init(__pyx_CyFunctionObject *op, PyMethodDef *ml, int flags, PyObject* qualname,
                                       PyObject *closure, PyObject *module, PyObject* globals, PyObject* code) {
    if (unlikely(op == NULL))
454
        return NULL;
455
    op->flags = flags;
456
    __Pyx_CyFunction_weakreflist(op) = NULL;
457
    op->func.m_ml = ml;
458 459 460
    op->func.m_self = (PyObject *) op;
    Py_XINCREF(closure);
    op->func_closure = closure;
461 462 463 464
    Py_XINCREF(module);
    op->func.m_module = module;
    op->func_dict = NULL;
    op->func_name = NULL;
465 466
    Py_INCREF(qualname);
    op->func_qualname = qualname;
467
    op->func_doc = NULL;
468
    op->func_classobj = NULL;
469 470
    op->func_globals = globals;
    Py_INCREF(op->func_globals);
471 472
    Py_XINCREF(code);
    op->func_code = code;
473
    // Dynamic Default args
474
    op->defaults_pyobjects = 0;
475
    op->defaults_size = 0;
476
    op->defaults = NULL;
477
    op->defaults_tuple = NULL;
478
    op->defaults_kwdict = NULL;
479
    op->defaults_getter = NULL;
480
    op->func_annotations = NULL;
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
#if CYTHON_METH_FASTCALL
    switch (ml->ml_flags & (METH_VARARGS | METH_FASTCALL | METH_NOARGS | METH_O | METH_KEYWORDS)) {
    case METH_NOARGS:
        __Pyx_CyFunction_func_vectorcall(op) = __Pyx_CyFunction_Vectorcall_NOARGS;
        break;
    case METH_O:
        __Pyx_CyFunction_func_vectorcall(op) = __Pyx_CyFunction_Vectorcall_O;
        break;
    // case METH_FASTCALL is not used
    case METH_FASTCALL | METH_KEYWORDS:
        __Pyx_CyFunction_func_vectorcall(op) = __Pyx_CyFunction_Vectorcall_FASTCALL_KEYWORDS;
        break;
    // case METH_VARARGS is not used
    case METH_VARARGS | METH_KEYWORDS:
        __Pyx_CyFunction_func_vectorcall(op) = NULL;
        break;
    default:
        PyErr_SetString(PyExc_SystemError, "Bad call flags for CyFunction");
499
        Py_DECREF(op);
500 501 502
        return NULL;
    }
#endif
503 504 505 506 507 508
    return (PyObject *) op;
}

static int
__Pyx_CyFunction_clear(__pyx_CyFunctionObject *m)
{
509
    Py_CLEAR(m->func_closure);
510 511 512
    Py_CLEAR(m->func.m_module);
    Py_CLEAR(m->func_dict);
    Py_CLEAR(m->func_name);
513
    Py_CLEAR(m->func_qualname);
514
    Py_CLEAR(m->func_doc);
515
    Py_CLEAR(m->func_globals);
516
    Py_CLEAR(m->func_code);
517
    Py_CLEAR(m->func_classobj);
518
    Py_CLEAR(m->defaults_tuple);
519
    Py_CLEAR(m->defaults_kwdict);
520
    Py_CLEAR(m->func_annotations);
521 522 523 524 525 526 527 528

    if (m->defaults) {
        PyObject **pydefaults = __Pyx_CyFunction_Defaults(PyObject *, m);
        int i;

        for (i = 0; i < m->defaults_pyobjects; i++)
            Py_XDECREF(pydefaults[i]);

529
        PyObject_Free(m->defaults);
530 531 532
        m->defaults = NULL;
    }

533 534 535
    return 0;
}

536
static void __Pyx__CyFunction_dealloc(__pyx_CyFunctionObject *m)
537
{
538
    if (__Pyx_CyFunction_weakreflist(m) != NULL)
539 540 541 542 543
        PyObject_ClearWeakRefs((PyObject *) m);
    __Pyx_CyFunction_clear(m);
    PyObject_GC_Del(m);
}

544 545 546 547 548 549
static void __Pyx_CyFunction_dealloc(__pyx_CyFunctionObject *m)
{
    PyObject_GC_UnTrack(m);
    __Pyx__CyFunction_dealloc(m);
}

550 551
static int __Pyx_CyFunction_traverse(__pyx_CyFunctionObject *m, visitproc visit, void *arg)
{
552
    Py_VISIT(m->func_closure);
553 554 555
    Py_VISIT(m->func.m_module);
    Py_VISIT(m->func_dict);
    Py_VISIT(m->func_name);
556
    Py_VISIT(m->func_qualname);
557
    Py_VISIT(m->func_doc);
558
    Py_VISIT(m->func_globals);
559
    Py_VISIT(m->func_code);
560
    Py_VISIT(m->func_classobj);
561
    Py_VISIT(m->defaults_tuple);
562
    Py_VISIT(m->defaults_kwdict);
563 564 565 566 567 568 569 570 571

    if (m->defaults) {
        PyObject **pydefaults = __Pyx_CyFunction_Defaults(PyObject *, m);
        int i;

        for (i = 0; i < m->defaults_pyobjects; i++)
            Py_VISIT(pydefaults[i]);
    }

572 573 574 575 576 577 578 579
    return 0;
}

static PyObject*
__Pyx_CyFunction_repr(__pyx_CyFunctionObject *op)
{
#if PY_MAJOR_VERSION >= 3
    return PyUnicode_FromFormat("<cyfunction %U at %p>",
580
                                op->func_qualname, (void *)op);
581 582
#else
    return PyString_FromFormat("<cyfunction %s at %p>",
583
                               PyString_AsString(op->func_qualname), (void *)op);
584 585 586
#endif
}

587 588
static PyObject * __Pyx_CyFunction_CallMethod(PyObject *func, PyObject *self, PyObject *arg, PyObject *kw) {
    // originally copied from PyCFunction_Call() in CPython's Objects/methodobject.c
589
    PyCFunctionObject* f = (PyCFunctionObject*)func;
590
    PyCFunction meth = f->m_ml->ml_meth;
591 592
    Py_ssize_t size;

593
    switch (f->m_ml->ml_flags & (METH_VARARGS | METH_KEYWORDS | METH_NOARGS | METH_O)) {
594
    case METH_VARARGS:
595
        if (likely(kw == NULL || PyDict_Size(kw) == 0))
596 597 598
            return (*meth)(self, arg);
        break;
    case METH_VARARGS | METH_KEYWORDS:
599
        return (*(PyCFunctionWithKeywords)(void*)meth)(self, arg, kw);
600
    case METH_NOARGS:
601
        if (likely(kw == NULL || PyDict_Size(kw) == 0)) {
602
            size = PyTuple_GET_SIZE(arg);
603
            if (likely(size == 0))
604 605
                return (*meth)(self, NULL);
            PyErr_Format(PyExc_TypeError,
606
                "%.200s() takes no arguments (%" CYTHON_FORMAT_SSIZE_T "d given)",
607 608 609 610 611
                f->m_ml->ml_name, size);
            return NULL;
        }
        break;
    case METH_O:
612
        if (likely(kw == NULL || PyDict_Size(kw) == 0)) {
613
            size = PyTuple_GET_SIZE(arg);
614
            if (likely(size == 1)) {
615 616 617 618 619 620
                PyObject *result, *arg0;
                #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
                arg0 = PyTuple_GET_ITEM(arg, 0);
                #else
                arg0 = PySequence_ITEM(arg, 0); if (unlikely(!arg0)) return NULL;
                #endif
621
                result = (*meth)(self, arg0);
622
                #if !(CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS)
623
                Py_DECREF(arg0);
624
                #endif
625 626
                return result;
            }
627
            PyErr_Format(PyExc_TypeError,
628
                "%.200s() takes exactly one argument (%" CYTHON_FORMAT_SSIZE_T "d given)",
629 630 631 632 633
                f->m_ml->ml_name, size);
            return NULL;
        }
        break;
    default:
634
        PyErr_SetString(PyExc_SystemError, "Bad call flags for CyFunction");
635 636 637 638 639 640
        return NULL;
    }
    PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
                 f->m_ml->ml_name);
    return NULL;
}
641 642 643 644 645 646 647 648

static CYTHON_INLINE PyObject *__Pyx_CyFunction_Call(PyObject *func, PyObject *arg, PyObject *kw) {
    return __Pyx_CyFunction_CallMethod(func, ((PyCFunctionObject*)func)->m_self, arg, kw);
}

static PyObject *__Pyx_CyFunction_CallAsMethod(PyObject *func, PyObject *args, PyObject *kw) {
    PyObject *result;
    __pyx_CyFunctionObject *cyfunc = (__pyx_CyFunctionObject *) func;
649 650

#if CYTHON_METH_FASTCALL
651 652
    // Prefer vectorcall if available. This is not the typical case, as
    // CPython would normally use vectorcall directly instead of tp_call.
653 654
     __pyx_vectorcallfunc vc = __Pyx_CyFunction_func_vectorcall(cyfunc);
    if (vc) {
655
#if CYTHON_ASSUME_SAFE_MACROS
656
        return __Pyx_PyVectorcall_FastCallDict(func, vc, &PyTuple_GET_ITEM(args, 0), PyTuple_GET_SIZE(args), kw);
657 658 659 660 661
#else
        // avoid unused function warning
        (void) &__Pyx_PyVectorcall_FastCallDict;
        return PyVectorcall_Call(func, args, kw);
#endif
662 663 664
    }
#endif

665
    if ((cyfunc->flags & __Pyx_CYFUNCTION_CCLASS) && !(cyfunc->flags & __Pyx_CYFUNCTION_STATICMETHOD)) {
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
        Py_ssize_t argc;
        PyObject *new_args;
        PyObject *self;

        argc = PyTuple_GET_SIZE(args);
        new_args = PyTuple_GetSlice(args, 1, argc);

        if (unlikely(!new_args))
            return NULL;

        self = PyTuple_GetItem(args, 0);
        if (unlikely(!self)) {
            Py_DECREF(new_args);
            return NULL;
        }

        result = __Pyx_CyFunction_CallMethod(func, self, new_args, kw);
        Py_DECREF(new_args);
    } else {
        result = __Pyx_CyFunction_Call(func, args, kw);
    }
    return result;
688
}
689

690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
#if CYTHON_METH_FASTCALL
// Check that kwnames is empty (if you want to allow keyword arguments,
// simply pass kwnames=NULL) and figure out what to do with "self".
// Return value:
//  1: self = args[0]
//  0: self = cyfunc->func.m_self
// -1: error
static CYTHON_INLINE int __Pyx_CyFunction_Vectorcall_CheckArgs(__pyx_CyFunctionObject *cyfunc, Py_ssize_t nargs, PyObject *kwnames)
{
    int ret = 0;
    if ((cyfunc->flags & __Pyx_CYFUNCTION_CCLASS) && !(cyfunc->flags & __Pyx_CYFUNCTION_STATICMETHOD)) {
        if (unlikely(nargs < 1)) {
            PyErr_Format(PyExc_TypeError, "%.200s() needs an argument",
                         cyfunc->func.m_ml->ml_name);
            return -1;
        }
        ret = 1;
    }
708
    if (unlikely(kwnames) && unlikely(PyTuple_GET_SIZE(kwnames))) {
709 710 711 712 713 714 715 716 717 718 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 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
        PyErr_Format(PyExc_TypeError,
                     "%.200s() takes no keyword arguments", cyfunc->func.m_ml->ml_name);
        return -1;
    }
    return ret;
}

static PyObject * __Pyx_CyFunction_Vectorcall_NOARGS(PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
{
    __pyx_CyFunctionObject *cyfunc = (__pyx_CyFunctionObject *)func;
    PyMethodDef* def = cyfunc->func.m_ml;
#if CYTHON_BACKPORT_VECTORCALL
    Py_ssize_t nargs = (Py_ssize_t)nargsf;
#else
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
#endif
    PyObject *self;
    switch (__Pyx_CyFunction_Vectorcall_CheckArgs(cyfunc, nargs, kwnames)) {
    case 1:
        self = args[0];
        args += 1;
        nargs -= 1;
        break;
    case 0:
        self = cyfunc->func.m_self;
        break;
    default:
        return NULL;
    }

    if (unlikely(nargs != 0)) {
        PyErr_Format(PyExc_TypeError,
            "%.200s() takes no arguments (%" CYTHON_FORMAT_SSIZE_T "d given)",
            def->ml_name, nargs);
        return NULL;
    }
    return def->ml_meth(self, NULL);
}

static PyObject * __Pyx_CyFunction_Vectorcall_O(PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
{
    __pyx_CyFunctionObject *cyfunc = (__pyx_CyFunctionObject *)func;
    PyMethodDef* def = cyfunc->func.m_ml;
#if CYTHON_BACKPORT_VECTORCALL
    Py_ssize_t nargs = (Py_ssize_t)nargsf;
#else
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
#endif
    PyObject *self;
    switch (__Pyx_CyFunction_Vectorcall_CheckArgs(cyfunc, nargs, kwnames)) {
    case 1:
        self = args[0];
        args += 1;
        nargs -= 1;
        break;
    case 0:
        self = cyfunc->func.m_self;
        break;
    default:
        return NULL;
    }

    if (unlikely(nargs != 1)) {
        PyErr_Format(PyExc_TypeError,
            "%.200s() takes exactly one argument (%" CYTHON_FORMAT_SSIZE_T "d given)",
            def->ml_name, nargs);
        return NULL;
    }
    return def->ml_meth(self, args[0]);
}

static PyObject * __Pyx_CyFunction_Vectorcall_FASTCALL_KEYWORDS(PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames)
{
    __pyx_CyFunctionObject *cyfunc = (__pyx_CyFunctionObject *)func;
    PyMethodDef* def = cyfunc->func.m_ml;
#if CYTHON_BACKPORT_VECTORCALL
    Py_ssize_t nargs = (Py_ssize_t)nargsf;
#else
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
#endif
    PyObject *self;
    switch (__Pyx_CyFunction_Vectorcall_CheckArgs(cyfunc, nargs, NULL)) {
    case 1:
        self = args[0];
        args += 1;
        nargs -= 1;
        break;
    case 0:
        self = cyfunc->func.m_self;
        break;
    default:
        return NULL;
    }

    return ((_PyCFunctionFastWithKeywords)(void(*)(void))def->ml_meth)(self, args, nargs, kwnames);
}
#endif

807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
#if CYTHON_COMPILING_IN_LIMITED_API
static PyType_Slot __pyx_CyFunctionType_slots[] = {
    {Py_tp_dealloc, (void *)__Pyx_CyFunction_dealloc},
    {Py_tp_repr, (void *)__Pyx_CyFunction_repr},
    {Py_tp_call, (void *)__Pyx_CyFunction_CallAsMethod},
    {Py_tp_traverse, (void *)__Pyx_CyFunction_traverse},
    {Py_tp_clear, (void *)__Pyx_CyFunction_clear},
    {Py_tp_methods, (void *)__pyx_CyFunction_methods},
    {Py_tp_members, (void *)__pyx_CyFunction_members},
    {Py_tp_getset, (void *)__pyx_CyFunction_getsets},
    {Py_tp_descr_get, (void *)__Pyx_PyMethod_New},
    {0, 0},
};

static PyType_Spec __pyx_CyFunctionType_spec = {
822
    __PYX_TYPE_MODULE_PREFIX "cython_function_or_method",
823 824 825 826 827 828 829
    sizeof(__pyx_CyFunctionObject),
    0,
    // TODO: Support _Py_TPFLAGS_HAVE_VECTORCALL and _Py_TPFLAGS_HAVE_VECTORCALL
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
    __pyx_CyFunctionType_slots
};
#else
830 831
static PyTypeObject __pyx_CyFunctionType_type = {
    PyVarObject_HEAD_INIT(0, 0)
832
    __PYX_TYPE_MODULE_PREFIX "cython_function_or_method",  /*tp_name*/
833 834 835
    sizeof(__pyx_CyFunctionObject),   /*tp_basicsize*/
    0,                                  /*tp_itemsize*/
    (destructor) __Pyx_CyFunction_dealloc, /*tp_dealloc*/
836
#if !CYTHON_METH_FASTCALL
837
    0,                                  /*tp_print*/
838 839 840 841 842
#elif CYTHON_BACKPORT_VECTORCALL
    (printfunc)offsetof(__pyx_CyFunctionObject, func_vectorcall), /*tp_vectorcall_offset backported into tp_print*/
#else
    offsetof(__pyx_CyFunctionObject, func.vectorcall), /*tp_vectorcall_offset*/
#endif
843 844 845 846 847
    0,                                  /*tp_getattr*/
    0,                                  /*tp_setattr*/
#if PY_MAJOR_VERSION < 3
    0,                                  /*tp_compare*/
#else
848
    0,                                  /*tp_as_async*/
849 850 851 852 853 854
#endif
    (reprfunc) __Pyx_CyFunction_repr,   /*tp_repr*/
    0,                                  /*tp_as_number*/
    0,                                  /*tp_as_sequence*/
    0,                                  /*tp_as_mapping*/
    0,                                  /*tp_hash*/
855
    __Pyx_CyFunction_CallAsMethod,      /*tp_call*/
856 857 858 859
    0,                                  /*tp_str*/
    0,                                  /*tp_getattro*/
    0,                                  /*tp_setattro*/
    0,                                  /*tp_as_buffer*/
860 861
#ifdef Py_TPFLAGS_METHOD_DESCRIPTOR
    Py_TPFLAGS_METHOD_DESCRIPTOR |
862 863 864
#endif
#ifdef _Py_TPFLAGS_HAVE_VECTORCALL
    _Py_TPFLAGS_HAVE_VECTORCALL |
865
#endif
866
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
867 868 869 870
    0,                                  /*tp_doc*/
    (traverseproc) __Pyx_CyFunction_traverse,   /*tp_traverse*/
    (inquiry) __Pyx_CyFunction_clear,   /*tp_clear*/
    0,                                  /*tp_richcompare*/
871
#if PY_VERSION_HEX < 0x030500A0
872
    offsetof(__pyx_CyFunctionObject, func_weakreflist), /*tp_weaklistoffset*/
873 874 875
#else
    offsetof(PyCFunctionObject, m_weakreflist),         /*tp_weaklistoffset*/
#endif
876 877 878 879 880 881 882
    0,                                  /*tp_iter*/
    0,                                  /*tp_iternext*/
    __pyx_CyFunction_methods,           /*tp_methods*/
    __pyx_CyFunction_members,           /*tp_members*/
    __pyx_CyFunction_getsets,           /*tp_getset*/
    0,                                  /*tp_base*/
    0,                                  /*tp_dict*/
883
    __Pyx_PyMethod_New,                 /*tp_descr_get*/
884 885 886 887 888 889 890 891 892 893 894 895 896 897
    0,                                  /*tp_descr_set*/
    offsetof(__pyx_CyFunctionObject, func_dict),/*tp_dictoffset*/
    0,                                  /*tp_init*/
    0,                                  /*tp_alloc*/
    0,                                  /*tp_new*/
    0,                                  /*tp_free*/
    0,                                  /*tp_is_gc*/
    0,                                  /*tp_bases*/
    0,                                  /*tp_mro*/
    0,                                  /*tp_cache*/
    0,                                  /*tp_subclasses*/
    0,                                  /*tp_weaklist*/
    0,                                  /*tp_del*/
    0,                                  /*tp_version_tag*/
898
#if PY_VERSION_HEX >= 0x030400a1
899 900
    0,                                  /*tp_finalize*/
#endif
901 902 903
#if PY_VERSION_HEX >= 0x030800b1
    0,                                  /*tp_vectorcall*/
#endif
904 905 906
#if PY_VERSION_HEX >= 0x030800b4 && PY_VERSION_HEX < 0x03090000
    0,                                  /*tp_print*/
#endif
907 908 909
#if CYTHON_COMPILING_IN_PYPY && PYPY_VERSION_NUM+0 >= 0x06000000
    0,                                          /*tp_pypy_flags*/
#endif
910
};
911
#endif  /* CYTHON_COMPILING_IN_LIMITED_API */
912 913


914
static int __pyx_CyFunction_init(void) {
915 916 917
#if CYTHON_COMPILING_IN_LIMITED_API
    __pyx_CyFunctionType = __Pyx_FetchCommonTypeFromSpec(&__pyx_CyFunctionType_spec, NULL);
#else
918
    __pyx_CyFunctionType = __Pyx_FetchCommonType(&__pyx_CyFunctionType_type);
919
#endif
920
    if (unlikely(__pyx_CyFunctionType == NULL)) {
921
        return -1;
922
    }
923 924 925
    return 0;
}

926
static CYTHON_INLINE void *__Pyx_CyFunction_InitDefaults(PyObject *func, size_t size, int pyobjects) {
927 928
    __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func;

929
    m->defaults = PyObject_Malloc(size);
930
    if (unlikely(!m->defaults))
931
        return PyErr_NoMemory();
932
    memset(m->defaults, 0, size);
933
    m->defaults_pyobjects = pyobjects;
934
    m->defaults_size = size;
935 936
    return m->defaults;
}
937

938
static CYTHON_INLINE void __Pyx_CyFunction_SetDefaultsTuple(PyObject *func, PyObject *tuple) {
939 940 941 942
    __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func;
    m->defaults_tuple = tuple;
    Py_INCREF(tuple);
}
943

944 945 946 947 948 949
static CYTHON_INLINE void __Pyx_CyFunction_SetDefaultsKwDict(PyObject *func, PyObject *dict) {
    __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func;
    m->defaults_kwdict = dict;
    Py_INCREF(dict);
}

950 951 952 953 954 955
static CYTHON_INLINE void __Pyx_CyFunction_SetAnnotationsDict(PyObject *func, PyObject *dict) {
    __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func;
    m->func_annotations = dict;
    Py_INCREF(dict);
}

956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980

//////////////////// CythonFunction.proto ////////////////////

static PyObject *__Pyx_CyFunction_New(PyMethodDef *ml,
                                      int flags, PyObject* qualname,
                                      PyObject *closure,
                                      PyObject *module, PyObject *globals,
                                      PyObject* code);

//////////////////// CythonFunction ////////////////////
//@requires: CythonFunctionShared

static PyObject *__Pyx_CyFunction_New(PyMethodDef *ml, int flags, PyObject* qualname,
                                      PyObject *closure, PyObject *module, PyObject* globals, PyObject* code) {
    PyObject *op = __Pyx_CyFunction_Init(
        PyObject_GC_New(__pyx_CyFunctionObject, __pyx_CyFunctionType),
        ml, flags, qualname, closure, module, globals, code
    );
    if (likely(op)) {
        PyObject_GC_Track(op);
    }
    return op;
}


981
//////////////////// CyFunctionClassCell.proto ////////////////////
982
static int __Pyx_CyFunction_InitClassCell(PyObject *cyfunctions, PyObject *classobj);/*proto*/
983 984

//////////////////// CyFunctionClassCell ////////////////////
985
//@requires: CythonFunctionShared
986

987
static int __Pyx_CyFunction_InitClassCell(PyObject *cyfunctions, PyObject *classobj) {
988
    Py_ssize_t i, count = PyList_GET_SIZE(cyfunctions);
989

990 991
    for (i = 0; i < count; i++) {
        __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *)
992
#if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
993 994 995 996 997 998
            PyList_GET_ITEM(cyfunctions, i);
#else
            PySequence_ITEM(cyfunctions, i);
        if (unlikely(!m))
            return -1;
#endif
999
        Py_INCREF(classobj);
1000
        m->func_classobj = classobj;
1001
#if !(CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS)
1002 1003
        Py_DECREF((PyObject*)m);
#endif
1004
    }
1005
    return 0;
1006
}
1007

1008

1009
//////////////////// FusedFunction.proto ////////////////////
1010

1011 1012 1013 1014 1015 1016
typedef struct {
    __pyx_CyFunctionObject func;
    PyObject *__signatures__;
    PyObject *self;
} __pyx_FusedFunctionObject;

1017
static PyObject *__pyx_FusedFunction_New(PyMethodDef *ml, int flags,
1018
                                         PyObject *qualname, PyObject *closure,
1019
                                         PyObject *module, PyObject *globals,
1020 1021
                                         PyObject *code);

1022
static int __pyx_FusedFunction_clear(__pyx_FusedFunctionObject *self);
1023
#if !CYTHON_COMPILING_IN_LIMITED_API
1024
static PyTypeObject *__pyx_FusedFunctionType = NULL;
1025
#endif
1026 1027 1028 1029 1030
static int __pyx_FusedFunction_init(void);

#define __Pyx_FusedFunction_USED

//////////////////// FusedFunction ////////////////////
1031
//@requires: CythonFunctionShared
1032

1033
static PyObject *
1034
__pyx_FusedFunction_New(PyMethodDef *ml, int flags,
1035
                        PyObject *qualname, PyObject *closure,
1036 1037
                        PyObject *module, PyObject *globals,
                        PyObject *code)
1038
{
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
    PyObject *op = __Pyx_CyFunction_Init(
        // __pyx_CyFunctionObject is correct below since that's the cast that we want.
        PyObject_GC_New(__pyx_CyFunctionObject, __pyx_FusedFunctionType),
        ml, flags, qualname, closure, module, globals, code
    );
    if (likely(op)) {
        __pyx_FusedFunctionObject *fusedfunc = (__pyx_FusedFunctionObject *) op;
        fusedfunc->__signatures__ = NULL;
        fusedfunc->self = NULL;
        PyObject_GC_Track(op);
    }
    return op;
1051 1052
}

1053 1054 1055 1056 1057 1058 1059
static void
__pyx_FusedFunction_dealloc(__pyx_FusedFunctionObject *self)
{
    PyObject_GC_UnTrack(self);
    Py_CLEAR(self->self);
    Py_CLEAR(self->__signatures__);
    __Pyx__CyFunction_dealloc((__pyx_CyFunctionObject *) self);
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
}

static int
__pyx_FusedFunction_traverse(__pyx_FusedFunctionObject *self,
                             visitproc visit,
                             void *arg)
{
    Py_VISIT(self->self);
    Py_VISIT(self->__signatures__);
    return __Pyx_CyFunction_traverse((__pyx_CyFunctionObject *) self, visit, arg);
}

static int
__pyx_FusedFunction_clear(__pyx_FusedFunctionObject *self)
{
    Py_CLEAR(self->self);
    Py_CLEAR(self->__signatures__);
    return __Pyx_CyFunction_clear((__pyx_CyFunctionObject *) self);
}


static PyObject *
__pyx_FusedFunction_descr_get(PyObject *self, PyObject *obj, PyObject *type)
{
    __pyx_FusedFunctionObject *func, *meth;

    func = (__pyx_FusedFunctionObject *) self;

1088
    if (func->self || func->func.flags & __Pyx_CYFUNCTION_STATICMETHOD) {
1089
        // Do not allow rebinding and don't do anything for static methods
1090 1091 1092 1093 1094 1095 1096
        Py_INCREF(self);
        return self;
    }

    if (obj == Py_None)
        obj = NULL;

1097
    meth = (__pyx_FusedFunctionObject *) __pyx_FusedFunction_New(
1098
                    ((PyCFunctionObject *) func)->m_ml,
1099
                    ((__pyx_CyFunctionObject *) func)->flags,
1100
                    ((__pyx_CyFunctionObject *) func)->func_qualname,
1101
                    ((__pyx_CyFunctionObject *) func)->func_closure,
1102
                    ((PyCFunctionObject *) func)->m_module,
1103
                    ((__pyx_CyFunctionObject *) func)->func_globals,
1104
                    ((__pyx_CyFunctionObject *) func)->func_code);
1105
    if (unlikely(!meth))
1106 1107
        return NULL;

1108 1109 1110 1111 1112 1113 1114
    // defaults needs copying fully rather than just copying the pointer
    // since otherwise it will be freed on destruction of meth despite
    // belonging to func rather than meth
    if (func->func.defaults) {
        PyObject **pydefaults;
        int i;

1115 1116 1117 1118
        if (unlikely(!__Pyx_CyFunction_InitDefaults(
                (PyObject*)meth,
                func->func.defaults_size,
                func->func.defaults_pyobjects))) {
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
            Py_XDECREF((PyObject*)meth);
            return NULL;
        }
        memcpy(meth->func.defaults, func->func.defaults, func->func.defaults_size);

        pydefaults = __Pyx_CyFunction_Defaults(PyObject *, meth);
        for (i = 0; i < meth->func.defaults_pyobjects; i++)
            Py_XINCREF(pydefaults[i]);
    }

1129 1130 1131
    Py_XINCREF(func->func.func_classobj);
    meth->func.func_classobj = func->func.func_classobj;

1132 1133 1134
    Py_XINCREF(func->__signatures__);
    meth->__signatures__ = func->__signatures__;

1135 1136 1137
    Py_XINCREF(func->func.defaults_tuple);
    meth->func.defaults_tuple = func->func.defaults_tuple;

1138 1139 1140
    if (func->func.flags & __Pyx_CYFUNCTION_CLASSMETHOD)
        obj = type;

1141 1142 1143 1144 1145 1146
    Py_XINCREF(obj);
    meth->self = obj;

    return (PyObject *) meth;
}

1147
static PyObject *
1148
_obj_to_string(PyObject *obj)
1149
{
1150 1151 1152
    if (PyUnicode_CheckExact(obj))
        return __Pyx_NewRef(obj);
#if PY_MAJOR_VERSION == 2
1153 1154
    else if (PyString_Check(obj))
        return PyUnicode_FromEncodedObject(obj, NULL, "strict");
1155 1156
#endif
    else if (PyType_Check(obj))
1157
        return PyObject_GetAttr(obj, PYIDENT("__name__"));
1158
    else
1159
        return PyObject_Unicode(obj);
1160 1161
}

1162 1163 1164 1165 1166 1167 1168
static PyObject *
__pyx_FusedFunction_getitem(__pyx_FusedFunctionObject *self, PyObject *idx)
{
    PyObject *signature = NULL;
    PyObject *unbound_result_func;
    PyObject *result_func = NULL;

1169
    if (unlikely(self->__signatures__ == NULL)) {
1170 1171 1172 1173 1174 1175
        PyErr_SetString(PyExc_TypeError, "Function is not fused");
        return NULL;
    }

    if (PyTuple_Check(idx)) {
        Py_ssize_t n = PyTuple_GET_SIZE(idx);
1176
        PyObject *list = PyList_New(n);
1177 1178
        int i;

1179
        if (unlikely(!list))
1180 1181 1182
            return NULL;

        for (i = 0; i < n; i++) {
1183
            PyObject *string;
1184
#if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
1185
            PyObject *item = PyTuple_GET_ITEM(idx, i);
1186
#else
Stefan Behnel's avatar
Stefan Behnel committed
1187
            PyObject *item = PySequence_ITEM(idx, i);  if (unlikely(!item)) goto __pyx_err;
1188
#endif
1189
            string = _obj_to_string(item);
1190
#if !(CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS)
1191 1192
            Py_DECREF(item);
#endif
1193
            if (unlikely(!string)) goto __pyx_err;
1194
            PyList_SET_ITEM(list, i, string);
1195 1196
        }

1197 1198
        signature = PyUnicode_Join(PYUNICODE("|"), list);
__pyx_err:;
1199 1200
        Py_DECREF(list);
    } else {
1201
        signature = _obj_to_string(idx);
1202 1203
    }

1204
    if (unlikely(!signature))
1205 1206 1207 1208
        return NULL;

    unbound_result_func = PyObject_GetItem(self->__signatures__, signature);

1209
    if (likely(unbound_result_func)) {
1210
        if (self->self) {
1211 1212
            __pyx_FusedFunctionObject *unbound = (__pyx_FusedFunctionObject *) unbound_result_func;

1213
            // TODO: move this to InitClassCell
1214
            Py_XINCREF(self->func.func_classobj);
1215
            __Pyx_Py_XDECREF_SET(unbound->func.func_classobj, self->func.func_classobj);
1216 1217

            result_func = __pyx_FusedFunction_descr_get(unbound_result_func,
1218
                                                        self->self, self->self);
1219 1220 1221 1222
        } else {
            result_func = unbound_result_func;
            Py_INCREF(result_func);
        }
1223
    }
1224 1225 1226 1227 1228 1229 1230

    Py_DECREF(signature);
    Py_XDECREF(unbound_result_func);

    return result_func;
}

1231 1232 1233 1234 1235 1236 1237
static PyObject *
__pyx_FusedFunction_callfunction(PyObject *func, PyObject *args, PyObject *kw)
{
     __pyx_CyFunctionObject *cyfunc = (__pyx_CyFunctionObject *) func;
    int static_specialized = (cyfunc->flags & __Pyx_CYFUNCTION_STATICMETHOD &&
                              !((__pyx_FusedFunctionObject *) func)->__signatures__);

1238
    if ((cyfunc->flags & __Pyx_CYFUNCTION_CCLASS) && !static_specialized) {
1239
        return __Pyx_CyFunction_CallAsMethod(func, args, kw);
1240
    } else {
1241
        return __Pyx_CyFunction_Call(func, args, kw);
1242 1243 1244
    }
}

1245 1246 1247 1248 1249 1250
// Note: the 'self' from method binding is passed in in the args tuple,
//       whereas PyCFunctionObject's m_self is passed in as the first
//       argument to the C function. For extension methods we need
//       to pass 'self' as 'm_self' and not as the first element of the
//       args tuple.

1251 1252 1253 1254 1255 1256
static PyObject *
__pyx_FusedFunction_call(PyObject *func, PyObject *args, PyObject *kw)
{
    __pyx_FusedFunctionObject *binding_func = (__pyx_FusedFunctionObject *) func;
    Py_ssize_t argc = PyTuple_GET_SIZE(args);
    PyObject *new_args = NULL;
1257
    __pyx_FusedFunctionObject *new_func = NULL;
1258
    PyObject *result = NULL;
1259
    int is_staticmethod = binding_func->func.flags & __Pyx_CYFUNCTION_STATICMETHOD;
1260

1261
    if (binding_func->self) {
1262
        // Bound method call, put 'self' in the args tuple
1263
        PyObject *self;
1264 1265
        Py_ssize_t i;
        new_args = PyTuple_New(argc + 1);
1266
        if (unlikely(!new_args))
1267 1268 1269
            return NULL;

        self = binding_func->self;
1270

1271 1272
        Py_INCREF(self);
        PyTuple_SET_ITEM(new_args, 0, self);
1273
        self = NULL;
1274 1275

        for (i = 0; i < argc; i++) {
1276
#if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
1277 1278
            PyObject *item = PyTuple_GET_ITEM(args, i);
            Py_INCREF(item);
1279 1280 1281
#else
            PyObject *item = PySequence_ITEM(args, i);  if (unlikely(!item)) goto bad;
#endif
1282 1283 1284 1285 1286 1287 1288
            PyTuple_SET_ITEM(new_args, i + 1, item);
        }

        args = new_args;
    }

    if (binding_func->__signatures__) {
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
        PyObject *tup;
        if (is_staticmethod && binding_func->func.flags & __Pyx_CYFUNCTION_CCLASS) {
            // FIXME: this seems wrong, but we must currently pass the signatures dict as 'self' argument
            tup = PyTuple_Pack(3, args,
                               kw == NULL ? Py_None : kw,
                               binding_func->func.defaults_tuple);
            if (unlikely(!tup)) goto bad;
            new_func = (__pyx_FusedFunctionObject *) __Pyx_CyFunction_CallMethod(
                func, binding_func->__signatures__, tup, NULL);
        } else {
            tup = PyTuple_Pack(4, binding_func->__signatures__, args,
                               kw == NULL ? Py_None : kw,
                               binding_func->func.defaults_tuple);
            if (unlikely(!tup)) goto bad;
            new_func = (__pyx_FusedFunctionObject *) __pyx_FusedFunction_callfunction(func, tup, NULL);
        }
1305 1306
        Py_DECREF(tup);

1307
        if (unlikely(!new_func))
1308
            goto bad;
1309 1310

        Py_XINCREF(binding_func->func.func_classobj);
1311
        __Pyx_Py_XDECREF_SET(new_func->func.func_classobj, binding_func->func.func_classobj);
1312 1313

        func = (PyObject *) new_func;
1314 1315
    }

1316
    result = __pyx_FusedFunction_callfunction(func, args, kw);
1317
bad:
1318
    Py_XDECREF(new_args);
1319
    Py_XDECREF((PyObject *) new_func);
1320 1321 1322 1323 1324 1325 1326
    return result;
}

static PyMemberDef __pyx_FusedFunction_members[] = {
    {(char *) "__signatures__",
     T_OBJECT,
     offsetof(__pyx_FusedFunctionObject, __signatures__),
1327
     READONLY,
1328
     0},
Stefan Behnel's avatar
Stefan Behnel committed
1329
    {0, 0, 0, 0, 0},
1330 1331
};

1332 1333 1334 1335 1336 1337 1338 1339 1340
#if CYTHON_COMPILING_IN_LIMITED_API
static PyType_Slot __pyx_FusedFunctionType_slots[] = {
    {Py_tp_dealloc, (void *)__pyx_FusedFunction_dealloc},
    {Py_tp_call, (void *)__pyx_FusedFunction_call},
    {Py_tp_traverse, (void *)__pyx_FusedFunction_traverse},
    {Py_tp_clear, (void *)__pyx_FusedFunction_clear},
    {Py_tp_members, (void *)__pyx_FusedFunction_members},
    {Py_tp_getset, (void *)__pyx_CyFunction_getsets},
    {Py_tp_descr_get, (void *)__pyx_FusedFunction_descr_get},
1341
    {Py_mp_subscript, (void *)__pyx_FusedFunction_getitem},
1342 1343 1344 1345
    {0, 0},
};

static PyType_Spec __pyx_FusedFunctionType_spec = {
1346
    __PYX_TYPE_MODULE_PREFIX "fused_cython_function",
1347 1348 1349 1350 1351
    sizeof(__pyx_FusedFunctionObject),
    0,
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /*tp_flags*/
    __pyx_FusedFunctionType_slots
};
1352 1353 1354 1355 1356 1357 1358 1359 1360

#else  /* !CYTHON_COMPILING_IN_LIMITED_API */

static PyMappingMethods __pyx_FusedFunction_mapping_methods = {
    0,
    (binaryfunc) __pyx_FusedFunction_getitem,
    0,
};

1361 1362
static PyTypeObject __pyx_FusedFunctionType_type = {
    PyVarObject_HEAD_INIT(0, 0)
1363
    __PYX_TYPE_MODULE_PREFIX "fused_cython_function",  /*tp_name*/
1364 1365 1366 1367 1368 1369 1370 1371 1372
    sizeof(__pyx_FusedFunctionObject), /*tp_basicsize*/
    0,                                  /*tp_itemsize*/
    (destructor) __pyx_FusedFunction_dealloc, /*tp_dealloc*/
    0,                                  /*tp_print*/
    0,                                  /*tp_getattr*/
    0,                                  /*tp_setattr*/
#if PY_MAJOR_VERSION < 3
    0,                                  /*tp_compare*/
#else
1373
    0,                                  /*tp_as_async*/
1374 1375 1376 1377 1378 1379
#endif
    0,                                  /*tp_repr*/
    0,                                  /*tp_as_number*/
    0,                                  /*tp_as_sequence*/
    &__pyx_FusedFunction_mapping_methods, /*tp_as_mapping*/
    0,                                  /*tp_hash*/
1380
    (ternaryfunc) __pyx_FusedFunction_call, /*tp_call*/
1381 1382 1383 1384
    0,                                  /*tp_str*/
    0,                                  /*tp_getattro*/
    0,                                  /*tp_setattro*/
    0,                                  /*tp_as_buffer*/
1385
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /*tp_flags*/
1386 1387 1388 1389 1390 1391 1392 1393 1394
    0,                                  /*tp_doc*/
    (traverseproc) __pyx_FusedFunction_traverse,   /*tp_traverse*/
    (inquiry) __pyx_FusedFunction_clear,/*tp_clear*/
    0,                                  /*tp_richcompare*/
    0,                                  /*tp_weaklistoffset*/
    0,                                  /*tp_iter*/
    0,                                  /*tp_iternext*/
    0,                                  /*tp_methods*/
    __pyx_FusedFunction_members,        /*tp_members*/
1395 1396
    // __doc__ is None for the fused function type, but we need it to be
    // a descriptor for the instance's __doc__, so rebuild descriptors in our subclass
1397
    __pyx_CyFunction_getsets,           /*tp_getset*/
1398
    // NOTE: tp_base may be changed later during module initialisation when importing CyFunction across modules.
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
    &__pyx_CyFunctionType_type,         /*tp_base*/
    0,                                  /*tp_dict*/
    __pyx_FusedFunction_descr_get,      /*tp_descr_get*/
    0,                                  /*tp_descr_set*/
    0,                                  /*tp_dictoffset*/
    0,                                  /*tp_init*/
    0,                                  /*tp_alloc*/
    0,                                  /*tp_new*/
    0,                                  /*tp_free*/
    0,                                  /*tp_is_gc*/
    0,                                  /*tp_bases*/
    0,                                  /*tp_mro*/
    0,                                  /*tp_cache*/
    0,                                  /*tp_subclasses*/
    0,                                  /*tp_weaklist*/
    0,                                  /*tp_del*/
    0,                                  /*tp_version_tag*/
1416
#if PY_VERSION_HEX >= 0x030400a1
1417 1418
    0,                                  /*tp_finalize*/
#endif
1419 1420 1421
#if PY_VERSION_HEX >= 0x030800b1
    0,                                  /*tp_vectorcall*/
#endif
1422 1423 1424
#if PY_VERSION_HEX >= 0x030800b4 && PY_VERSION_HEX < 0x03090000
    0,                                  /*tp_print*/
#endif
1425 1426 1427
#if CYTHON_COMPILING_IN_PYPY && PYPY_VERSION_NUM+0 >= 0x06000000
    0,                                          /*tp_pypy_flags*/
#endif
1428
};
1429
#endif
1430 1431

static int __pyx_FusedFunction_init(void) {
1432 1433 1434 1435 1436 1437 1438 1439
#if CYTHON_COMPILING_IN_LIMITED_API
    PyObject *bases = PyTuple_Pack(1, __pyx_CyFunctionType);
    if (unlikely(!bases)) {
        return -1;
    }
    __pyx_FusedFunctionType = __Pyx_FetchCommonTypeFromSpec(&__pyx_FusedFunctionType_spec, bases);
    Py_DECREF(bases);
#else
1440 1441
    // Set base from __Pyx_FetchCommonTypeFromSpec, in case it's different from the local static value.
    __pyx_FusedFunctionType_type.tp_base = __pyx_CyFunctionType;
1442
    __pyx_FusedFunctionType = __Pyx_FetchCommonType(&__pyx_FusedFunctionType_type);
1443
#endif
1444
    if (unlikely(__pyx_FusedFunctionType == NULL)) {
1445 1446 1447 1448
        return -1;
    }
    return 0;
}
1449 1450 1451 1452

//////////////////// ClassMethod.proto ////////////////////

#include "descrobject.h"
1453
static CYTHON_UNUSED PyObject* __Pyx_Method_ClassMethod(PyObject *method); /*proto*/
1454 1455 1456 1457

//////////////////// ClassMethod ////////////////////

static PyObject* __Pyx_Method_ClassMethod(PyObject *method) {
1458
#if CYTHON_COMPILING_IN_PYPY && PYPY_VERSION_NUM <= 0x05080000
1459 1460
    if (PyObject_TypeCheck(method, &PyWrapperDescr_Type)) {
        // cdef classes
1461 1462
        return PyClassMethod_New(method);
    }
Boxiang Sun's avatar
Boxiang Sun committed
1463
#else
1464 1465
#if CYTHON_COMPILING_IN_PYSTON || CYTHON_COMPILING_IN_PYPY
    // special C-API function only in Pyston and PyPy >= 5.9
1466
    if (PyMethodDescr_Check(method))
1467
#else
1468 1469
    #if PY_MAJOR_VERSION == 2
    // PyMethodDescr_Type is not exposed in the CPython C-API in Py2.
1470
    static PyTypeObject *methoddescr_type = NULL;
1471
    if (unlikely(methoddescr_type == NULL)) {
1472
       PyObject *meth = PyObject_GetAttrString((PyObject*)&PyList_Type, "append");
1473
       if (unlikely(!meth)) return NULL;
1474 1475 1476
       methoddescr_type = Py_TYPE(meth);
       Py_DECREF(meth);
    }
1477 1478 1479
    #else
    PyTypeObject *methoddescr_type = &PyMethodDescr_Type;
    #endif
1480
    if (__Pyx_TypeCheck(method, methoddescr_type))
Boxiang Sun's avatar
Boxiang Sun committed
1481
#endif
1482
    {
1483
        // cdef classes
1484 1485 1486 1487 1488 1489 1490 1491 1492
        PyMethodDescrObject *descr = (PyMethodDescrObject *)method;
        #if PY_VERSION_HEX < 0x03020000
        PyTypeObject *d_type = descr->d_type;
        #else
        PyTypeObject *d_type = descr->d_common.d_type;
        #endif
        return PyDescr_NewClassMethod(d_type, descr->d_method);
    }
#endif
1493 1494
    else if (PyMethod_Check(method)) {
        // python classes
1495 1496
        return PyClassMethod_New(PyMethod_GET_FUNCTION(method));
    }
1497
    else {
1498 1499 1500
        return PyClassMethod_New(method);
    }
}