CythonFunction.c 46 KB

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