CythonFunction.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  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(PyObject *obj)
  463. {
  464. __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) obj;
  465. PyObject_GC_UnTrack(m);
  466. __Pyx__CyFunction_dealloc(m);
  467. }
  468. static int __Pyx_CyFunction_traverse(__pyx_CyFunctionObject *m, visitproc visit, void *arg)
  469. {
  470. Py_VISIT(m->func_closure);
  471. Py_VISIT(m->func.m_module);
  472. Py_VISIT(m->func_dict);
  473. Py_VISIT(m->func_name);
  474. Py_VISIT(m->func_qualname);
  475. Py_VISIT(m->func_doc);
  476. Py_VISIT(m->func_globals);
  477. Py_VISIT(m->func_code);
  478. Py_VISIT(m->func_classobj);
  479. Py_VISIT(m->defaults_tuple);
  480. Py_VISIT(m->defaults_kwdict);
  481. if (m->defaults) {
  482. PyObject **pydefaults = __Pyx_CyFunction_Defaults(PyObject *, m);
  483. int i;
  484. for (i = 0; i < m->defaults_pyobjects; i++)
  485. Py_VISIT(pydefaults[i]);
  486. }
  487. return 0;
  488. }
  489. static PyObject *__Pyx_CyFunction_descr_get(PyObject *func, PyObject *obj, PyObject *type)
  490. {
  491. #if PY_MAJOR_VERSION < 3
  492. __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func;
  493. if (m->flags & __Pyx_CYFUNCTION_STATICMETHOD) {
  494. Py_INCREF(func);
  495. return func;
  496. }
  497. if (m->flags & __Pyx_CYFUNCTION_CLASSMETHOD) {
  498. if (type == NULL)
  499. type = (PyObject *)(Py_TYPE(obj));
  500. return __Pyx_PyMethod_New(func, type, (PyObject *)(Py_TYPE(type)));
  501. }
  502. if (obj == Py_None)
  503. obj = NULL;
  504. #endif
  505. return __Pyx_PyMethod_New(func, obj, type);
  506. }
  507. static PyObject*
  508. __Pyx_CyFunction_repr(__pyx_CyFunctionObject *op)
  509. {
  510. #if PY_MAJOR_VERSION >= 3
  511. return PyUnicode_FromFormat("<cyfunction %U at %p>",
  512. op->func_qualname, (void *)op);
  513. #else
  514. return PyString_FromFormat("<cyfunction %s at %p>",
  515. PyString_AsString(op->func_qualname), (void *)op);
  516. #endif
  517. }
  518. static PyObject * __Pyx_CyFunction_CallMethod(PyObject *func, PyObject *self, PyObject *arg, PyObject *kw) {
  519. // originally copied from PyCFunction_Call() in CPython's Objects/methodobject.c
  520. PyCFunctionObject* f = (PyCFunctionObject*)func;
  521. PyCFunction meth = f->m_ml->ml_meth;
  522. Py_ssize_t size;
  523. switch (f->m_ml->ml_flags & (METH_VARARGS | METH_KEYWORDS | METH_NOARGS | METH_O)) {
  524. case METH_VARARGS:
  525. if (likely(kw == NULL || PyDict_Size(kw) == 0))
  526. return (*meth)(self, arg);
  527. break;
  528. case METH_VARARGS | METH_KEYWORDS:
  529. return (*(PyCFunctionWithKeywords)(void*)meth)(self, arg, kw);
  530. case METH_NOARGS:
  531. if (likely(kw == NULL || PyDict_Size(kw) == 0)) {
  532. size = PyTuple_GET_SIZE(arg);
  533. if (likely(size == 0))
  534. return (*meth)(self, NULL);
  535. PyErr_Format(PyExc_TypeError,
  536. "%.200s() takes no arguments (%" CYTHON_FORMAT_SSIZE_T "d given)",
  537. f->m_ml->ml_name, size);
  538. return NULL;
  539. }
  540. break;
  541. case METH_O:
  542. if (likely(kw == NULL || PyDict_Size(kw) == 0)) {
  543. size = PyTuple_GET_SIZE(arg);
  544. if (likely(size == 1)) {
  545. PyObject *result, *arg0;
  546. #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
  547. arg0 = PyTuple_GET_ITEM(arg, 0);
  548. #else
  549. arg0 = PySequence_ITEM(arg, 0); if (unlikely(!arg0)) return NULL;
  550. #endif
  551. result = (*meth)(self, arg0);
  552. #if !(CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS)
  553. Py_DECREF(arg0);
  554. #endif
  555. return result;
  556. }
  557. PyErr_Format(PyExc_TypeError,
  558. "%.200s() takes exactly one argument (%" CYTHON_FORMAT_SSIZE_T "d given)",
  559. f->m_ml->ml_name, size);
  560. return NULL;
  561. }
  562. break;
  563. default:
  564. PyErr_SetString(PyExc_SystemError, "Bad call flags in "
  565. "__Pyx_CyFunction_Call. METH_OLDARGS is no "
  566. "longer supported!");
  567. return NULL;
  568. }
  569. PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
  570. f->m_ml->ml_name);
  571. return NULL;
  572. }
  573. static CYTHON_INLINE PyObject *__Pyx_CyFunction_Call(PyObject *func, PyObject *arg, PyObject *kw) {
  574. return __Pyx_CyFunction_CallMethod(func, ((PyCFunctionObject*)func)->m_self, arg, kw);
  575. }
  576. static PyObject *__Pyx_CyFunction_CallAsMethod(PyObject *func, PyObject *args, PyObject *kw) {
  577. PyObject *result;
  578. __pyx_CyFunctionObject *cyfunc = (__pyx_CyFunctionObject *) func;
  579. if ((cyfunc->flags & __Pyx_CYFUNCTION_CCLASS) && !(cyfunc->flags & __Pyx_CYFUNCTION_STATICMETHOD)) {
  580. Py_ssize_t argc;
  581. PyObject *new_args;
  582. PyObject *self;
  583. argc = PyTuple_GET_SIZE(args);
  584. new_args = PyTuple_GetSlice(args, 1, argc);
  585. if (unlikely(!new_args))
  586. return NULL;
  587. self = PyTuple_GetItem(args, 0);
  588. if (unlikely(!self)) {
  589. Py_DECREF(new_args);
  590. PyErr_Format(PyExc_TypeError,
  591. "unbound method %.200s() needs an argument",
  592. #if PY_MAJOR_VERSION >= 3
  593. cyfunc->func_qualname);
  594. #else
  595. PyString_AsString(cyfunc->func_qualname));
  596. #endif
  597. return NULL;
  598. }
  599. result = __Pyx_CyFunction_CallMethod(func, self, new_args, kw);
  600. Py_DECREF(new_args);
  601. } else {
  602. result = __Pyx_CyFunction_Call(func, args, kw);
  603. }
  604. return result;
  605. }
  606. static PyTypeObject __pyx_CyFunctionType_type = {
  607. PyVarObject_HEAD_INIT(0, 0)
  608. "cython_function_or_method", /*tp_name*/
  609. sizeof(__pyx_CyFunctionObject), /*tp_basicsize*/
  610. 0, /*tp_itemsize*/
  611. (destructor) __Pyx_CyFunction_dealloc, /*tp_dealloc*/
  612. 0, /*tp_print*/
  613. 0, /*tp_getattr*/
  614. 0, /*tp_setattr*/
  615. #if PY_MAJOR_VERSION < 3
  616. 0, /*tp_compare*/
  617. #else
  618. 0, /*reserved*/
  619. #endif
  620. (reprfunc) __Pyx_CyFunction_repr, /*tp_repr*/
  621. 0, /*tp_as_number*/
  622. 0, /*tp_as_sequence*/
  623. 0, /*tp_as_mapping*/
  624. 0, /*tp_hash*/
  625. __Pyx_CyFunction_CallAsMethod, /*tp_call*/
  626. 0, /*tp_str*/
  627. 0, /*tp_getattro*/
  628. 0, /*tp_setattro*/
  629. 0, /*tp_as_buffer*/
  630. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
  631. 0, /*tp_doc*/
  632. (traverseproc) __Pyx_CyFunction_traverse, /*tp_traverse*/
  633. (inquiry) __Pyx_CyFunction_clear, /*tp_clear*/
  634. 0, /*tp_richcompare*/
  635. #if PY_VERSION_HEX < 0x030500A0
  636. offsetof(__pyx_CyFunctionObject, func_weakreflist), /*tp_weaklistoffset*/
  637. #else
  638. offsetof(PyCFunctionObject, m_weakreflist), /*tp_weaklistoffset*/
  639. #endif
  640. 0, /*tp_iter*/
  641. 0, /*tp_iternext*/
  642. __pyx_CyFunction_methods, /*tp_methods*/
  643. __pyx_CyFunction_members, /*tp_members*/
  644. __pyx_CyFunction_getsets, /*tp_getset*/
  645. 0, /*tp_base*/
  646. 0, /*tp_dict*/
  647. __Pyx_CyFunction_descr_get, /*tp_descr_get*/
  648. 0, /*tp_descr_set*/
  649. offsetof(__pyx_CyFunctionObject, func_dict),/*tp_dictoffset*/
  650. 0, /*tp_init*/
  651. 0, /*tp_alloc*/
  652. 0, /*tp_new*/
  653. 0, /*tp_free*/
  654. 0, /*tp_is_gc*/
  655. 0, /*tp_bases*/
  656. 0, /*tp_mro*/
  657. 0, /*tp_cache*/
  658. 0, /*tp_subclasses*/
  659. 0, /*tp_weaklist*/
  660. 0, /*tp_del*/
  661. 0, /*tp_version_tag*/
  662. #if PY_VERSION_HEX >= 0x030400a1
  663. 0, /*tp_finalize*/
  664. #endif
  665. #if PY_VERSION_HEX >= 0x030800b1 && (!CYTHON_COMPILING_IN_PYPY || PYPY_VERSION_NUM >= 0x07030800)
  666. 0, /*tp_vectorcall*/
  667. #endif
  668. #if PY_VERSION_HEX >= 0x030800b4 && PY_VERSION_HEX < 0x03090000
  669. 0, /*tp_print*/
  670. #endif
  671. #if CYTHON_COMPILING_IN_PYPY && PY_VERSION_HEX >= 0x03090000
  672. 0, /*tp_pypy_flags*/
  673. #endif
  674. };
  675. static int __pyx_CyFunction_init(void) {
  676. __pyx_CyFunctionType = __Pyx_FetchCommonType(&__pyx_CyFunctionType_type);
  677. if (unlikely(__pyx_CyFunctionType == NULL)) {
  678. return -1;
  679. }
  680. return 0;
  681. }
  682. static CYTHON_INLINE void *__Pyx_CyFunction_InitDefaults(PyObject *func, size_t size, int pyobjects) {
  683. __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func;
  684. m->defaults = PyObject_Malloc(size);
  685. if (unlikely(!m->defaults))
  686. return PyErr_NoMemory();
  687. memset(m->defaults, 0, size);
  688. m->defaults_pyobjects = pyobjects;
  689. m->defaults_size = size;
  690. return m->defaults;
  691. }
  692. static CYTHON_INLINE void __Pyx_CyFunction_SetDefaultsTuple(PyObject *func, PyObject *tuple) {
  693. __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func;
  694. m->defaults_tuple = tuple;
  695. Py_INCREF(tuple);
  696. }
  697. static CYTHON_INLINE void __Pyx_CyFunction_SetDefaultsKwDict(PyObject *func, PyObject *dict) {
  698. __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func;
  699. m->defaults_kwdict = dict;
  700. Py_INCREF(dict);
  701. }
  702. static CYTHON_INLINE void __Pyx_CyFunction_SetAnnotationsDict(PyObject *func, PyObject *dict) {
  703. __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *) func;
  704. m->func_annotations = dict;
  705. Py_INCREF(dict);
  706. }
  707. //////////////////// CythonFunction.proto ////////////////////
  708. static PyObject *__Pyx_CyFunction_New(PyMethodDef *ml,
  709. int flags, PyObject* qualname,
  710. PyObject *closure,
  711. PyObject *module, PyObject *globals,
  712. PyObject* code);
  713. //////////////////// CythonFunction ////////////////////
  714. //@requires: CythonFunctionShared
  715. static PyObject *__Pyx_CyFunction_New(PyMethodDef *ml, int flags, PyObject* qualname,
  716. PyObject *closure, PyObject *module, PyObject* globals, PyObject* code) {
  717. PyObject *op = __Pyx_CyFunction_Init(
  718. PyObject_GC_New(__pyx_CyFunctionObject, __pyx_CyFunctionType),
  719. ml, flags, qualname, closure, module, globals, code
  720. );
  721. if (likely(op)) {
  722. PyObject_GC_Track(op);
  723. }
  724. return op;
  725. }
  726. //////////////////// CyFunctionClassCell.proto ////////////////////
  727. static int __Pyx_CyFunction_InitClassCell(PyObject *cyfunctions, PyObject *classobj);/*proto*/
  728. //////////////////// CyFunctionClassCell ////////////////////
  729. //@requires: CythonFunctionShared
  730. static int __Pyx_CyFunction_InitClassCell(PyObject *cyfunctions, PyObject *classobj) {
  731. Py_ssize_t i, count = PyList_GET_SIZE(cyfunctions);
  732. for (i = 0; i < count; i++) {
  733. __pyx_CyFunctionObject *m = (__pyx_CyFunctionObject *)
  734. #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
  735. PyList_GET_ITEM(cyfunctions, i);
  736. #else
  737. PySequence_ITEM(cyfunctions, i);
  738. if (unlikely(!m))
  739. return -1;
  740. #endif
  741. Py_INCREF(classobj);
  742. m->func_classobj = classobj;
  743. #if !(CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS)
  744. Py_DECREF((PyObject*)m);
  745. #endif
  746. }
  747. return 0;
  748. }
  749. //////////////////// FusedFunction.proto ////////////////////
  750. typedef struct {
  751. __pyx_CyFunctionObject func;
  752. PyObject *__signatures__;
  753. PyObject *type;
  754. PyObject *self;
  755. } __pyx_FusedFunctionObject;
  756. static PyObject *__pyx_FusedFunction_New(PyMethodDef *ml, int flags,
  757. PyObject *qualname, PyObject *closure,
  758. PyObject *module, PyObject *globals,
  759. PyObject *code);
  760. static int __pyx_FusedFunction_clear(__pyx_FusedFunctionObject *self);
  761. static PyTypeObject *__pyx_FusedFunctionType = NULL;
  762. static int __pyx_FusedFunction_init(void);
  763. #define __Pyx_FusedFunction_USED
  764. //////////////////// FusedFunction ////////////////////
  765. //@requires: CythonFunctionShared
  766. static PyObject *
  767. __pyx_FusedFunction_New(PyMethodDef *ml, int flags,
  768. PyObject *qualname, PyObject *closure,
  769. PyObject *module, PyObject *globals,
  770. PyObject *code)
  771. {
  772. PyObject *op = __Pyx_CyFunction_Init(
  773. // __pyx_CyFunctionObject is correct below since that's the cast that we want.
  774. PyObject_GC_New(__pyx_CyFunctionObject, __pyx_FusedFunctionType),
  775. ml, flags, qualname, closure, module, globals, code
  776. );
  777. if (likely(op)) {
  778. __pyx_FusedFunctionObject *fusedfunc = (__pyx_FusedFunctionObject *) op;
  779. fusedfunc->__signatures__ = NULL;
  780. fusedfunc->type = NULL;
  781. fusedfunc->self = NULL;
  782. PyObject_GC_Track(op);
  783. }
  784. return op;
  785. }
  786. static void
  787. __pyx_FusedFunction_dealloc(__pyx_FusedFunctionObject *self)
  788. {
  789. PyObject_GC_UnTrack(self);
  790. Py_CLEAR(self->self);
  791. Py_CLEAR(self->type);
  792. Py_CLEAR(self->__signatures__);
  793. __Pyx__CyFunction_dealloc((__pyx_CyFunctionObject *) self);
  794. }
  795. static int
  796. __pyx_FusedFunction_traverse(__pyx_FusedFunctionObject *self,
  797. visitproc visit,
  798. void *arg)
  799. {
  800. Py_VISIT(self->self);
  801. Py_VISIT(self->type);
  802. Py_VISIT(self->__signatures__);
  803. return __Pyx_CyFunction_traverse((__pyx_CyFunctionObject *) self, visit, arg);
  804. }
  805. static int
  806. __pyx_FusedFunction_clear(__pyx_FusedFunctionObject *self)
  807. {
  808. Py_CLEAR(self->self);
  809. Py_CLEAR(self->type);
  810. Py_CLEAR(self->__signatures__);
  811. return __Pyx_CyFunction_clear((__pyx_CyFunctionObject *) self);
  812. }
  813. static PyObject *
  814. __pyx_FusedFunction_descr_get(PyObject *self, PyObject *obj, PyObject *type)
  815. {
  816. __pyx_FusedFunctionObject *func, *meth;
  817. func = (__pyx_FusedFunctionObject *) self;
  818. if (func->self || func->func.flags & __Pyx_CYFUNCTION_STATICMETHOD) {
  819. // Do not allow rebinding and don't do anything for static methods
  820. Py_INCREF(self);
  821. return self;
  822. }
  823. if (obj == Py_None)
  824. obj = NULL;
  825. meth = (__pyx_FusedFunctionObject *) __pyx_FusedFunction_New(
  826. ((PyCFunctionObject *) func)->m_ml,
  827. ((__pyx_CyFunctionObject *) func)->flags,
  828. ((__pyx_CyFunctionObject *) func)->func_qualname,
  829. ((__pyx_CyFunctionObject *) func)->func_closure,
  830. ((PyCFunctionObject *) func)->m_module,
  831. ((__pyx_CyFunctionObject *) func)->func_globals,
  832. ((__pyx_CyFunctionObject *) func)->func_code);
  833. if (!meth)
  834. return NULL;
  835. // defaults needs copying fully rather than just copying the pointer
  836. // since otherwise it will be freed on destruction of meth despite
  837. // belonging to func rather than meth
  838. if (func->func.defaults) {
  839. PyObject **pydefaults;
  840. int i;
  841. if (!__Pyx_CyFunction_InitDefaults((PyObject*)meth,
  842. func->func.defaults_size,
  843. func->func.defaults_pyobjects)) {
  844. Py_XDECREF((PyObject*)meth);
  845. return NULL;
  846. }
  847. memcpy(meth->func.defaults, func->func.defaults, func->func.defaults_size);
  848. pydefaults = __Pyx_CyFunction_Defaults(PyObject *, meth);
  849. for (i = 0; i < meth->func.defaults_pyobjects; i++)
  850. Py_XINCREF(pydefaults[i]);
  851. }
  852. Py_XINCREF(func->func.func_classobj);
  853. meth->func.func_classobj = func->func.func_classobj;
  854. Py_XINCREF(func->__signatures__);
  855. meth->__signatures__ = func->__signatures__;
  856. Py_XINCREF(type);
  857. meth->type = type;
  858. Py_XINCREF(func->func.defaults_tuple);
  859. meth->func.defaults_tuple = func->func.defaults_tuple;
  860. if (func->func.flags & __Pyx_CYFUNCTION_CLASSMETHOD)
  861. obj = type;
  862. Py_XINCREF(obj);
  863. meth->self = obj;
  864. return (PyObject *) meth;
  865. }
  866. static PyObject *
  867. _obj_to_str(PyObject *obj)
  868. {
  869. if (PyType_Check(obj))
  870. return PyObject_GetAttr(obj, PYIDENT("__name__"));
  871. else
  872. return PyObject_Str(obj);
  873. }
  874. static PyObject *
  875. __pyx_FusedFunction_getitem(__pyx_FusedFunctionObject *self, PyObject *idx)
  876. {
  877. PyObject *signature = NULL;
  878. PyObject *unbound_result_func;
  879. PyObject *result_func = NULL;
  880. if (self->__signatures__ == NULL) {
  881. PyErr_SetString(PyExc_TypeError, "Function is not fused");
  882. return NULL;
  883. }
  884. if (PyTuple_Check(idx)) {
  885. PyObject *list = PyList_New(0);
  886. Py_ssize_t n = PyTuple_GET_SIZE(idx);
  887. PyObject *sep = NULL;
  888. int i;
  889. if (unlikely(!list))
  890. return NULL;
  891. for (i = 0; i < n; i++) {
  892. int ret;
  893. PyObject *string;
  894. #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
  895. PyObject *item = PyTuple_GET_ITEM(idx, i);
  896. #else
  897. PyObject *item = PySequence_ITEM(idx, i); if (unlikely(!item)) goto __pyx_err;
  898. #endif
  899. string = _obj_to_str(item);
  900. #if !(CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS)
  901. Py_DECREF(item);
  902. #endif
  903. if (unlikely(!string)) goto __pyx_err;
  904. ret = PyList_Append(list, string);
  905. Py_DECREF(string);
  906. if (unlikely(ret < 0)) goto __pyx_err;
  907. }
  908. sep = PyUnicode_FromString("|");
  909. if (likely(sep))
  910. signature = PyUnicode_Join(sep, list);
  911. __pyx_err:
  912. ;
  913. Py_DECREF(list);
  914. Py_XDECREF(sep);
  915. } else {
  916. signature = _obj_to_str(idx);
  917. }
  918. if (!signature)
  919. return NULL;
  920. unbound_result_func = PyObject_GetItem(self->__signatures__, signature);
  921. if (unbound_result_func) {
  922. if (self->self || self->type) {
  923. __pyx_FusedFunctionObject *unbound = (__pyx_FusedFunctionObject *) unbound_result_func;
  924. // TODO: move this to InitClassCell
  925. Py_CLEAR(unbound->func.func_classobj);
  926. Py_XINCREF(self->func.func_classobj);
  927. unbound->func.func_classobj = self->func.func_classobj;
  928. result_func = __pyx_FusedFunction_descr_get(unbound_result_func,
  929. self->self, self->type);
  930. } else {
  931. result_func = unbound_result_func;
  932. Py_INCREF(result_func);
  933. }
  934. }
  935. Py_DECREF(signature);
  936. Py_XDECREF(unbound_result_func);
  937. return result_func;
  938. }
  939. static PyObject *
  940. __pyx_FusedFunction_callfunction(PyObject *func, PyObject *args, PyObject *kw)
  941. {
  942. __pyx_CyFunctionObject *cyfunc = (__pyx_CyFunctionObject *) func;
  943. int static_specialized = (cyfunc->flags & __Pyx_CYFUNCTION_STATICMETHOD &&
  944. !((__pyx_FusedFunctionObject *) func)->__signatures__);
  945. if (cyfunc->flags & __Pyx_CYFUNCTION_CCLASS && !static_specialized) {
  946. return __Pyx_CyFunction_CallAsMethod(func, args, kw);
  947. } else {
  948. return __Pyx_CyFunction_Call(func, args, kw);
  949. }
  950. }
  951. // Note: the 'self' from method binding is passed in in the args tuple,
  952. // whereas PyCFunctionObject's m_self is passed in as the first
  953. // argument to the C function. For extension methods we need
  954. // to pass 'self' as 'm_self' and not as the first element of the
  955. // args tuple.
  956. static PyObject *
  957. __pyx_FusedFunction_call(PyObject *func, PyObject *args, PyObject *kw)
  958. {
  959. __pyx_FusedFunctionObject *binding_func = (__pyx_FusedFunctionObject *) func;
  960. Py_ssize_t argc = PyTuple_GET_SIZE(args);
  961. PyObject *new_args = NULL;
  962. __pyx_FusedFunctionObject *new_func = NULL;
  963. PyObject *result = NULL;
  964. PyObject *self = NULL;
  965. int is_staticmethod = binding_func->func.flags & __Pyx_CYFUNCTION_STATICMETHOD;
  966. int is_classmethod = binding_func->func.flags & __Pyx_CYFUNCTION_CLASSMETHOD;
  967. if (binding_func->self) {
  968. // Bound method call, put 'self' in the args tuple
  969. Py_ssize_t i;
  970. new_args = PyTuple_New(argc + 1);
  971. if (!new_args)
  972. return NULL;
  973. self = binding_func->self;
  974. #if !(CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS)
  975. Py_INCREF(self);
  976. #endif
  977. Py_INCREF(self);
  978. PyTuple_SET_ITEM(new_args, 0, self);
  979. for (i = 0; i < argc; i++) {
  980. #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
  981. PyObject *item = PyTuple_GET_ITEM(args, i);
  982. Py_INCREF(item);
  983. #else
  984. PyObject *item = PySequence_ITEM(args, i); if (unlikely(!item)) goto bad;
  985. #endif
  986. PyTuple_SET_ITEM(new_args, i + 1, item);
  987. }
  988. args = new_args;
  989. } else if (binding_func->type) {
  990. // Unbound method call
  991. if (argc < 1) {
  992. PyErr_SetString(PyExc_TypeError, "Need at least one argument, 0 given.");
  993. return NULL;
  994. }
  995. #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
  996. self = PyTuple_GET_ITEM(args, 0);
  997. #else
  998. self = PySequence_ITEM(args, 0); if (unlikely(!self)) return NULL;
  999. #endif
  1000. }
  1001. if (self && !is_classmethod && !is_staticmethod) {
  1002. int is_instance = PyObject_IsInstance(self, binding_func->type);
  1003. if (unlikely(!is_instance)) {
  1004. PyErr_Format(PyExc_TypeError,
  1005. "First argument should be of type %.200s, got %.200s.",
  1006. ((PyTypeObject *) binding_func->type)->tp_name,
  1007. Py_TYPE(self)->tp_name);
  1008. goto bad;
  1009. } else if (unlikely(is_instance == -1)) {
  1010. goto bad;
  1011. }
  1012. }
  1013. #if !(CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS)
  1014. Py_XDECREF(self);
  1015. self = NULL;
  1016. #endif
  1017. if (binding_func->__signatures__) {
  1018. PyObject *tup;
  1019. if (is_staticmethod && binding_func->func.flags & __Pyx_CYFUNCTION_CCLASS) {
  1020. // FIXME: this seems wrong, but we must currently pass the signatures dict as 'self' argument
  1021. tup = PyTuple_Pack(3, args,
  1022. kw == NULL ? Py_None : kw,
  1023. binding_func->func.defaults_tuple);
  1024. if (unlikely(!tup)) goto bad;
  1025. new_func = (__pyx_FusedFunctionObject *) __Pyx_CyFunction_CallMethod(
  1026. func, binding_func->__signatures__, tup, NULL);
  1027. } else {
  1028. tup = PyTuple_Pack(4, binding_func->__signatures__, args,
  1029. kw == NULL ? Py_None : kw,
  1030. binding_func->func.defaults_tuple);
  1031. if (unlikely(!tup)) goto bad;
  1032. new_func = (__pyx_FusedFunctionObject *) __pyx_FusedFunction_callfunction(func, tup, NULL);
  1033. }
  1034. Py_DECREF(tup);
  1035. if (unlikely(!new_func))
  1036. goto bad;
  1037. Py_XINCREF(binding_func->func.func_classobj);
  1038. Py_CLEAR(new_func->func.func_classobj);
  1039. new_func->func.func_classobj = binding_func->func.func_classobj;
  1040. func = (PyObject *) new_func;
  1041. }
  1042. result = __pyx_FusedFunction_callfunction(func, args, kw);
  1043. bad:
  1044. #if !(CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS)
  1045. Py_XDECREF(self);
  1046. #endif
  1047. Py_XDECREF(new_args);
  1048. Py_XDECREF((PyObject *) new_func);
  1049. return result;
  1050. }
  1051. static PyMemberDef __pyx_FusedFunction_members[] = {
  1052. {(char *) "__signatures__",
  1053. T_OBJECT,
  1054. offsetof(__pyx_FusedFunctionObject, __signatures__),
  1055. READONLY,
  1056. 0},
  1057. {0, 0, 0, 0, 0},
  1058. };
  1059. static PyMappingMethods __pyx_FusedFunction_mapping_methods = {
  1060. 0,
  1061. (binaryfunc) __pyx_FusedFunction_getitem,
  1062. 0,
  1063. };
  1064. static PyTypeObject __pyx_FusedFunctionType_type = {
  1065. PyVarObject_HEAD_INIT(0, 0)
  1066. "fused_cython_function", /*tp_name*/
  1067. sizeof(__pyx_FusedFunctionObject), /*tp_basicsize*/
  1068. 0, /*tp_itemsize*/
  1069. (destructor) __pyx_FusedFunction_dealloc, /*tp_dealloc*/
  1070. 0, /*tp_print*/
  1071. 0, /*tp_getattr*/
  1072. 0, /*tp_setattr*/
  1073. #if PY_MAJOR_VERSION < 3
  1074. 0, /*tp_compare*/
  1075. #else
  1076. 0, /*reserved*/
  1077. #endif
  1078. 0, /*tp_repr*/
  1079. 0, /*tp_as_number*/
  1080. 0, /*tp_as_sequence*/
  1081. &__pyx_FusedFunction_mapping_methods, /*tp_as_mapping*/
  1082. 0, /*tp_hash*/
  1083. (ternaryfunc) __pyx_FusedFunction_call, /*tp_call*/
  1084. 0, /*tp_str*/
  1085. 0, /*tp_getattro*/
  1086. 0, /*tp_setattro*/
  1087. 0, /*tp_as_buffer*/
  1088. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /*tp_flags*/
  1089. 0, /*tp_doc*/
  1090. (traverseproc) __pyx_FusedFunction_traverse, /*tp_traverse*/
  1091. (inquiry) __pyx_FusedFunction_clear,/*tp_clear*/
  1092. 0, /*tp_richcompare*/
  1093. 0, /*tp_weaklistoffset*/
  1094. 0, /*tp_iter*/
  1095. 0, /*tp_iternext*/
  1096. 0, /*tp_methods*/
  1097. __pyx_FusedFunction_members, /*tp_members*/
  1098. // __doc__ is None for the fused function type, but we need it to be
  1099. // a descriptor for the instance's __doc__, so rebuild descriptors in our subclass
  1100. __pyx_CyFunction_getsets, /*tp_getset*/
  1101. // NOTE: tp_base may be changed later during module initialisation when importing CyFunction across modules.
  1102. &__pyx_CyFunctionType_type, /*tp_base*/
  1103. 0, /*tp_dict*/
  1104. __pyx_FusedFunction_descr_get, /*tp_descr_get*/
  1105. 0, /*tp_descr_set*/
  1106. 0, /*tp_dictoffset*/
  1107. 0, /*tp_init*/
  1108. 0, /*tp_alloc*/
  1109. 0, /*tp_new*/
  1110. 0, /*tp_free*/
  1111. 0, /*tp_is_gc*/
  1112. 0, /*tp_bases*/
  1113. 0, /*tp_mro*/
  1114. 0, /*tp_cache*/
  1115. 0, /*tp_subclasses*/
  1116. 0, /*tp_weaklist*/
  1117. 0, /*tp_del*/
  1118. 0, /*tp_version_tag*/
  1119. #if PY_VERSION_HEX >= 0x030400a1
  1120. 0, /*tp_finalize*/
  1121. #endif
  1122. #if PY_VERSION_HEX >= 0x030800b1 && (!CYTHON_COMPILING_IN_PYPY || PYPY_VERSION_NUM >= 0x07030800)
  1123. 0, /*tp_vectorcall*/
  1124. #endif
  1125. #if PY_VERSION_HEX >= 0x030800b4 && PY_VERSION_HEX < 0x03090000
  1126. 0, /*tp_print*/
  1127. #endif
  1128. #if CYTHON_COMPILING_IN_PYPY && PY_VERSION_HEX >= 0x03090000
  1129. 0, /*tp_pypy_flags*/
  1130. #endif
  1131. };
  1132. static int __pyx_FusedFunction_init(void) {
  1133. // Set base from __Pyx_FetchCommonTypeFromSpec, in case it's different from the local static value.
  1134. __pyx_FusedFunctionType_type.tp_base = __pyx_CyFunctionType;
  1135. __pyx_FusedFunctionType = __Pyx_FetchCommonType(&__pyx_FusedFunctionType_type);
  1136. if (__pyx_FusedFunctionType == NULL) {
  1137. return -1;
  1138. }
  1139. return 0;
  1140. }
  1141. //////////////////// ClassMethod.proto ////////////////////
  1142. #include "descrobject.h"
  1143. static CYTHON_UNUSED PyObject* __Pyx_Method_ClassMethod(PyObject *method); /*proto*/
  1144. //////////////////// ClassMethod ////////////////////
  1145. static PyObject* __Pyx_Method_ClassMethod(PyObject *method) {
  1146. #if CYTHON_COMPILING_IN_PYPY && PYPY_VERSION_NUM <= 0x05080000
  1147. if (PyObject_TypeCheck(method, &PyWrapperDescr_Type)) {
  1148. // cdef classes
  1149. return PyClassMethod_New(method);
  1150. }
  1151. #else
  1152. #if CYTHON_COMPILING_IN_PYSTON || CYTHON_COMPILING_IN_PYPY
  1153. // special C-API function only in Pyston and PyPy >= 5.9
  1154. if (PyMethodDescr_Check(method))
  1155. #else
  1156. #if PY_MAJOR_VERSION == 2
  1157. // PyMethodDescr_Type is not exposed in the CPython C-API in Py2.
  1158. static PyTypeObject *methoddescr_type = NULL;
  1159. if (methoddescr_type == NULL) {
  1160. PyObject *meth = PyObject_GetAttrString((PyObject*)&PyList_Type, "append");
  1161. if (!meth) return NULL;
  1162. methoddescr_type = Py_TYPE(meth);
  1163. Py_DECREF(meth);
  1164. }
  1165. #else
  1166. PyTypeObject *methoddescr_type = &PyMethodDescr_Type;
  1167. #endif
  1168. if (__Pyx_TypeCheck(method, methoddescr_type))
  1169. #endif
  1170. {
  1171. // cdef classes
  1172. PyMethodDescrObject *descr = (PyMethodDescrObject *)method;
  1173. #if PY_VERSION_HEX < 0x03020000
  1174. PyTypeObject *d_type = descr->d_type;
  1175. #else
  1176. PyTypeObject *d_type = descr->d_common.d_type;
  1177. #endif
  1178. return PyDescr_NewClassMethod(d_type, descr->d_method);
  1179. }
  1180. #endif
  1181. else if (PyMethod_Check(method)) {
  1182. // python classes
  1183. return PyClassMethod_New(PyMethod_GET_FUNCTION(method));
  1184. }
  1185. else {
  1186. return PyClassMethod_New(method);
  1187. }
  1188. }