CythonFunction.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  1. //////////////////// CythonFunction.proto ////////////////////
  2. #define __Pyx_CyFunction_USED 1
  3. #define __Pyx_CYFUNCTION_STATICMETHOD 0x01
  4. #define __Pyx_CYFUNCTION_CLASSMETHOD 0x02
  5. #define __Pyx_CYFUNCTION_CCLASS 0x04
  6. #define __Pyx_CyFunction_GetClosure(f) \
  7. (((__pyx_CyFunctionObject *) (f))->func_closure)
  8. #define __Pyx_CyFunction_GetClassObj(f) \
  9. (((__pyx_CyFunctionObject *) (f))->func_classobj)
  10. #define __Pyx_CyFunction_Defaults(type, f) \
  11. ((type *)(((__pyx_CyFunctionObject *) (f))->defaults))
  12. #define __Pyx_CyFunction_SetDefaultsGetter(f, g) \
  13. ((__pyx_CyFunctionObject *) (f))->defaults_getter = (g)
  14. typedef struct {
  15. PyCFunctionObject func;
  16. #if PY_VERSION_HEX < 0x030500A0
  17. PyObject *func_weakreflist;
  18. #endif
  19. PyObject *func_dict;
  20. PyObject *func_name;
  21. PyObject *func_qualname;
  22. PyObject *func_doc;
  23. PyObject *func_globals;
  24. PyObject *func_code;
  25. PyObject *func_closure;
  26. // No-args super() class cell
  27. PyObject *func_classobj;
  28. // Dynamic default args and annotations
  29. void *defaults;
  30. int defaults_pyobjects;
  31. int flags;
  32. // Defaults info
  33. PyObject *defaults_tuple; /* Const defaults tuple */
  34. PyObject *defaults_kwdict; /* Const kwonly defaults dict */
  35. PyObject *(*defaults_getter)(PyObject *);
  36. PyObject *func_annotations; /* function annotations dict */
  37. } __pyx_CyFunctionObject;
  38. static PyTypeObject *__pyx_CyFunctionType = 0;
  39. #define __Pyx_CyFunction_NewEx(ml, flags, qualname, self, module, globals, code) \
  40. __Pyx_CyFunction_New(__pyx_CyFunctionType, ml, flags, qualname, self, module, globals, code)
  41. static PyObject *__Pyx_CyFunction_New(PyTypeObject *, PyMethodDef *ml,
  42. int flags, PyObject* qualname,
  43. PyObject *self,
  44. PyObject *module, PyObject *globals,
  45. PyObject* code);
  46. static CYTHON_INLINE void *__Pyx_CyFunction_InitDefaults(PyObject *m,
  47. size_t size,
  48. int pyobjects);
  49. static CYTHON_INLINE void __Pyx_CyFunction_SetDefaultsTuple(PyObject *m,
  50. PyObject *tuple);
  51. static CYTHON_INLINE void __Pyx_CyFunction_SetDefaultsKwDict(PyObject *m,
  52. PyObject *dict);
  53. static CYTHON_INLINE void __Pyx_CyFunction_SetAnnotationsDict(PyObject *m,
  54. PyObject *dict);
  55. static int __pyx_CyFunction_init(void);
  56. //////////////////// CythonFunction ////////////////////
  57. //@substitute: naming
  58. //@requires: CommonStructures.c::FetchCommonType
  59. ////@requires: ObjectHandling.c::PyObjectGetAttrStr
  60. #include <structmember.h>
  61. static PyObject *
  62. __Pyx_CyFunction_get_doc(__pyx_CyFunctionObject *op, CYTHON_UNUSED void *closure)
  63. {
  64. if (unlikely(op->func_doc == NULL)) {
  65. if (op->func.m_ml->ml_doc) {
  66. #if PY_MAJOR_VERSION >= 3
  67. op->func_doc = PyUnicode_FromString(op->func.m_ml->ml_doc);
  68. #else
  69. op->func_doc = PyString_FromString(op->func.m_ml->ml_doc);
  70. #endif
  71. if (unlikely(op->func_doc == NULL))
  72. return NULL;
  73. } else {
  74. Py_INCREF(Py_None);
  75. return Py_None;
  76. }
  77. }
  78. Py_INCREF(op->func_doc);
  79. return op->func_doc;
  80. }
  81. static int
  82. __Pyx_CyFunction_set_doc(__pyx_CyFunctionObject *op, PyObject *value)
  83. {
  84. PyObject *tmp = op->func_doc;
  85. if (value == NULL) {
  86. // Mark as deleted
  87. value = Py_None;
  88. }
  89. Py_INCREF(value);
  90. op->func_doc = value;
  91. Py_XDECREF(tmp);
  92. return 0;
  93. }
  94. static PyObject *
  95. __Pyx_CyFunction_get_name(__pyx_CyFunctionObject *op)
  96. {
  97. if (unlikely(op->func_name == NULL)) {
  98. #if PY_MAJOR_VERSION >= 3
  99. op->func_name = PyUnicode_InternFromString(op->func.m_ml->ml_name);
  100. #else
  101. op->func_name = PyString_InternFromString(op->func.m_ml->ml_name);
  102. #endif
  103. if (unlikely(op->func_name == NULL))
  104. return NULL;
  105. }
  106. Py_INCREF(op->func_name);
  107. return op->func_name;
  108. }
  109. static int
  110. __Pyx_CyFunction_set_name(__pyx_CyFunctionObject *op, PyObject *value)
  111. {
  112. PyObject *tmp;
  113. #if PY_MAJOR_VERSION >= 3
  114. if (unlikely(value == NULL || !PyUnicode_Check(value))) {
  115. #else
  116. if (unlikely(value == NULL || !PyString_Check(value))) {
  117. #endif
  118. PyErr_SetString(PyExc_TypeError,
  119. "__name__ must be set to a string object");
  120. return -1;
  121. }
  122. tmp = op->func_name;
  123. Py_INCREF(value);
  124. op->func_name = value;
  125. Py_XDECREF(tmp);
  126. return 0;
  127. }
  128. static PyObject *
  129. __Pyx_CyFunction_get_qualname(__pyx_CyFunctionObject *op)
  130. {
  131. Py_INCREF(op->func_qualname);
  132. return op->func_qualname;
  133. }
  134. static int
  135. __Pyx_CyFunction_set_qualname(__pyx_CyFunctionObject *op, PyObject *value)
  136. {
  137. PyObject *tmp;
  138. #if PY_MAJOR_VERSION >= 3
  139. if (unlikely(value == NULL || !PyUnicode_Check(value))) {
  140. #else
  141. if (unlikely(value == NULL || !PyString_Check(value))) {
  142. #endif
  143. PyErr_SetString(PyExc_TypeError,
  144. "__qualname__ must be set to a string object");
  145. return -1;
  146. }
  147. tmp = op->func_qualname;
  148. Py_INCREF(value);
  149. op->func_qualname = value;
  150. Py_XDECREF(tmp);
  151. return 0;
  152. }
  153. static PyObject *
  154. __Pyx_CyFunction_get_self(__pyx_CyFunctionObject *m, CYTHON_UNUSED void *closure)
  155. {
  156. PyObject *self;
  157. self = m->func_closure;
  158. if (self == NULL)
  159. self = Py_None;
  160. Py_INCREF(self);
  161. return self;
  162. }
  163. static PyObject *
  164. __Pyx_CyFunction_get_dict(__pyx_CyFunctionObject *op)
  165. {
  166. if (unlikely(op->func_dict == NULL)) {
  167. op->func_dict = PyDict_New();
  168. if (unlikely(op->func_dict == NULL))
  169. return NULL;
  170. }
  171. Py_INCREF(op->func_dict);
  172. return op->func_dict;
  173. }
  174. static int
  175. __Pyx_CyFunction_set_dict(__pyx_CyFunctionObject *op, PyObject *value)
  176. {
  177. PyObject *tmp;
  178. if (unlikely(value == NULL)) {
  179. PyErr_SetString(PyExc_TypeError,
  180. "function's dictionary may not be deleted");
  181. return -1;
  182. }
  183. if (unlikely(!PyDict_Check(value))) {
  184. PyErr_SetString(PyExc_TypeError,
  185. "setting function's dictionary to a non-dict");
  186. return -1;
  187. }
  188. tmp = op->func_dict;
  189. Py_INCREF(value);
  190. op->func_dict = value;
  191. Py_XDECREF(tmp);
  192. return 0;
  193. }
  194. static PyObject *
  195. __Pyx_CyFunction_get_globals(__pyx_CyFunctionObject *op)
  196. {
  197. Py_INCREF(op->func_globals);
  198. return op->func_globals;
  199. }
  200. static PyObject *
  201. __Pyx_CyFunction_get_closure(CYTHON_UNUSED __pyx_CyFunctionObject *op)
  202. {
  203. Py_INCREF(Py_None);
  204. return Py_None;
  205. }
  206. static PyObject *
  207. __Pyx_CyFunction_get_code(__pyx_CyFunctionObject *op)
  208. {
  209. PyObject* result = (op->func_code) ? op->func_code : Py_None;
  210. Py_INCREF(result);
  211. return result;
  212. }
  213. static int
  214. __Pyx_CyFunction_init_defaults(__pyx_CyFunctionObject *op) {
  215. int result = 0;
  216. PyObject *res = op->defaults_getter((PyObject *) op);
  217. if (unlikely(!res))
  218. return -1;
  219. // Cache result
  220. #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
  221. op->defaults_tuple = PyTuple_GET_ITEM(res, 0);
  222. Py_INCREF(op->defaults_tuple);
  223. op->defaults_kwdict = PyTuple_GET_ITEM(res, 1);
  224. Py_INCREF(op->defaults_kwdict);
  225. #else
  226. op->defaults_tuple = PySequence_ITEM(res, 0);
  227. if (unlikely(!op->defaults_tuple)) result = -1;
  228. else {
  229. op->defaults_kwdict = PySequence_ITEM(res, 1);
  230. if (unlikely(!op->defaults_kwdict)) result = -1;
  231. }
  232. #endif
  233. Py_DECREF(res);
  234. return result;
  235. }
  236. static int
  237. __Pyx_CyFunction_set_defaults(__pyx_CyFunctionObject *op, PyObject* value) {
  238. PyObject* tmp;
  239. if (!value) {
  240. // del => explicit None to prevent rebuilding
  241. value = Py_None;
  242. } else if (value != Py_None && !PyTuple_Check(value)) {
  243. PyErr_SetString(PyExc_TypeError,
  244. "__defaults__ must be set to a tuple object");
  245. return -1;
  246. }
  247. Py_INCREF(value);
  248. tmp = op->defaults_tuple;
  249. op->defaults_tuple = value;
  250. Py_XDECREF(tmp);
  251. return 0;
  252. }
  253. static PyObject *
  254. __Pyx_CyFunction_get_defaults(__pyx_CyFunctionObject *op) {
  255. PyObject* result = op->defaults_tuple;
  256. if (unlikely(!result)) {
  257. if (op->defaults_getter) {
  258. if (__Pyx_CyFunction_init_defaults(op) < 0) return NULL;
  259. result = op->defaults_tuple;
  260. } else {
  261. result = Py_None;
  262. }
  263. }
  264. Py_INCREF(result);
  265. return result;
  266. }
  267. static int
  268. __Pyx_CyFunction_set_kwdefaults(__pyx_CyFunctionObject *op, PyObject* value) {
  269. PyObject* tmp;
  270. if (!value) {
  271. // del => explicit None to prevent rebuilding
  272. value = Py_None;
  273. } else if (value != Py_None && !PyDict_Check(value)) {
  274. PyErr_SetString(PyExc_TypeError,
  275. "__kwdefaults__ must be set to a dict object");
  276. return -1;
  277. }
  278. Py_INCREF(value);
  279. tmp = op->defaults_kwdict;
  280. op->defaults_kwdict = value;
  281. Py_XDECREF(tmp);
  282. return 0;
  283. }
  284. static PyObject *
  285. __Pyx_CyFunction_get_kwdefaults(__pyx_CyFunctionObject *op) {
  286. PyObject* result = op->defaults_kwdict;
  287. if (unlikely(!result)) {
  288. if (op->defaults_getter) {
  289. if (__Pyx_CyFunction_init_defaults(op) < 0) return NULL;
  290. result = op->defaults_kwdict;
  291. } else {
  292. result = Py_None;
  293. }
  294. }
  295. Py_INCREF(result);
  296. return result;
  297. }
  298. static int
  299. __Pyx_CyFunction_set_annotations(__pyx_CyFunctionObject *op, PyObject* value) {
  300. PyObject* tmp;
  301. if (!value || value == Py_None) {
  302. value = NULL;
  303. } else if (!PyDict_Check(value)) {
  304. PyErr_SetString(PyExc_TypeError,
  305. "__annotations__ must be set to a dict object");
  306. return -1;
  307. }
  308. Py_XINCREF(value);
  309. tmp = op->func_annotations;
  310. op->func_annotations = value;
  311. Py_XDECREF(tmp);
  312. return 0;
  313. }
  314. static PyObject *
  315. __Pyx_CyFunction_get_annotations(__pyx_CyFunctionObject *op) {
  316. PyObject* result = op->func_annotations;
  317. if (unlikely(!result)) {
  318. result = PyDict_New();
  319. if (unlikely(!result)) return NULL;
  320. op->func_annotations = result;
  321. }
  322. Py_INCREF(result);
  323. return result;
  324. }
  325. //#if PY_VERSION_HEX >= 0x030400C1
  326. //static PyObject *
  327. //__Pyx_CyFunction_get_signature(__pyx_CyFunctionObject *op) {
  328. // PyObject *inspect_module, *signature_class, *signature;
  329. // // from inspect import Signature
  330. // inspect_module = PyImport_ImportModuleLevelObject(PYIDENT("inspect"), NULL, NULL, NULL, 0);
  331. // if (unlikely(!inspect_module))
  332. // goto bad;
  333. // signature_class = __Pyx_PyObject_GetAttrStr(inspect_module, PYIDENT("Signature"));
  334. // Py_DECREF(inspect_module);
  335. // if (unlikely(!signature_class))
  336. // goto bad;
  337. // // return Signature.from_function(op)
  338. // signature = PyObject_CallMethodObjArgs(signature_class, PYIDENT("from_function"), op, NULL);
  339. // Py_DECREF(signature_class);
  340. // if (likely(signature))
  341. // return signature;
  342. //bad:
  343. // // make sure we raise an AttributeError from this property on any errors
  344. // if (!PyErr_ExceptionMatches(PyExc_AttributeError))
  345. // PyErr_SetString(PyExc_AttributeError, "failed to calculate __signature__");
  346. // return NULL;
  347. //}
  348. //#endif
  349. static PyGetSetDef __pyx_CyFunction_getsets[] = {
  350. {(char *) "func_doc", (getter)__Pyx_CyFunction_get_doc, (setter)__Pyx_CyFunction_set_doc, 0, 0},
  351. {(char *) "__doc__", (getter)__Pyx_CyFunction_get_doc, (setter)__Pyx_CyFunction_set_doc, 0, 0},
  352. {(char *) "func_name", (getter)__Pyx_CyFunction_get_name, (setter)__Pyx_CyFunction_set_name, 0, 0},
  353. {(char *) "__name__", (getter)__Pyx_CyFunction_get_name, (setter)__Pyx_CyFunction_set_name, 0, 0},
  354. {(char *) "__qualname__", (getter)__Pyx_CyFunction_get_qualname, (setter)__Pyx_CyFunction_set_qualname, 0, 0},
  355. {(char *) "__self__", (getter)__Pyx_CyFunction_get_self, 0, 0, 0},
  356. {(char *) "func_dict", (getter)__Pyx_CyFunction_get_dict, (setter)__Pyx_CyFunction_set_dict, 0, 0},
  357. {(char *) "__dict__", (getter)__Pyx_CyFunction_get_dict, (setter)__Pyx_CyFunction_set_dict, 0, 0},
  358. {(char *) "func_globals", (getter)__Pyx_CyFunction_get_globals, 0, 0, 0},
  359. {(char *) "__globals__", (getter)__Pyx_CyFunction_get_globals, 0, 0, 0},
  360. {(char *) "func_closure", (getter)__Pyx_CyFunction_get_closure, 0, 0, 0},
  361. {(char *) "__closure__", (getter)__Pyx_CyFunction_get_closure, 0, 0, 0},
  362. {(char *) "func_code", (getter)__Pyx_CyFunction_get_code, 0, 0, 0},
  363. {(char *) "__code__", (getter)__Pyx_CyFunction_get_code, 0, 0, 0},
  364. {(char *) "func_defaults", (getter)__Pyx_CyFunction_get_defaults, (setter)__Pyx_CyFunction_set_defaults, 0, 0},
  365. {(char *) "__defaults__", (getter)__Pyx_CyFunction_get_defaults, (setter)__Pyx_CyFunction_set_defaults, 0, 0},
  366. {(char *) "__kwdefaults__", (getter)__Pyx_CyFunction_get_kwdefaults, (setter)__Pyx_CyFunction_set_kwdefaults, 0, 0},
  367. {(char *) "__annotations__", (getter)__Pyx_CyFunction_get_annotations, (setter)__Pyx_CyFunction_set_annotations, 0, 0},
  368. //#if PY_VERSION_HEX >= 0x030400C1
  369. // {(char *) "__signature__", (getter)__Pyx_CyFunction_get_signature, 0, 0, 0},
  370. //#endif
  371. {0, 0, 0, 0, 0}
  372. };
  373. static PyMemberDef __pyx_CyFunction_members[] = {
  374. {(char *) "__module__", T_OBJECT, offsetof(PyCFunctionObject, m_module), PY_WRITE_RESTRICTED, 0},
  375. {0, 0, 0, 0, 0}
  376. };
  377. static PyObject *
  378. __Pyx_CyFunction_reduce(__pyx_CyFunctionObject *m, CYTHON_UNUSED PyObject *args)
  379. {
  380. #if PY_MAJOR_VERSION >= 3
  381. return PyUnicode_FromString(m->func.m_ml->ml_name);
  382. #else
  383. return PyString_FromString(m->func.m_ml->ml_name);
  384. #endif
  385. }
  386. static PyMethodDef __pyx_CyFunction_methods[] = {
  387. {"__reduce__", (PyCFunction)__Pyx_CyFunction_reduce, METH_VARARGS, 0},
  388. {0, 0, 0, 0}
  389. };
  390. #if PY_VERSION_HEX < 0x030500A0
  391. #define __Pyx_CyFunction_weakreflist(cyfunc) ((cyfunc)->func_weakreflist)
  392. #else
  393. #define __Pyx_CyFunction_weakreflist(cyfunc) ((cyfunc)->func.m_weakreflist)
  394. #endif
  395. static PyObject *__Pyx_CyFunction_New(PyTypeObject *type, PyMethodDef *ml, int flags, PyObject* qualname,
  396. PyObject *closure, PyObject *module, PyObject* globals, PyObject* code) {
  397. __pyx_CyFunctionObject *op = PyObject_GC_New(__pyx_CyFunctionObject, type);
  398. if (op == NULL)
  399. return NULL;
  400. op->flags = flags;
  401. __Pyx_CyFunction_weakreflist(op) = NULL;
  402. op->func.m_ml = ml;
  403. op->func.m_self = (PyObject *) op;
  404. Py_XINCREF(closure);
  405. op->func_closure = closure;
  406. Py_XINCREF(module);
  407. op->func.m_module = module;
  408. op->func_dict = NULL;
  409. op->func_name = NULL;
  410. Py_INCREF(qualname);
  411. op->func_qualname = qualname;
  412. op->func_doc = NULL;
  413. op->func_classobj = NULL;
  414. op->func_globals = globals;
  415. Py_INCREF(op->func_globals);
  416. Py_XINCREF(code);
  417. op->func_code = code;
  418. // Dynamic Default args
  419. op->defaults_pyobjects = 0;
  420. op->defaults = NULL;
  421. op->defaults_tuple = NULL;
  422. op->defaults_kwdict = NULL;
  423. op->defaults_getter = NULL;
  424. op->func_annotations = NULL;
  425. PyObject_GC_Track(op);
  426. return (PyObject *) op;
  427. }
  428. static int
  429. __Pyx_CyFunction_clear(__pyx_CyFunctionObject *m)
  430. {
  431. Py_CLEAR(m->func_closure);
  432. Py_CLEAR(m->func.m_module);
  433. Py_CLEAR(m->func_dict);
  434. Py_CLEAR(m->func_name);
  435. Py_CLEAR(m->func_qualname);
  436. Py_CLEAR(m->func_doc);
  437. Py_CLEAR(m->func_globals);
  438. Py_CLEAR(m->func_code);
  439. Py_CLEAR(m->func_classobj);
  440. Py_CLEAR(m->defaults_tuple);
  441. Py_CLEAR(m->defaults_kwdict);
  442. Py_CLEAR(m->func_annotations);
  443. if (m->defaults) {
  444. PyObject **pydefaults = __Pyx_CyFunction_Defaults(PyObject *, m);
  445. int i;
  446. for (i = 0; i < m->defaults_pyobjects; i++)
  447. Py_XDECREF(pydefaults[i]);
  448. PyObject_Free(m->defaults);
  449. m->defaults = NULL;
  450. }
  451. return 0;
  452. }
  453. static void __Pyx__CyFunction_dealloc(__pyx_CyFunctionObject *m)
  454. {
  455. if (__Pyx_CyFunction_weakreflist(m) != NULL)
  456. PyObject_ClearWeakRefs((PyObject *) m);
  457. __Pyx_CyFunction_clear(m);
  458. PyObject_GC_Del(m);
  459. }
  460. static void __Pyx_CyFunction_dealloc(__pyx_CyFunctionObject *m)
  461. {
  462. PyObject_GC_UnTrack(m);
  463. __Pyx__CyFunction_dealloc(m);
  464. }
  465. static int __Pyx_CyFunction_traverse(__pyx_CyFunctionObject *m, visitproc visit, void *arg)
  466. {
  467. Py_VISIT(m->func_closure);
  468. Py_VISIT(m->func.m_module);
  469. Py_VISIT(m->func_dict);
  470. Py_VISIT(m->func_name);
  471. Py_VISIT(m->func_qualname);
  472. Py_VISIT(m->func_doc);
  473. Py_VISIT(m->func_globals);
  474. Py_VISIT(m->func_code);
  475. Py_VISIT(m->func_classobj);
  476. Py_VISIT(m->defaults_tuple);
  477. Py_VISIT(m->defaults_kwdict);
  478. if (m->defaults) {
  479. PyObject **pydefaults = __Pyx_CyFunction_Defaults(PyObject *, m);
  480. int i;
  481. for (i = 0; i < m->defaults_pyobjects; i++)
  482. Py_VISIT(pydefaults[i]);
  483. }
  484. return 0;
  485. }
  486. static PyObject *__Pyx_CyFunction_descr_get(PyObject *func, PyObject *obj, PyObject *type)
  487. {
  488. __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func;
  489. if (m->flags & __Pyx_CYFUNCTION_STATICMETHOD) {
  490. Py_INCREF(func);
  491. return func;
  492. }
  493. if (m->flags & __Pyx_CYFUNCTION_CLASSMETHOD) {
  494. if (type == NULL)
  495. type = (PyObject *)(Py_TYPE(obj));
  496. return __Pyx_PyMethod_New(func, type, (PyObject *)(Py_TYPE(type)));
  497. }
  498. if (obj == Py_None)
  499. obj = NULL;
  500. return __Pyx_PyMethod_New(func, obj, type);
  501. }
  502. static PyObject*
  503. __Pyx_CyFunction_repr(__pyx_CyFunctionObject *op)
  504. {
  505. #if PY_MAJOR_VERSION >= 3
  506. return PyUnicode_FromFormat("<cyfunction %U at %p>",
  507. op->func_qualname, (void *)op);
  508. #else
  509. return PyString_FromFormat("<cyfunction %s at %p>",
  510. PyString_AsString(op->func_qualname), (void *)op);
  511. #endif
  512. }
  513. static PyObject * __Pyx_CyFunction_CallMethod(PyObject *func, PyObject *self, PyObject *arg, PyObject *kw) {
  514. // originally copied from PyCFunction_Call() in CPython's Objects/methodobject.c
  515. PyCFunctionObject* f = (PyCFunctionObject*)func;
  516. PyCFunction meth = f->m_ml->ml_meth;
  517. Py_ssize_t size;
  518. switch (f->m_ml->ml_flags & (METH_VARARGS | METH_KEYWORDS | METH_NOARGS | METH_O)) {
  519. case METH_VARARGS:
  520. if (likely(kw == NULL || PyDict_Size(kw) == 0))
  521. return (*meth)(self, arg);
  522. break;
  523. case METH_VARARGS | METH_KEYWORDS:
  524. return (*(PyCFunctionWithKeywords)meth)(self, arg, kw);
  525. case METH_NOARGS:
  526. if (likely(kw == NULL || PyDict_Size(kw) == 0)) {
  527. size = PyTuple_GET_SIZE(arg);
  528. if (likely(size == 0))
  529. return (*meth)(self, NULL);
  530. PyErr_Format(PyExc_TypeError,
  531. "%.200s() takes no arguments (%" CYTHON_FORMAT_SSIZE_T "d given)",
  532. f->m_ml->ml_name, size);
  533. return NULL;
  534. }
  535. break;
  536. case METH_O:
  537. if (likely(kw == NULL || PyDict_Size(kw) == 0)) {
  538. size = PyTuple_GET_SIZE(arg);
  539. if (likely(size == 1)) {
  540. PyObject *result, *arg0;
  541. #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
  542. arg0 = PyTuple_GET_ITEM(arg, 0);
  543. #else
  544. arg0 = PySequence_ITEM(arg, 0); if (unlikely(!arg0)) return NULL;
  545. #endif
  546. result = (*meth)(self, arg0);
  547. #if !(CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS)
  548. Py_DECREF(arg0);
  549. #endif
  550. return result;
  551. }
  552. PyErr_Format(PyExc_TypeError,
  553. "%.200s() takes exactly one argument (%" CYTHON_FORMAT_SSIZE_T "d given)",
  554. f->m_ml->ml_name, size);
  555. return NULL;
  556. }
  557. break;
  558. default:
  559. PyErr_SetString(PyExc_SystemError, "Bad call flags in "
  560. "__Pyx_CyFunction_Call. METH_OLDARGS is no "
  561. "longer supported!");
  562. return NULL;
  563. }
  564. PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
  565. f->m_ml->ml_name);
  566. return NULL;
  567. }
  568. static CYTHON_INLINE PyObject *__Pyx_CyFunction_Call(PyObject *func, PyObject *arg, PyObject *kw) {
  569. return __Pyx_CyFunction_CallMethod(func, ((PyCFunctionObject*)func)->m_self, arg, kw);
  570. }
  571. static PyObject *__Pyx_CyFunction_CallAsMethod(PyObject *func, PyObject *args, PyObject *kw) {
  572. PyObject *result;
  573. __pyx_CyFunctionObject *cyfunc = (__pyx_CyFunctionObject *) func;
  574. if ((cyfunc->flags & __Pyx_CYFUNCTION_CCLASS) && !(cyfunc->flags & __Pyx_CYFUNCTION_STATICMETHOD)) {
  575. Py_ssize_t argc;
  576. PyObject *new_args;
  577. PyObject *self;
  578. argc = PyTuple_GET_SIZE(args);
  579. new_args = PyTuple_GetSlice(args, 1, argc);
  580. if (unlikely(!new_args))
  581. return NULL;
  582. self = PyTuple_GetItem(args, 0);
  583. if (unlikely(!self)) {
  584. Py_DECREF(new_args);
  585. return NULL;
  586. }
  587. result = __Pyx_CyFunction_CallMethod(func, self, new_args, kw);
  588. Py_DECREF(new_args);
  589. } else {
  590. result = __Pyx_CyFunction_Call(func, args, kw);
  591. }
  592. return result;
  593. }
  594. static PyTypeObject __pyx_CyFunctionType_type = {
  595. PyVarObject_HEAD_INIT(0, 0)
  596. "cython_function_or_method", /*tp_name*/
  597. sizeof(__pyx_CyFunctionObject), /*tp_basicsize*/
  598. 0, /*tp_itemsize*/
  599. (destructor) __Pyx_CyFunction_dealloc, /*tp_dealloc*/
  600. 0, /*tp_print*/
  601. 0, /*tp_getattr*/
  602. 0, /*tp_setattr*/
  603. #if PY_MAJOR_VERSION < 3
  604. 0, /*tp_compare*/
  605. #else
  606. 0, /*reserved*/
  607. #endif
  608. (reprfunc) __Pyx_CyFunction_repr, /*tp_repr*/
  609. 0, /*tp_as_number*/
  610. 0, /*tp_as_sequence*/
  611. 0, /*tp_as_mapping*/
  612. 0, /*tp_hash*/
  613. __Pyx_CyFunction_CallAsMethod, /*tp_call*/
  614. 0, /*tp_str*/
  615. 0, /*tp_getattro*/
  616. 0, /*tp_setattro*/
  617. 0, /*tp_as_buffer*/
  618. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
  619. 0, /*tp_doc*/
  620. (traverseproc) __Pyx_CyFunction_traverse, /*tp_traverse*/
  621. (inquiry) __Pyx_CyFunction_clear, /*tp_clear*/
  622. 0, /*tp_richcompare*/
  623. #if PY_VERSION_HEX < 0x030500A0
  624. offsetof(__pyx_CyFunctionObject, func_weakreflist), /*tp_weaklistoffset*/
  625. #else
  626. offsetof(PyCFunctionObject, m_weakreflist), /*tp_weaklistoffset*/
  627. #endif
  628. 0, /*tp_iter*/
  629. 0, /*tp_iternext*/
  630. __pyx_CyFunction_methods, /*tp_methods*/
  631. __pyx_CyFunction_members, /*tp_members*/
  632. __pyx_CyFunction_getsets, /*tp_getset*/
  633. 0, /*tp_base*/
  634. 0, /*tp_dict*/
  635. __Pyx_CyFunction_descr_get, /*tp_descr_get*/
  636. 0, /*tp_descr_set*/
  637. offsetof(__pyx_CyFunctionObject, func_dict),/*tp_dictoffset*/
  638. 0, /*tp_init*/
  639. 0, /*tp_alloc*/
  640. 0, /*tp_new*/
  641. 0, /*tp_free*/
  642. 0, /*tp_is_gc*/
  643. 0, /*tp_bases*/
  644. 0, /*tp_mro*/
  645. 0, /*tp_cache*/
  646. 0, /*tp_subclasses*/
  647. 0, /*tp_weaklist*/
  648. 0, /*tp_del*/
  649. 0, /*tp_version_tag*/
  650. #if PY_VERSION_HEX >= 0x030400a1
  651. 0, /*tp_finalize*/
  652. #endif
  653. };
  654. static int __pyx_CyFunction_init(void) {
  655. __pyx_CyFunctionType = __Pyx_FetchCommonType(&__pyx_CyFunctionType_type);
  656. if (unlikely(__pyx_CyFunctionType == NULL)) {
  657. return -1;
  658. }
  659. return 0;
  660. }
  661. static CYTHON_INLINE void *__Pyx_CyFunction_InitDefaults(PyObject *func, size_t size, int pyobjects) {
  662. __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func;
  663. m->defaults = PyObject_Malloc(size);
  664. if (unlikely(!m->defaults))
  665. return PyErr_NoMemory();
  666. memset(m->defaults, 0, size);
  667. m->defaults_pyobjects = pyobjects;
  668. return m->defaults;
  669. }
  670. static CYTHON_INLINE void __Pyx_CyFunction_SetDefaultsTuple(PyObject *func, PyObject *tuple) {
  671. __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func;
  672. m->defaults_tuple = tuple;
  673. Py_INCREF(tuple);
  674. }
  675. static CYTHON_INLINE void __Pyx_CyFunction_SetDefaultsKwDict(PyObject *func, PyObject *dict) {
  676. __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func;
  677. m->defaults_kwdict = dict;
  678. Py_INCREF(dict);
  679. }
  680. static CYTHON_INLINE void __Pyx_CyFunction_SetAnnotationsDict(PyObject *func, PyObject *dict) {
  681. __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func;
  682. m->func_annotations = dict;
  683. Py_INCREF(dict);
  684. }
  685. //////////////////// CyFunctionClassCell.proto ////////////////////
  686. static int __Pyx_CyFunction_InitClassCell(PyObject *cyfunctions, PyObject *classobj);/*proto*/
  687. //////////////////// CyFunctionClassCell ////////////////////
  688. //@requires: CythonFunction
  689. static int __Pyx_CyFunction_InitClassCell(PyObject *cyfunctions, PyObject *classobj) {
  690. Py_ssize_t i, count = PyList_GET_SIZE(cyfunctions);
  691. for (i = 0; i < count; i++) {
  692. __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *)
  693. #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
  694. PyList_GET_ITEM(cyfunctions, i);
  695. #else
  696. PySequence_ITEM(cyfunctions, i);
  697. if (unlikely(!m))
  698. return -1;
  699. #endif
  700. Py_INCREF(classobj);
  701. m->func_classobj = classobj;
  702. #if !(CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS)
  703. Py_DECREF((PyObject*)m);
  704. #endif
  705. }
  706. return 0;
  707. }
  708. //////////////////// FusedFunction.proto ////////////////////
  709. typedef struct {
  710. __pyx_CyFunctionObject func;
  711. PyObject *__signatures__;
  712. PyObject *type;
  713. PyObject *self;
  714. } __pyx_FusedFunctionObject;
  715. #define __pyx_FusedFunction_NewEx(ml, flags, qualname, self, module, globals, code) \
  716. __pyx_FusedFunction_New(__pyx_FusedFunctionType, ml, flags, qualname, self, module, globals, code)
  717. static PyObject *__pyx_FusedFunction_New(PyTypeObject *type,
  718. PyMethodDef *ml, int flags,
  719. PyObject *qualname, PyObject *self,
  720. PyObject *module, PyObject *globals,
  721. PyObject *code);
  722. static int __pyx_FusedFunction_clear(__pyx_FusedFunctionObject *self);
  723. static PyTypeObject *__pyx_FusedFunctionType = NULL;
  724. static int __pyx_FusedFunction_init(void);
  725. #define __Pyx_FusedFunction_USED
  726. //////////////////// FusedFunction ////////////////////
  727. //@requires: CythonFunction
  728. static PyObject *
  729. __pyx_FusedFunction_New(PyTypeObject *type, PyMethodDef *ml, int flags,
  730. PyObject *qualname, PyObject *self,
  731. PyObject *module, PyObject *globals,
  732. PyObject *code)
  733. {
  734. __pyx_FusedFunctionObject *fusedfunc =
  735. (__pyx_FusedFunctionObject *) __Pyx_CyFunction_New(type, ml, flags, qualname,
  736. self, module, globals, code);
  737. if (!fusedfunc)
  738. return NULL;
  739. fusedfunc->__signatures__ = NULL;
  740. fusedfunc->type = NULL;
  741. fusedfunc->self = NULL;
  742. return (PyObject *) fusedfunc;
  743. }
  744. static void
  745. __pyx_FusedFunction_dealloc(__pyx_FusedFunctionObject *self)
  746. {
  747. PyObject_GC_UnTrack(self);
  748. Py_CLEAR(self->self);
  749. Py_CLEAR(self->type);
  750. Py_CLEAR(self->__signatures__);
  751. __Pyx__CyFunction_dealloc((__pyx_CyFunctionObject *) self);
  752. }
  753. static int
  754. __pyx_FusedFunction_traverse(__pyx_FusedFunctionObject *self,
  755. visitproc visit,
  756. void *arg)
  757. {
  758. Py_VISIT(self->self);
  759. Py_VISIT(self->type);
  760. Py_VISIT(self->__signatures__);
  761. return __Pyx_CyFunction_traverse((__pyx_CyFunctionObject *) self, visit, arg);
  762. }
  763. static int
  764. __pyx_FusedFunction_clear(__pyx_FusedFunctionObject *self)
  765. {
  766. Py_CLEAR(self->self);
  767. Py_CLEAR(self->type);
  768. Py_CLEAR(self->__signatures__);
  769. return __Pyx_CyFunction_clear((__pyx_CyFunctionObject *) self);
  770. }
  771. static PyObject *
  772. __pyx_FusedFunction_descr_get(PyObject *self, PyObject *obj, PyObject *type)
  773. {
  774. __pyx_FusedFunctionObject *func, *meth;
  775. func = (__pyx_FusedFunctionObject *) self;
  776. if (func->self || func->func.flags & __Pyx_CYFUNCTION_STATICMETHOD) {
  777. // Do not allow rebinding and don't do anything for static methods
  778. Py_INCREF(self);
  779. return self;
  780. }
  781. if (obj == Py_None)
  782. obj = NULL;
  783. meth = (__pyx_FusedFunctionObject *) __pyx_FusedFunction_NewEx(
  784. ((PyCFunctionObject *) func)->m_ml,
  785. ((__pyx_CyFunctionObject *) func)->flags,
  786. ((__pyx_CyFunctionObject *) func)->func_qualname,
  787. ((__pyx_CyFunctionObject *) func)->func_closure,
  788. ((PyCFunctionObject *) func)->m_module,
  789. ((__pyx_CyFunctionObject *) func)->func_globals,
  790. ((__pyx_CyFunctionObject *) func)->func_code);
  791. if (!meth)
  792. return NULL;
  793. Py_XINCREF(func->func.func_classobj);
  794. meth->func.func_classobj = func->func.func_classobj;
  795. Py_XINCREF(func->__signatures__);
  796. meth->__signatures__ = func->__signatures__;
  797. Py_XINCREF(type);
  798. meth->type = type;
  799. Py_XINCREF(func->func.defaults_tuple);
  800. meth->func.defaults_tuple = func->func.defaults_tuple;
  801. if (func->func.flags & __Pyx_CYFUNCTION_CLASSMETHOD)
  802. obj = type;
  803. Py_XINCREF(obj);
  804. meth->self = obj;
  805. return (PyObject *) meth;
  806. }
  807. static PyObject *
  808. _obj_to_str(PyObject *obj)
  809. {
  810. if (PyType_Check(obj))
  811. return PyObject_GetAttr(obj, PYIDENT("__name__"));
  812. else
  813. return PyObject_Str(obj);
  814. }
  815. static PyObject *
  816. __pyx_FusedFunction_getitem(__pyx_FusedFunctionObject *self, PyObject *idx)
  817. {
  818. PyObject *signature = NULL;
  819. PyObject *unbound_result_func;
  820. PyObject *result_func = NULL;
  821. if (self->__signatures__ == NULL) {
  822. PyErr_SetString(PyExc_TypeError, "Function is not fused");
  823. return NULL;
  824. }
  825. if (PyTuple_Check(idx)) {
  826. PyObject *list = PyList_New(0);
  827. Py_ssize_t n = PyTuple_GET_SIZE(idx);
  828. PyObject *string = NULL;
  829. PyObject *sep = NULL;
  830. int i;
  831. if (!list)
  832. return NULL;
  833. for (i = 0; i < n; i++) {
  834. #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
  835. PyObject *item = PyTuple_GET_ITEM(idx, i);
  836. #else
  837. PyObject *item = PySequence_ITEM(idx, i);
  838. #endif
  839. string = _obj_to_str(item);
  840. #if !(CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS)
  841. Py_DECREF(item);
  842. #endif
  843. if (!string || PyList_Append(list, string) < 0)
  844. goto __pyx_err;
  845. Py_DECREF(string);
  846. }
  847. sep = PyUnicode_FromString("|");
  848. if (sep)
  849. signature = PyUnicode_Join(sep, list);
  850. __pyx_err:
  851. ;
  852. Py_DECREF(list);
  853. Py_XDECREF(sep);
  854. } else {
  855. signature = _obj_to_str(idx);
  856. }
  857. if (!signature)
  858. return NULL;
  859. unbound_result_func = PyObject_GetItem(self->__signatures__, signature);
  860. if (unbound_result_func) {
  861. if (self->self || self->type) {
  862. __pyx_FusedFunctionObject *unbound = (__pyx_FusedFunctionObject *) unbound_result_func;
  863. // TODO: move this to InitClassCell
  864. Py_CLEAR(unbound->func.func_classobj);
  865. Py_XINCREF(self->func.func_classobj);
  866. unbound->func.func_classobj = self->func.func_classobj;
  867. result_func = __pyx_FusedFunction_descr_get(unbound_result_func,
  868. self->self, self->type);
  869. } else {
  870. result_func = unbound_result_func;
  871. Py_INCREF(result_func);
  872. }
  873. }
  874. Py_DECREF(signature);
  875. Py_XDECREF(unbound_result_func);
  876. return result_func;
  877. }
  878. static PyObject *
  879. __pyx_FusedFunction_callfunction(PyObject *func, PyObject *args, PyObject *kw)
  880. {
  881. __pyx_CyFunctionObject *cyfunc = (__pyx_CyFunctionObject *) func;
  882. int static_specialized = (cyfunc->flags & __Pyx_CYFUNCTION_STATICMETHOD &&
  883. !((__pyx_FusedFunctionObject *) func)->__signatures__);
  884. if (cyfunc->flags & __Pyx_CYFUNCTION_CCLASS && !static_specialized) {
  885. return __Pyx_CyFunction_CallAsMethod(func, args, kw);
  886. } else {
  887. return __Pyx_CyFunction_Call(func, args, kw);
  888. }
  889. }
  890. // Note: the 'self' from method binding is passed in in the args tuple,
  891. // whereas PyCFunctionObject's m_self is passed in as the first
  892. // argument to the C function. For extension methods we need
  893. // to pass 'self' as 'm_self' and not as the first element of the
  894. // args tuple.
  895. static PyObject *
  896. __pyx_FusedFunction_call(PyObject *func, PyObject *args, PyObject *kw)
  897. {
  898. __pyx_FusedFunctionObject *binding_func = (__pyx_FusedFunctionObject *) func;
  899. Py_ssize_t argc = PyTuple_GET_SIZE(args);
  900. PyObject *new_args = NULL;
  901. __pyx_FusedFunctionObject *new_func = NULL;
  902. PyObject *result = NULL;
  903. PyObject *self = NULL;
  904. int is_staticmethod = binding_func->func.flags & __Pyx_CYFUNCTION_STATICMETHOD;
  905. int is_classmethod = binding_func->func.flags & __Pyx_CYFUNCTION_CLASSMETHOD;
  906. if (binding_func->self) {
  907. // Bound method call, put 'self' in the args tuple
  908. Py_ssize_t i;
  909. new_args = PyTuple_New(argc + 1);
  910. if (!new_args)
  911. return NULL;
  912. self = binding_func->self;
  913. #if !(CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS)
  914. Py_INCREF(self);
  915. #endif
  916. Py_INCREF(self);
  917. PyTuple_SET_ITEM(new_args, 0, self);
  918. for (i = 0; i < argc; i++) {
  919. #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
  920. PyObject *item = PyTuple_GET_ITEM(args, i);
  921. Py_INCREF(item);
  922. #else
  923. PyObject *item = PySequence_ITEM(args, i); if (unlikely(!item)) goto bad;
  924. #endif
  925. PyTuple_SET_ITEM(new_args, i + 1, item);
  926. }
  927. args = new_args;
  928. } else if (binding_func->type) {
  929. // Unbound method call
  930. if (argc < 1) {
  931. PyErr_SetString(PyExc_TypeError, "Need at least one argument, 0 given.");
  932. return NULL;
  933. }
  934. #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
  935. self = PyTuple_GET_ITEM(args, 0);
  936. #else
  937. self = PySequence_ITEM(args, 0); if (unlikely(!self)) return NULL;
  938. #endif
  939. }
  940. if (self && !is_classmethod && !is_staticmethod) {
  941. int is_instance = PyObject_IsInstance(self, binding_func->type);
  942. if (unlikely(!is_instance)) {
  943. PyErr_Format(PyExc_TypeError,
  944. "First argument should be of type %.200s, got %.200s.",
  945. ((PyTypeObject *) binding_func->type)->tp_name,
  946. self->ob_type->tp_name);
  947. goto bad;
  948. } else if (unlikely(is_instance == -1)) {
  949. goto bad;
  950. }
  951. }
  952. #if !(CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS)
  953. Py_XDECREF(self);
  954. self = NULL;
  955. #endif
  956. if (binding_func->__signatures__) {
  957. PyObject *tup;
  958. if (is_staticmethod && binding_func->func.flags & __Pyx_CYFUNCTION_CCLASS) {
  959. // FIXME: this seems wrong, but we must currently pass the signatures dict as 'self' argument
  960. tup = PyTuple_Pack(3, args,
  961. kw == NULL ? Py_None : kw,
  962. binding_func->func.defaults_tuple);
  963. if (unlikely(!tup)) goto bad;
  964. new_func = (__pyx_FusedFunctionObject *) __Pyx_CyFunction_CallMethod(
  965. func, binding_func->__signatures__, tup, NULL);
  966. } else {
  967. tup = PyTuple_Pack(4, binding_func->__signatures__, args,
  968. kw == NULL ? Py_None : kw,
  969. binding_func->func.defaults_tuple);
  970. if (unlikely(!tup)) goto bad;
  971. new_func = (__pyx_FusedFunctionObject *) __pyx_FusedFunction_callfunction(func, tup, NULL);
  972. }
  973. Py_DECREF(tup);
  974. if (unlikely(!new_func))
  975. goto bad;
  976. Py_XINCREF(binding_func->func.func_classobj);
  977. Py_CLEAR(new_func->func.func_classobj);
  978. new_func->func.func_classobj = binding_func->func.func_classobj;
  979. func = (PyObject *) new_func;
  980. }
  981. result = __pyx_FusedFunction_callfunction(func, args, kw);
  982. bad:
  983. #if !(CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS)
  984. Py_XDECREF(self);
  985. #endif
  986. Py_XDECREF(new_args);
  987. Py_XDECREF((PyObject *) new_func);
  988. return result;
  989. }
  990. static PyMemberDef __pyx_FusedFunction_members[] = {
  991. {(char *) "__signatures__",
  992. T_OBJECT,
  993. offsetof(__pyx_FusedFunctionObject, __signatures__),
  994. READONLY,
  995. 0},
  996. {0, 0, 0, 0, 0},
  997. };
  998. static PyMappingMethods __pyx_FusedFunction_mapping_methods = {
  999. 0,
  1000. (binaryfunc) __pyx_FusedFunction_getitem,
  1001. 0,
  1002. };
  1003. static PyTypeObject __pyx_FusedFunctionType_type = {
  1004. PyVarObject_HEAD_INIT(0, 0)
  1005. "fused_cython_function", /*tp_name*/
  1006. sizeof(__pyx_FusedFunctionObject), /*tp_basicsize*/
  1007. 0, /*tp_itemsize*/
  1008. (destructor) __pyx_FusedFunction_dealloc, /*tp_dealloc*/
  1009. 0, /*tp_print*/
  1010. 0, /*tp_getattr*/
  1011. 0, /*tp_setattr*/
  1012. #if PY_MAJOR_VERSION < 3
  1013. 0, /*tp_compare*/
  1014. #else
  1015. 0, /*reserved*/
  1016. #endif
  1017. 0, /*tp_repr*/
  1018. 0, /*tp_as_number*/
  1019. 0, /*tp_as_sequence*/
  1020. &__pyx_FusedFunction_mapping_methods, /*tp_as_mapping*/
  1021. 0, /*tp_hash*/
  1022. (ternaryfunc) __pyx_FusedFunction_call, /*tp_call*/
  1023. 0, /*tp_str*/
  1024. 0, /*tp_getattro*/
  1025. 0, /*tp_setattro*/
  1026. 0, /*tp_as_buffer*/
  1027. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /*tp_flags*/
  1028. 0, /*tp_doc*/
  1029. (traverseproc) __pyx_FusedFunction_traverse, /*tp_traverse*/
  1030. (inquiry) __pyx_FusedFunction_clear,/*tp_clear*/
  1031. 0, /*tp_richcompare*/
  1032. 0, /*tp_weaklistoffset*/
  1033. 0, /*tp_iter*/
  1034. 0, /*tp_iternext*/
  1035. 0, /*tp_methods*/
  1036. __pyx_FusedFunction_members, /*tp_members*/
  1037. // __doc__ is None for the fused function type, but we need it to be
  1038. // a descriptor for the instance's __doc__, so rebuild descriptors in our subclass
  1039. __pyx_CyFunction_getsets, /*tp_getset*/
  1040. &__pyx_CyFunctionType_type, /*tp_base*/
  1041. 0, /*tp_dict*/
  1042. __pyx_FusedFunction_descr_get, /*tp_descr_get*/
  1043. 0, /*tp_descr_set*/
  1044. 0, /*tp_dictoffset*/
  1045. 0, /*tp_init*/
  1046. 0, /*tp_alloc*/
  1047. 0, /*tp_new*/
  1048. 0, /*tp_free*/
  1049. 0, /*tp_is_gc*/
  1050. 0, /*tp_bases*/
  1051. 0, /*tp_mro*/
  1052. 0, /*tp_cache*/
  1053. 0, /*tp_subclasses*/
  1054. 0, /*tp_weaklist*/
  1055. 0, /*tp_del*/
  1056. 0, /*tp_version_tag*/
  1057. #if PY_VERSION_HEX >= 0x030400a1
  1058. 0, /*tp_finalize*/
  1059. #endif
  1060. };
  1061. static int __pyx_FusedFunction_init(void) {
  1062. __pyx_FusedFunctionType = __Pyx_FetchCommonType(&__pyx_FusedFunctionType_type);
  1063. if (__pyx_FusedFunctionType == NULL) {
  1064. return -1;
  1065. }
  1066. return 0;
  1067. }
  1068. //////////////////// ClassMethod.proto ////////////////////
  1069. #include "descrobject.h"
  1070. static PyObject* __Pyx_Method_ClassMethod(PyObject *method); /*proto*/
  1071. //////////////////// ClassMethod ////////////////////
  1072. static PyObject* __Pyx_Method_ClassMethod(PyObject *method) {
  1073. #if CYTHON_COMPILING_IN_PYPY && PYPY_VERSION_NUM <= 0x05080000
  1074. if (PyObject_TypeCheck(method, &PyWrapperDescr_Type)) {
  1075. // cdef classes
  1076. return PyClassMethod_New(method);
  1077. }
  1078. #else
  1079. #if CYTHON_COMPILING_IN_PYSTON || CYTHON_COMPILING_IN_PYPY
  1080. // special C-API function only in Pyston and PyPy >= 5.9
  1081. if (PyMethodDescr_Check(method)) {
  1082. #else
  1083. // It appears that PyMethodDescr_Type is not exposed anywhere in the CPython C-API
  1084. static PyTypeObject *methoddescr_type = NULL;
  1085. if (methoddescr_type == NULL) {
  1086. PyObject *meth = PyObject_GetAttrString((PyObject*)&PyList_Type, "append");
  1087. if (!meth) return NULL;
  1088. methoddescr_type = Py_TYPE(meth);
  1089. Py_DECREF(meth);
  1090. }
  1091. if (__Pyx_TypeCheck(method, methoddescr_type)) {
  1092. #endif
  1093. // cdef classes
  1094. PyMethodDescrObject *descr = (PyMethodDescrObject *)method;
  1095. #if PY_VERSION_HEX < 0x03020000
  1096. PyTypeObject *d_type = descr->d_type;
  1097. #else
  1098. PyTypeObject *d_type = descr->d_common.d_type;
  1099. #endif
  1100. return PyDescr_NewClassMethod(d_type, descr->d_method);
  1101. }
  1102. #endif
  1103. else if (PyMethod_Check(method)) {
  1104. // python classes
  1105. return PyClassMethod_New(PyMethod_GET_FUNCTION(method));
  1106. }
  1107. else if (PyCFunction_Check(method)) {
  1108. return PyClassMethod_New(method);
  1109. }
  1110. #ifdef __Pyx_CyFunction_USED
  1111. else if (__Pyx_TypeCheck(method, __pyx_CyFunctionType)) {
  1112. return PyClassMethod_New(method);
  1113. }
  1114. #endif
  1115. PyErr_SetString(PyExc_TypeError,
  1116. "Class-level classmethod() can only be called on "
  1117. "a method_descriptor or instance method.");
  1118. return NULL;
  1119. }