Coroutine.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227
  1. //////////////////// GeneratorYieldFrom.proto ////////////////////
  2. static CYTHON_INLINE PyObject* __Pyx_Generator_Yield_From(__pyx_CoroutineObject *gen, PyObject *source);
  3. //////////////////// GeneratorYieldFrom ////////////////////
  4. //@requires: Generator
  5. static void __PyxPyIter_CheckErrorAndDecref(PyObject *source) {
  6. PyErr_Format(PyExc_TypeError,
  7. "iter() returned non-iterator of type '%.100s'",
  8. Py_TYPE(source)->tp_name);
  9. Py_DECREF(source);
  10. }
  11. static CYTHON_INLINE PyObject* __Pyx_Generator_Yield_From(__pyx_CoroutineObject *gen, PyObject *source) {
  12. PyObject *source_gen, *retval;
  13. #ifdef __Pyx_Coroutine_USED
  14. if (__Pyx_Coroutine_Check(source)) {
  15. // TODO: this should only happen for types.coroutine()ed generators, but we can't determine that here
  16. Py_INCREF(source);
  17. source_gen = source;
  18. retval = __Pyx_Generator_Next(source);
  19. } else
  20. #endif
  21. {
  22. #if CYTHON_USE_TYPE_SLOTS
  23. if (likely(Py_TYPE(source)->tp_iter)) {
  24. source_gen = Py_TYPE(source)->tp_iter(source);
  25. if (unlikely(!source_gen))
  26. return NULL;
  27. if (unlikely(!PyIter_Check(source_gen))) {
  28. __PyxPyIter_CheckErrorAndDecref(source_gen);
  29. return NULL;
  30. }
  31. } else
  32. // CPython also allows non-iterable sequences to be iterated over
  33. #endif
  34. {
  35. source_gen = PyObject_GetIter(source);
  36. if (unlikely(!source_gen))
  37. return NULL;
  38. }
  39. // source_gen is now the iterator, make the first next() call
  40. #if CYTHON_USE_TYPE_SLOTS
  41. retval = Py_TYPE(source_gen)->tp_iternext(source_gen);
  42. #else
  43. retval = PyIter_Next(source_gen);
  44. #endif
  45. }
  46. if (likely(retval)) {
  47. gen->yieldfrom = source_gen;
  48. return retval;
  49. }
  50. Py_DECREF(source_gen);
  51. return NULL;
  52. }
  53. //////////////////// CoroutineYieldFrom.proto ////////////////////
  54. static CYTHON_INLINE PyObject* __Pyx_Coroutine_Yield_From(__pyx_CoroutineObject *gen, PyObject *source);
  55. //////////////////// CoroutineYieldFrom ////////////////////
  56. //@requires: Coroutine
  57. //@requires: GetAwaitIter
  58. static PyObject* __Pyx__Coroutine_Yield_From_Generic(__pyx_CoroutineObject *gen, PyObject *source) {
  59. PyObject *retval;
  60. PyObject *source_gen = __Pyx__Coroutine_GetAwaitableIter(source);
  61. if (unlikely(!source_gen)) {
  62. return NULL;
  63. }
  64. // source_gen is now the iterator, make the first next() call
  65. if (__Pyx_Coroutine_Check(source_gen)) {
  66. retval = __Pyx_Generator_Next(source_gen);
  67. } else {
  68. #if CYTHON_USE_TYPE_SLOTS
  69. retval = Py_TYPE(source_gen)->tp_iternext(source_gen);
  70. #else
  71. retval = PyIter_Next(source_gen);
  72. #endif
  73. }
  74. if (retval) {
  75. gen->yieldfrom = source_gen;
  76. return retval;
  77. }
  78. Py_DECREF(source_gen);
  79. return NULL;
  80. }
  81. static CYTHON_INLINE PyObject* __Pyx_Coroutine_Yield_From(__pyx_CoroutineObject *gen, PyObject *source) {
  82. PyObject *retval;
  83. if (__Pyx_Coroutine_Check(source)) {
  84. if (unlikely(((__pyx_CoroutineObject*)source)->yieldfrom)) {
  85. PyErr_SetString(
  86. PyExc_RuntimeError,
  87. "coroutine is being awaited already");
  88. return NULL;
  89. }
  90. retval = __Pyx_Generator_Next(source);
  91. #ifdef __Pyx_AsyncGen_USED
  92. // inlined "__pyx_PyAsyncGenASend" handling to avoid the series of generic calls
  93. } else if (__pyx_PyAsyncGenASend_CheckExact(source)) {
  94. retval = __Pyx_async_gen_asend_iternext(source);
  95. #endif
  96. } else {
  97. return __Pyx__Coroutine_Yield_From_Generic(gen, source);
  98. }
  99. if (retval) {
  100. Py_INCREF(source);
  101. gen->yieldfrom = source;
  102. }
  103. return retval;
  104. }
  105. //////////////////// GetAwaitIter.proto ////////////////////
  106. static CYTHON_INLINE PyObject *__Pyx_Coroutine_GetAwaitableIter(PyObject *o); /*proto*/
  107. static PyObject *__Pyx__Coroutine_GetAwaitableIter(PyObject *o); /*proto*/
  108. //////////////////// GetAwaitIter ////////////////////
  109. //@requires: ObjectHandling.c::PyObjectGetAttrStr
  110. //@requires: ObjectHandling.c::PyObjectCallNoArg
  111. //@requires: ObjectHandling.c::PyObjectCallOneArg
  112. static CYTHON_INLINE PyObject *__Pyx_Coroutine_GetAwaitableIter(PyObject *o) {
  113. #ifdef __Pyx_Coroutine_USED
  114. if (__Pyx_Coroutine_Check(o)) {
  115. return __Pyx_NewRef(o);
  116. }
  117. #endif
  118. return __Pyx__Coroutine_GetAwaitableIter(o);
  119. }
  120. static void __Pyx_Coroutine_AwaitableIterError(PyObject *source) {
  121. #if PY_VERSION_HEX >= 0x030600B3 || defined(_PyErr_FormatFromCause)
  122. _PyErr_FormatFromCause(
  123. PyExc_TypeError,
  124. "'async for' received an invalid object "
  125. "from __anext__: %.100s",
  126. Py_TYPE(source)->tp_name);
  127. #elif PY_MAJOR_VERSION >= 3
  128. PyObject *exc, *val, *val2, *tb;
  129. assert(PyErr_Occurred());
  130. PyErr_Fetch(&exc, &val, &tb);
  131. PyErr_NormalizeException(&exc, &val, &tb);
  132. if (tb != NULL) {
  133. PyException_SetTraceback(val, tb);
  134. Py_DECREF(tb);
  135. }
  136. Py_DECREF(exc);
  137. assert(!PyErr_Occurred());
  138. PyErr_Format(
  139. PyExc_TypeError,
  140. "'async for' received an invalid object "
  141. "from __anext__: %.100s",
  142. Py_TYPE(source)->tp_name);
  143. PyErr_Fetch(&exc, &val2, &tb);
  144. PyErr_NormalizeException(&exc, &val2, &tb);
  145. Py_INCREF(val);
  146. PyException_SetCause(val2, val);
  147. PyException_SetContext(val2, val);
  148. PyErr_Restore(exc, val2, tb);
  149. #else
  150. // since Py2 does not have exception chaining, it's better to avoid shadowing exceptions there
  151. source++;
  152. #endif
  153. }
  154. // adapted from genobject.c in Py3.5
  155. static PyObject *__Pyx__Coroutine_GetAwaitableIter(PyObject *obj) {
  156. PyObject *res;
  157. #if CYTHON_USE_ASYNC_SLOTS
  158. __Pyx_PyAsyncMethodsStruct* am = __Pyx_PyType_AsAsync(obj);
  159. if (likely(am && am->am_await)) {
  160. res = (*am->am_await)(obj);
  161. } else
  162. #endif
  163. #if PY_VERSION_HEX >= 0x030500B2 || defined(PyCoro_CheckExact)
  164. if (PyCoro_CheckExact(obj)) {
  165. return __Pyx_NewRef(obj);
  166. } else
  167. #endif
  168. #if CYTHON_COMPILING_IN_CPYTHON && defined(CO_ITERABLE_COROUTINE)
  169. if (PyGen_CheckExact(obj) && ((PyGenObject*)obj)->gi_code && ((PyCodeObject *)((PyGenObject*)obj)->gi_code)->co_flags & CO_ITERABLE_COROUTINE) {
  170. // Python generator marked with "@types.coroutine" decorator
  171. return __Pyx_NewRef(obj);
  172. } else
  173. #endif
  174. {
  175. PyObject *method = __Pyx_PyObject_GetAttrStr(obj, PYIDENT("__await__"));
  176. if (unlikely(!method)) goto slot_error;
  177. #if CYTHON_UNPACK_METHODS
  178. if (likely(PyMethod_Check(method))) {
  179. PyObject *self = PyMethod_GET_SELF(method);
  180. if (likely(self)) {
  181. PyObject *function = PyMethod_GET_FUNCTION(method);
  182. res = __Pyx_PyObject_CallOneArg(function, self);
  183. } else
  184. res = __Pyx_PyObject_CallNoArg(method);
  185. } else
  186. #endif
  187. res = __Pyx_PyObject_CallNoArg(method);
  188. Py_DECREF(method);
  189. }
  190. if (unlikely(!res)) {
  191. // surprisingly, CPython replaces the exception here...
  192. __Pyx_Coroutine_AwaitableIterError(obj);
  193. goto bad;
  194. }
  195. if (unlikely(!PyIter_Check(res))) {
  196. PyErr_Format(PyExc_TypeError,
  197. "__await__() returned non-iterator of type '%.100s'",
  198. Py_TYPE(res)->tp_name);
  199. Py_CLEAR(res);
  200. } else {
  201. int is_coroutine = 0;
  202. #ifdef __Pyx_Coroutine_USED
  203. is_coroutine |= __Pyx_Coroutine_Check(res);
  204. #endif
  205. #if PY_VERSION_HEX >= 0x030500B2 || defined(PyCoro_CheckExact)
  206. is_coroutine |= PyCoro_CheckExact(res);
  207. #endif
  208. if (unlikely(is_coroutine)) {
  209. /* __await__ must return an *iterator*, not
  210. a coroutine or another awaitable (see PEP 492) */
  211. PyErr_SetString(PyExc_TypeError,
  212. "__await__() returned a coroutine");
  213. Py_CLEAR(res);
  214. }
  215. }
  216. return res;
  217. slot_error:
  218. PyErr_Format(PyExc_TypeError,
  219. "object %.100s can't be used in 'await' expression",
  220. Py_TYPE(obj)->tp_name);
  221. bad:
  222. return NULL;
  223. }
  224. //////////////////// AsyncIter.proto ////////////////////
  225. static CYTHON_INLINE PyObject *__Pyx_Coroutine_GetAsyncIter(PyObject *o); /*proto*/
  226. static CYTHON_INLINE PyObject *__Pyx_Coroutine_AsyncIterNext(PyObject *o); /*proto*/
  227. //////////////////// AsyncIter ////////////////////
  228. //@requires: GetAwaitIter
  229. //@requires: ObjectHandling.c::PyObjectCallMethod0
  230. static PyObject *__Pyx_Coroutine_GetAsyncIter_Generic(PyObject *obj) {
  231. #if PY_VERSION_HEX < 0x030500B1
  232. {
  233. PyObject *iter = __Pyx_PyObject_CallMethod0(obj, PYIDENT("__aiter__"));
  234. if (likely(iter))
  235. return iter;
  236. // FIXME: for the sake of a nicely conforming exception message, assume any AttributeError meant '__aiter__'
  237. if (!PyErr_ExceptionMatches(PyExc_AttributeError))
  238. return NULL;
  239. }
  240. #else
  241. // avoid C warning about 'unused function'
  242. if ((0)) (void) __Pyx_PyObject_CallMethod0(obj, PYIDENT("__aiter__"));
  243. #endif
  244. PyErr_Format(PyExc_TypeError, "'async for' requires an object with __aiter__ method, got %.100s",
  245. Py_TYPE(obj)->tp_name);
  246. return NULL;
  247. }
  248. static CYTHON_INLINE PyObject *__Pyx_Coroutine_GetAsyncIter(PyObject *obj) {
  249. #ifdef __Pyx_AsyncGen_USED
  250. if (__Pyx_AsyncGen_CheckExact(obj)) {
  251. return __Pyx_NewRef(obj);
  252. }
  253. #endif
  254. #if CYTHON_USE_ASYNC_SLOTS
  255. {
  256. __Pyx_PyAsyncMethodsStruct* am = __Pyx_PyType_AsAsync(obj);
  257. if (likely(am && am->am_aiter)) {
  258. return (*am->am_aiter)(obj);
  259. }
  260. }
  261. #endif
  262. return __Pyx_Coroutine_GetAsyncIter_Generic(obj);
  263. }
  264. static PyObject *__Pyx__Coroutine_AsyncIterNext(PyObject *obj) {
  265. #if PY_VERSION_HEX < 0x030500B1
  266. {
  267. PyObject *value = __Pyx_PyObject_CallMethod0(obj, PYIDENT("__anext__"));
  268. if (likely(value))
  269. return value;
  270. }
  271. // FIXME: for the sake of a nicely conforming exception message, assume any AttributeError meant '__anext__'
  272. if (PyErr_ExceptionMatches(PyExc_AttributeError))
  273. #endif
  274. PyErr_Format(PyExc_TypeError, "'async for' requires an object with __anext__ method, got %.100s",
  275. Py_TYPE(obj)->tp_name);
  276. return NULL;
  277. }
  278. static CYTHON_INLINE PyObject *__Pyx_Coroutine_AsyncIterNext(PyObject *obj) {
  279. #ifdef __Pyx_AsyncGen_USED
  280. if (__Pyx_AsyncGen_CheckExact(obj)) {
  281. return __Pyx_async_gen_anext(obj);
  282. }
  283. #endif
  284. #if CYTHON_USE_ASYNC_SLOTS
  285. {
  286. __Pyx_PyAsyncMethodsStruct* am = __Pyx_PyType_AsAsync(obj);
  287. if (likely(am && am->am_anext)) {
  288. return (*am->am_anext)(obj);
  289. }
  290. }
  291. #endif
  292. return __Pyx__Coroutine_AsyncIterNext(obj);
  293. }
  294. //////////////////// pep479.proto ////////////////////
  295. static void __Pyx_Generator_Replace_StopIteration(int in_async_gen); /*proto*/
  296. //////////////////// pep479 ////////////////////
  297. //@requires: Exceptions.c::GetException
  298. static void __Pyx_Generator_Replace_StopIteration(CYTHON_UNUSED int in_async_gen) {
  299. PyObject *exc, *val, *tb, *cur_exc;
  300. __Pyx_PyThreadState_declare
  301. #ifdef __Pyx_StopAsyncIteration_USED
  302. int is_async_stopiteration = 0;
  303. #endif
  304. cur_exc = PyErr_Occurred();
  305. if (likely(!__Pyx_PyErr_GivenExceptionMatches(cur_exc, PyExc_StopIteration))) {
  306. #ifdef __Pyx_StopAsyncIteration_USED
  307. if (in_async_gen && unlikely(__Pyx_PyErr_GivenExceptionMatches(cur_exc, __Pyx_PyExc_StopAsyncIteration))) {
  308. is_async_stopiteration = 1;
  309. } else
  310. #endif
  311. return;
  312. }
  313. __Pyx_PyThreadState_assign
  314. // Chain exceptions by moving Stop(Async)Iteration to exc_info before creating the RuntimeError.
  315. // In Py2.x, no chaining happens, but the exception still stays visible in exc_info.
  316. __Pyx_GetException(&exc, &val, &tb);
  317. Py_XDECREF(exc);
  318. Py_XDECREF(val);
  319. Py_XDECREF(tb);
  320. PyErr_SetString(PyExc_RuntimeError,
  321. #ifdef __Pyx_StopAsyncIteration_USED
  322. is_async_stopiteration ? "async generator raised StopAsyncIteration" :
  323. in_async_gen ? "async generator raised StopIteration" :
  324. #endif
  325. "generator raised StopIteration");
  326. }
  327. //////////////////// CoroutineBase.proto ////////////////////
  328. //@substitute: naming
  329. typedef PyObject *(*__pyx_coroutine_body_t)(PyObject *, PyThreadState *, PyObject *);
  330. typedef struct {
  331. PyObject_HEAD
  332. __pyx_coroutine_body_t body;
  333. PyObject *closure;
  334. PyObject *exc_type;
  335. PyObject *exc_value;
  336. PyObject *exc_traceback;
  337. PyObject *gi_weakreflist;
  338. PyObject *classobj;
  339. PyObject *yieldfrom;
  340. PyObject *gi_name;
  341. PyObject *gi_qualname;
  342. PyObject *gi_modulename;
  343. PyObject *gi_code;
  344. int resume_label;
  345. // using T_BOOL for property below requires char value
  346. char is_running;
  347. } __pyx_CoroutineObject;
  348. static __pyx_CoroutineObject *__Pyx__Coroutine_New(
  349. PyTypeObject *type, __pyx_coroutine_body_t body, PyObject *code, PyObject *closure,
  350. PyObject *name, PyObject *qualname, PyObject *module_name); /*proto*/
  351. static __pyx_CoroutineObject *__Pyx__Coroutine_NewInit(
  352. __pyx_CoroutineObject *gen, __pyx_coroutine_body_t body, PyObject *code, PyObject *closure,
  353. PyObject *name, PyObject *qualname, PyObject *module_name); /*proto*/
  354. static int __Pyx_Coroutine_clear(PyObject *self); /*proto*/
  355. static PyObject *__Pyx_Coroutine_Send(PyObject *self, PyObject *value); /*proto*/
  356. static PyObject *__Pyx_Coroutine_Close(PyObject *self); /*proto*/
  357. static PyObject *__Pyx_Coroutine_Throw(PyObject *gen, PyObject *args); /*proto*/
  358. // macros for exception state swapping instead of inline functions to make use of the local thread state context
  359. #define __Pyx_Coroutine_SwapException(self) { \
  360. __Pyx_ExceptionSwap(&(self)->exc_type, &(self)->exc_value, &(self)->exc_traceback); \
  361. __Pyx_Coroutine_ResetFrameBackpointer(self); \
  362. }
  363. #define __Pyx_Coroutine_ResetAndClearException(self) { \
  364. __Pyx_ExceptionReset((self)->exc_type, (self)->exc_value, (self)->exc_traceback); \
  365. (self)->exc_type = (self)->exc_value = (self)->exc_traceback = NULL; \
  366. }
  367. #if CYTHON_FAST_THREAD_STATE
  368. #define __Pyx_PyGen_FetchStopIterationValue(pvalue) \
  369. __Pyx_PyGen__FetchStopIterationValue($local_tstate_cname, pvalue)
  370. #else
  371. #define __Pyx_PyGen_FetchStopIterationValue(pvalue) \
  372. __Pyx_PyGen__FetchStopIterationValue(__Pyx_PyThreadState_Current, pvalue)
  373. #endif
  374. static int __Pyx_PyGen__FetchStopIterationValue(PyThreadState *tstate, PyObject **pvalue); /*proto*/
  375. static CYTHON_INLINE void __Pyx_Coroutine_ResetFrameBackpointer(__pyx_CoroutineObject *self); /*proto*/
  376. //////////////////// Coroutine.proto ////////////////////
  377. #define __Pyx_Coroutine_USED
  378. static PyTypeObject *__pyx_CoroutineType = 0;
  379. static PyTypeObject *__pyx_CoroutineAwaitType = 0;
  380. #define __Pyx_Coroutine_CheckExact(obj) (Py_TYPE(obj) == __pyx_CoroutineType)
  381. // __Pyx_Coroutine_Check(obj): see override for IterableCoroutine below
  382. #define __Pyx_Coroutine_Check(obj) __Pyx_Coroutine_CheckExact(obj)
  383. #define __Pyx_CoroutineAwait_CheckExact(obj) (Py_TYPE(obj) == __pyx_CoroutineAwaitType)
  384. #define __Pyx_Coroutine_New(body, code, closure, name, qualname, module_name) \
  385. __Pyx__Coroutine_New(__pyx_CoroutineType, body, code, closure, name, qualname, module_name)
  386. static int __pyx_Coroutine_init(void); /*proto*/
  387. static PyObject *__Pyx__Coroutine_await(PyObject *coroutine); /*proto*/
  388. typedef struct {
  389. PyObject_HEAD
  390. PyObject *coroutine;
  391. } __pyx_CoroutineAwaitObject;
  392. static PyObject *__Pyx_CoroutineAwait_Close(__pyx_CoroutineAwaitObject *self); /*proto*/
  393. static PyObject *__Pyx_CoroutineAwait_Throw(__pyx_CoroutineAwaitObject *self, PyObject *args); /*proto*/
  394. //////////////////// Generator.proto ////////////////////
  395. #define __Pyx_Generator_USED
  396. static PyTypeObject *__pyx_GeneratorType = 0;
  397. #define __Pyx_Generator_CheckExact(obj) (Py_TYPE(obj) == __pyx_GeneratorType)
  398. #define __Pyx_Generator_New(body, code, closure, name, qualname, module_name) \
  399. __Pyx__Coroutine_New(__pyx_GeneratorType, body, code, closure, name, qualname, module_name)
  400. static PyObject *__Pyx_Generator_Next(PyObject *self);
  401. static int __pyx_Generator_init(void); /*proto*/
  402. //////////////////// AsyncGen ////////////////////
  403. //@requires: AsyncGen.c::AsyncGenerator
  404. // -> empty, only delegates to separate file
  405. //////////////////// CoroutineBase ////////////////////
  406. //@substitute: naming
  407. //@requires: Exceptions.c::PyErrFetchRestore
  408. //@requires: Exceptions.c::PyThreadStateGet
  409. //@requires: Exceptions.c::SwapException
  410. //@requires: Exceptions.c::RaiseException
  411. //@requires: Exceptions.c::SaveResetException
  412. //@requires: ObjectHandling.c::PyObjectCallMethod1
  413. //@requires: ObjectHandling.c::PyObjectGetAttrStr
  414. //@requires: CommonStructures.c::FetchCommonType
  415. #include <structmember.h>
  416. #include <frameobject.h>
  417. #define __Pyx_Coroutine_Undelegate(gen) Py_CLEAR((gen)->yieldfrom)
  418. // If StopIteration exception is set, fetches its 'value'
  419. // attribute if any, otherwise sets pvalue to None.
  420. //
  421. // Returns 0 if no exception or StopIteration is set.
  422. // If any other exception is set, returns -1 and leaves
  423. // pvalue unchanged.
  424. static int __Pyx_PyGen__FetchStopIterationValue(CYTHON_UNUSED PyThreadState *$local_tstate_cname, PyObject **pvalue) {
  425. PyObject *et, *ev, *tb;
  426. PyObject *value = NULL;
  427. __Pyx_ErrFetch(&et, &ev, &tb);
  428. if (!et) {
  429. Py_XDECREF(tb);
  430. Py_XDECREF(ev);
  431. Py_INCREF(Py_None);
  432. *pvalue = Py_None;
  433. return 0;
  434. }
  435. // most common case: plain StopIteration without or with separate argument
  436. if (likely(et == PyExc_StopIteration)) {
  437. if (!ev) {
  438. Py_INCREF(Py_None);
  439. value = Py_None;
  440. }
  441. #if PY_VERSION_HEX >= 0x030300A0
  442. else if (Py_TYPE(ev) == (PyTypeObject*)PyExc_StopIteration) {
  443. value = ((PyStopIterationObject *)ev)->value;
  444. Py_INCREF(value);
  445. Py_DECREF(ev);
  446. }
  447. #endif
  448. // PyErr_SetObject() and friends put the value directly into ev
  449. else if (unlikely(PyTuple_Check(ev))) {
  450. // if it's a tuple, it is interpreted as separate constructor arguments (surprise!)
  451. if (PyTuple_GET_SIZE(ev) >= 1) {
  452. #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
  453. value = PyTuple_GET_ITEM(ev, 0);
  454. Py_INCREF(value);
  455. #else
  456. value = PySequence_ITEM(ev, 0);
  457. #endif
  458. } else {
  459. Py_INCREF(Py_None);
  460. value = Py_None;
  461. }
  462. Py_DECREF(ev);
  463. }
  464. else if (!__Pyx_TypeCheck(ev, (PyTypeObject*)PyExc_StopIteration)) {
  465. // 'steal' reference to ev
  466. value = ev;
  467. }
  468. if (likely(value)) {
  469. Py_XDECREF(tb);
  470. Py_DECREF(et);
  471. *pvalue = value;
  472. return 0;
  473. }
  474. } else if (!__Pyx_PyErr_GivenExceptionMatches(et, PyExc_StopIteration)) {
  475. __Pyx_ErrRestore(et, ev, tb);
  476. return -1;
  477. }
  478. // otherwise: normalise and check what that gives us
  479. PyErr_NormalizeException(&et, &ev, &tb);
  480. if (unlikely(!PyObject_TypeCheck(ev, (PyTypeObject*)PyExc_StopIteration))) {
  481. // looks like normalisation failed - raise the new exception
  482. __Pyx_ErrRestore(et, ev, tb);
  483. return -1;
  484. }
  485. Py_XDECREF(tb);
  486. Py_DECREF(et);
  487. #if PY_VERSION_HEX >= 0x030300A0
  488. value = ((PyStopIterationObject *)ev)->value;
  489. Py_INCREF(value);
  490. Py_DECREF(ev);
  491. #else
  492. {
  493. PyObject* args = __Pyx_PyObject_GetAttrStr(ev, PYIDENT("args"));
  494. Py_DECREF(ev);
  495. if (likely(args)) {
  496. value = PySequence_GetItem(args, 0);
  497. Py_DECREF(args);
  498. }
  499. if (unlikely(!value)) {
  500. __Pyx_ErrRestore(NULL, NULL, NULL);
  501. Py_INCREF(Py_None);
  502. value = Py_None;
  503. }
  504. }
  505. #endif
  506. *pvalue = value;
  507. return 0;
  508. }
  509. static CYTHON_INLINE
  510. void __Pyx_Coroutine_ExceptionClear(__pyx_CoroutineObject *self) {
  511. PyObject *exc_type = self->exc_type;
  512. PyObject *exc_value = self->exc_value;
  513. PyObject *exc_traceback = self->exc_traceback;
  514. self->exc_type = NULL;
  515. self->exc_value = NULL;
  516. self->exc_traceback = NULL;
  517. Py_XDECREF(exc_type);
  518. Py_XDECREF(exc_value);
  519. Py_XDECREF(exc_traceback);
  520. }
  521. #define __Pyx_Coroutine_AlreadyRunningError(gen) (__Pyx__Coroutine_AlreadyRunningError(gen), (PyObject*)NULL)
  522. static void __Pyx__Coroutine_AlreadyRunningError(CYTHON_UNUSED __pyx_CoroutineObject *gen) {
  523. const char *msg;
  524. if (0) {
  525. #ifdef __Pyx_Coroutine_USED
  526. } else if (__Pyx_Coroutine_Check((PyObject*)gen)) {
  527. msg = "coroutine already executing";
  528. #endif
  529. #ifdef __Pyx_AsyncGen_USED
  530. } else if (__Pyx_AsyncGen_CheckExact((PyObject*)gen)) {
  531. msg = "async generator already executing";
  532. #endif
  533. } else {
  534. msg = "generator already executing";
  535. }
  536. PyErr_SetString(PyExc_ValueError, msg);
  537. }
  538. #define __Pyx_Coroutine_NotStartedError(gen) (__Pyx__Coroutine_NotStartedError(gen), (PyObject*)NULL)
  539. static void __Pyx__Coroutine_NotStartedError(CYTHON_UNUSED PyObject *gen) {
  540. const char *msg;
  541. if (0) {
  542. #ifdef __Pyx_Coroutine_USED
  543. } else if (__Pyx_Coroutine_Check(gen)) {
  544. msg = "can't send non-None value to a just-started coroutine";
  545. #endif
  546. #ifdef __Pyx_AsyncGen_USED
  547. } else if (__Pyx_AsyncGen_CheckExact(gen)) {
  548. msg = "can't send non-None value to a just-started async generator";
  549. #endif
  550. } else {
  551. msg = "can't send non-None value to a just-started generator";
  552. }
  553. PyErr_SetString(PyExc_TypeError, msg);
  554. }
  555. #define __Pyx_Coroutine_AlreadyTerminatedError(gen, value, closing) (__Pyx__Coroutine_AlreadyTerminatedError(gen, value, closing), (PyObject*)NULL)
  556. static void __Pyx__Coroutine_AlreadyTerminatedError(CYTHON_UNUSED PyObject *gen, PyObject *value, CYTHON_UNUSED int closing) {
  557. #ifdef __Pyx_Coroutine_USED
  558. if (!closing && __Pyx_Coroutine_Check(gen)) {
  559. // `self` is an exhausted coroutine: raise an error,
  560. // except when called from gen_close(), which should
  561. // always be a silent method.
  562. PyErr_SetString(PyExc_RuntimeError, "cannot reuse already awaited coroutine");
  563. } else
  564. #endif
  565. if (value) {
  566. // `gen` is an exhausted generator:
  567. // only set exception if called from send().
  568. #ifdef __Pyx_AsyncGen_USED
  569. if (__Pyx_AsyncGen_CheckExact(gen))
  570. PyErr_SetNone(__Pyx_PyExc_StopAsyncIteration);
  571. else
  572. #endif
  573. PyErr_SetNone(PyExc_StopIteration);
  574. }
  575. }
  576. static
  577. PyObject *__Pyx_Coroutine_SendEx(__pyx_CoroutineObject *self, PyObject *value, int closing) {
  578. __Pyx_PyThreadState_declare
  579. PyThreadState *tstate;
  580. PyObject *retval;
  581. assert(!self->is_running);
  582. if (unlikely(self->resume_label == 0)) {
  583. if (unlikely(value && value != Py_None)) {
  584. return __Pyx_Coroutine_NotStartedError((PyObject*)self);
  585. }
  586. }
  587. if (unlikely(self->resume_label == -1)) {
  588. return __Pyx_Coroutine_AlreadyTerminatedError((PyObject*)self, value, closing);
  589. }
  590. #if CYTHON_FAST_THREAD_STATE
  591. __Pyx_PyThreadState_assign
  592. tstate = $local_tstate_cname;
  593. #else
  594. tstate = __Pyx_PyThreadState_Current;
  595. #endif
  596. // Traceback/Frame rules:
  597. // - on entry, save external exception state in self->exc_*, restore it on exit
  598. // - on exit, keep internally generated exceptions in self->exc_*, clear everything else
  599. // - on entry, set "f_back" pointer of internal exception traceback to (current) outer call frame
  600. // - on exit, clear "f_back" of internal exception traceback
  601. // - do not touch external frames and tracebacks
  602. if (self->exc_type) {
  603. #if CYTHON_COMPILING_IN_PYPY || CYTHON_COMPILING_IN_PYSTON
  604. // FIXME: what to do in PyPy?
  605. #else
  606. // Generators always return to their most recent caller, not
  607. // necessarily their creator.
  608. if (self->exc_traceback) {
  609. PyTracebackObject *tb = (PyTracebackObject *) self->exc_traceback;
  610. PyFrameObject *f = tb->tb_frame;
  611. Py_XINCREF(tstate->frame);
  612. assert(f->f_back == NULL);
  613. f->f_back = tstate->frame;
  614. }
  615. #endif
  616. // We were in an except handler when we left,
  617. // restore the exception state which was put aside.
  618. __Pyx_ExceptionSwap(&self->exc_type, &self->exc_value,
  619. &self->exc_traceback);
  620. // self->exc_* now holds the exception state of the caller
  621. } else {
  622. // save away the exception state of the caller
  623. __Pyx_Coroutine_ExceptionClear(self);
  624. __Pyx_ExceptionSave(&self->exc_type, &self->exc_value, &self->exc_traceback);
  625. }
  626. self->is_running = 1;
  627. retval = self->body((PyObject *) self, tstate, value);
  628. self->is_running = 0;
  629. return retval;
  630. }
  631. static CYTHON_INLINE void __Pyx_Coroutine_ResetFrameBackpointer(__pyx_CoroutineObject *self) {
  632. // Don't keep the reference to f_back any longer than necessary. It
  633. // may keep a chain of frames alive or it could create a reference
  634. // cycle.
  635. if (likely(self->exc_traceback)) {
  636. #if CYTHON_COMPILING_IN_PYPY || CYTHON_COMPILING_IN_PYSTON
  637. // FIXME: what to do in PyPy?
  638. #else
  639. PyTracebackObject *tb = (PyTracebackObject *) self->exc_traceback;
  640. PyFrameObject *f = tb->tb_frame;
  641. Py_CLEAR(f->f_back);
  642. #endif
  643. }
  644. }
  645. static CYTHON_INLINE
  646. PyObject *__Pyx_Coroutine_MethodReturn(CYTHON_UNUSED PyObject* gen, PyObject *retval) {
  647. if (unlikely(!retval)) {
  648. __Pyx_PyThreadState_declare
  649. __Pyx_PyThreadState_assign
  650. if (!__Pyx_PyErr_Occurred()) {
  651. // method call must not terminate with NULL without setting an exception
  652. PyObject *exc = PyExc_StopIteration;
  653. #ifdef __Pyx_AsyncGen_USED
  654. if (__Pyx_AsyncGen_CheckExact(gen))
  655. exc = __Pyx_PyExc_StopAsyncIteration;
  656. #endif
  657. __Pyx_PyErr_SetNone(exc);
  658. }
  659. }
  660. return retval;
  661. }
  662. static CYTHON_INLINE
  663. PyObject *__Pyx_Coroutine_FinishDelegation(__pyx_CoroutineObject *gen) {
  664. PyObject *ret;
  665. PyObject *val = NULL;
  666. __Pyx_Coroutine_Undelegate(gen);
  667. __Pyx_PyGen__FetchStopIterationValue(__Pyx_PyThreadState_Current, &val);
  668. // val == NULL on failure => pass on exception
  669. ret = __Pyx_Coroutine_SendEx(gen, val, 0);
  670. Py_XDECREF(val);
  671. return ret;
  672. }
  673. static PyObject *__Pyx_Coroutine_Send(PyObject *self, PyObject *value) {
  674. PyObject *retval;
  675. __pyx_CoroutineObject *gen = (__pyx_CoroutineObject*) self;
  676. PyObject *yf = gen->yieldfrom;
  677. if (unlikely(gen->is_running))
  678. return __Pyx_Coroutine_AlreadyRunningError(gen);
  679. if (yf) {
  680. PyObject *ret;
  681. // FIXME: does this really need an INCREF() ?
  682. //Py_INCREF(yf);
  683. gen->is_running = 1;
  684. #ifdef __Pyx_Generator_USED
  685. if (__Pyx_Generator_CheckExact(yf)) {
  686. ret = __Pyx_Coroutine_Send(yf, value);
  687. } else
  688. #endif
  689. #ifdef __Pyx_Coroutine_USED
  690. if (__Pyx_Coroutine_Check(yf)) {
  691. ret = __Pyx_Coroutine_Send(yf, value);
  692. } else
  693. #endif
  694. #ifdef __Pyx_AsyncGen_USED
  695. if (__pyx_PyAsyncGenASend_CheckExact(yf)) {
  696. ret = __Pyx_async_gen_asend_send(yf, value);
  697. } else
  698. #endif
  699. #if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x03030000 && (defined(__linux__) || PY_VERSION_HEX >= 0x030600B3)
  700. // _PyGen_Send() is not exported before Py3.6
  701. if (PyGen_CheckExact(yf)) {
  702. ret = _PyGen_Send((PyGenObject*)yf, value == Py_None ? NULL : value);
  703. } else
  704. #endif
  705. #if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x03050000 && defined(PyCoro_CheckExact) && (defined(__linux__) || PY_VERSION_HEX >= 0x030600B3)
  706. // _PyGen_Send() is not exported before Py3.6
  707. if (PyCoro_CheckExact(yf)) {
  708. ret = _PyGen_Send((PyGenObject*)yf, value == Py_None ? NULL : value);
  709. } else
  710. #endif
  711. {
  712. if (value == Py_None)
  713. ret = Py_TYPE(yf)->tp_iternext(yf);
  714. else
  715. ret = __Pyx_PyObject_CallMethod1(yf, PYIDENT("send"), value);
  716. }
  717. gen->is_running = 0;
  718. //Py_DECREF(yf);
  719. if (likely(ret)) {
  720. return ret;
  721. }
  722. retval = __Pyx_Coroutine_FinishDelegation(gen);
  723. } else {
  724. retval = __Pyx_Coroutine_SendEx(gen, value, 0);
  725. }
  726. return __Pyx_Coroutine_MethodReturn(self, retval);
  727. }
  728. // This helper function is used by gen_close and gen_throw to
  729. // close a subiterator being delegated to by yield-from.
  730. static int __Pyx_Coroutine_CloseIter(__pyx_CoroutineObject *gen, PyObject *yf) {
  731. PyObject *retval = NULL;
  732. int err = 0;
  733. #ifdef __Pyx_Generator_USED
  734. if (__Pyx_Generator_CheckExact(yf)) {
  735. retval = __Pyx_Coroutine_Close(yf);
  736. if (!retval)
  737. return -1;
  738. } else
  739. #endif
  740. #ifdef __Pyx_Coroutine_USED
  741. if (__Pyx_Coroutine_Check(yf)) {
  742. retval = __Pyx_Coroutine_Close(yf);
  743. if (!retval)
  744. return -1;
  745. } else
  746. if (__Pyx_CoroutineAwait_CheckExact(yf)) {
  747. retval = __Pyx_CoroutineAwait_Close((__pyx_CoroutineAwaitObject*)yf);
  748. if (!retval)
  749. return -1;
  750. } else
  751. #endif
  752. #ifdef __Pyx_AsyncGen_USED
  753. if (__pyx_PyAsyncGenASend_CheckExact(yf)) {
  754. retval = __Pyx_async_gen_asend_close(yf, NULL);
  755. // cannot fail
  756. } else
  757. if (__pyx_PyAsyncGenAThrow_CheckExact(yf)) {
  758. retval = __Pyx_async_gen_athrow_close(yf, NULL);
  759. // cannot fail
  760. } else
  761. #endif
  762. {
  763. PyObject *meth;
  764. gen->is_running = 1;
  765. meth = __Pyx_PyObject_GetAttrStr(yf, PYIDENT("close"));
  766. if (unlikely(!meth)) {
  767. if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
  768. PyErr_WriteUnraisable(yf);
  769. }
  770. PyErr_Clear();
  771. } else {
  772. retval = PyObject_CallFunction(meth, NULL);
  773. Py_DECREF(meth);
  774. if (!retval)
  775. err = -1;
  776. }
  777. gen->is_running = 0;
  778. }
  779. Py_XDECREF(retval);
  780. return err;
  781. }
  782. static PyObject *__Pyx_Generator_Next(PyObject *self) {
  783. __pyx_CoroutineObject *gen = (__pyx_CoroutineObject*) self;
  784. PyObject *yf = gen->yieldfrom;
  785. if (unlikely(gen->is_running))
  786. return __Pyx_Coroutine_AlreadyRunningError(gen);
  787. if (yf) {
  788. PyObject *ret;
  789. // FIXME: does this really need an INCREF() ?
  790. //Py_INCREF(yf);
  791. // YieldFrom code ensures that yf is an iterator
  792. gen->is_running = 1;
  793. #ifdef __Pyx_Generator_USED
  794. if (__Pyx_Generator_CheckExact(yf)) {
  795. ret = __Pyx_Generator_Next(yf);
  796. } else
  797. #endif
  798. #if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x03030000 && (defined(__linux__) || PY_VERSION_HEX >= 0x030600B3)
  799. // _PyGen_Send() is not exported before Py3.6
  800. if (PyGen_CheckExact(yf)) {
  801. ret = _PyGen_Send((PyGenObject*)yf, NULL);
  802. } else
  803. #endif
  804. #ifdef __Pyx_Coroutine_USED
  805. if (__Pyx_Coroutine_Check(yf)) {
  806. ret = __Pyx_Coroutine_Send(yf, Py_None);
  807. } else
  808. #endif
  809. ret = Py_TYPE(yf)->tp_iternext(yf);
  810. gen->is_running = 0;
  811. //Py_DECREF(yf);
  812. if (likely(ret)) {
  813. return ret;
  814. }
  815. return __Pyx_Coroutine_FinishDelegation(gen);
  816. }
  817. return __Pyx_Coroutine_SendEx(gen, Py_None, 0);
  818. }
  819. static PyObject *__Pyx_Coroutine_Close(PyObject *self) {
  820. __pyx_CoroutineObject *gen = (__pyx_CoroutineObject *) self;
  821. PyObject *retval, *raised_exception;
  822. PyObject *yf = gen->yieldfrom;
  823. int err = 0;
  824. if (unlikely(gen->is_running))
  825. return __Pyx_Coroutine_AlreadyRunningError(gen);
  826. if (yf) {
  827. Py_INCREF(yf);
  828. err = __Pyx_Coroutine_CloseIter(gen, yf);
  829. __Pyx_Coroutine_Undelegate(gen);
  830. Py_DECREF(yf);
  831. }
  832. if (err == 0)
  833. PyErr_SetNone(PyExc_GeneratorExit);
  834. retval = __Pyx_Coroutine_SendEx(gen, NULL, 1);
  835. if (unlikely(retval)) {
  836. const char *msg;
  837. Py_DECREF(retval);
  838. if ((0)) {
  839. #ifdef __Pyx_Coroutine_USED
  840. } else if (__Pyx_Coroutine_Check(self)) {
  841. msg = "coroutine ignored GeneratorExit";
  842. #endif
  843. #ifdef __Pyx_AsyncGen_USED
  844. } else if (__Pyx_AsyncGen_CheckExact(self)) {
  845. #if PY_VERSION_HEX < 0x03060000
  846. msg = "async generator ignored GeneratorExit - might require Python 3.6+ finalisation (PEP 525)";
  847. #else
  848. msg = "async generator ignored GeneratorExit";
  849. #endif
  850. #endif
  851. } else {
  852. msg = "generator ignored GeneratorExit";
  853. }
  854. PyErr_SetString(PyExc_RuntimeError, msg);
  855. return NULL;
  856. }
  857. raised_exception = PyErr_Occurred();
  858. if (likely(!raised_exception || __Pyx_PyErr_GivenExceptionMatches2(raised_exception, PyExc_GeneratorExit, PyExc_StopIteration))) {
  859. // ignore these errors
  860. if (raised_exception) PyErr_Clear();
  861. Py_INCREF(Py_None);
  862. return Py_None;
  863. }
  864. return NULL;
  865. }
  866. static PyObject *__Pyx__Coroutine_Throw(PyObject *self, PyObject *typ, PyObject *val, PyObject *tb,
  867. PyObject *args, int close_on_genexit) {
  868. __pyx_CoroutineObject *gen = (__pyx_CoroutineObject *) self;
  869. PyObject *yf = gen->yieldfrom;
  870. if (unlikely(gen->is_running))
  871. return __Pyx_Coroutine_AlreadyRunningError(gen);
  872. if (yf) {
  873. PyObject *ret;
  874. Py_INCREF(yf);
  875. if (__Pyx_PyErr_GivenExceptionMatches(typ, PyExc_GeneratorExit) && close_on_genexit) {
  876. // Asynchronous generators *should not* be closed right away.
  877. // We have to allow some awaits to work it through, hence the
  878. // `close_on_genexit` parameter here.
  879. int err = __Pyx_Coroutine_CloseIter(gen, yf);
  880. Py_DECREF(yf);
  881. __Pyx_Coroutine_Undelegate(gen);
  882. if (err < 0)
  883. return __Pyx_Coroutine_MethodReturn(self, __Pyx_Coroutine_SendEx(gen, NULL, 0));
  884. goto throw_here;
  885. }
  886. gen->is_running = 1;
  887. if (0
  888. #ifdef __Pyx_Generator_USED
  889. || __Pyx_Generator_CheckExact(yf)
  890. #endif
  891. #ifdef __Pyx_Coroutine_USED
  892. || __Pyx_Coroutine_Check(yf)
  893. #endif
  894. ) {
  895. ret = __Pyx__Coroutine_Throw(yf, typ, val, tb, args, close_on_genexit);
  896. #ifdef __Pyx_Coroutine_USED
  897. } else if (__Pyx_CoroutineAwait_CheckExact(yf)) {
  898. ret = __Pyx__Coroutine_Throw(((__pyx_CoroutineAwaitObject*)yf)->coroutine, typ, val, tb, args, close_on_genexit);
  899. #endif
  900. } else {
  901. PyObject *meth = __Pyx_PyObject_GetAttrStr(yf, PYIDENT("throw"));
  902. if (unlikely(!meth)) {
  903. Py_DECREF(yf);
  904. if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
  905. gen->is_running = 0;
  906. return NULL;
  907. }
  908. PyErr_Clear();
  909. __Pyx_Coroutine_Undelegate(gen);
  910. gen->is_running = 0;
  911. goto throw_here;
  912. }
  913. if (likely(args)) {
  914. ret = PyObject_CallObject(meth, args);
  915. } else {
  916. // "tb" or even "val" might be NULL, but that also correctly terminates the argument list
  917. ret = PyObject_CallFunctionObjArgs(meth, typ, val, tb, NULL);
  918. }
  919. Py_DECREF(meth);
  920. }
  921. gen->is_running = 0;
  922. Py_DECREF(yf);
  923. if (!ret) {
  924. ret = __Pyx_Coroutine_FinishDelegation(gen);
  925. }
  926. return __Pyx_Coroutine_MethodReturn(self, ret);
  927. }
  928. throw_here:
  929. __Pyx_Raise(typ, val, tb, NULL);
  930. return __Pyx_Coroutine_MethodReturn(self, __Pyx_Coroutine_SendEx(gen, NULL, 0));
  931. }
  932. static PyObject *__Pyx_Coroutine_Throw(PyObject *self, PyObject *args) {
  933. PyObject *typ;
  934. PyObject *val = NULL;
  935. PyObject *tb = NULL;
  936. if (!PyArg_UnpackTuple(args, (char *)"throw", 1, 3, &typ, &val, &tb))
  937. return NULL;
  938. return __Pyx__Coroutine_Throw(self, typ, val, tb, args, 1);
  939. }
  940. static int __Pyx_Coroutine_traverse(__pyx_CoroutineObject *gen, visitproc visit, void *arg) {
  941. Py_VISIT(gen->closure);
  942. Py_VISIT(gen->classobj);
  943. Py_VISIT(gen->yieldfrom);
  944. Py_VISIT(gen->exc_type);
  945. Py_VISIT(gen->exc_value);
  946. Py_VISIT(gen->exc_traceback);
  947. return 0;
  948. }
  949. static int __Pyx_Coroutine_clear(PyObject *self) {
  950. __pyx_CoroutineObject *gen = (__pyx_CoroutineObject *) self;
  951. Py_CLEAR(gen->closure);
  952. Py_CLEAR(gen->classobj);
  953. Py_CLEAR(gen->yieldfrom);
  954. Py_CLEAR(gen->exc_type);
  955. Py_CLEAR(gen->exc_value);
  956. Py_CLEAR(gen->exc_traceback);
  957. #ifdef __Pyx_AsyncGen_USED
  958. if (__Pyx_AsyncGen_CheckExact(self)) {
  959. Py_CLEAR(((__pyx_PyAsyncGenObject*)gen)->ag_finalizer);
  960. }
  961. #endif
  962. Py_CLEAR(gen->gi_code);
  963. Py_CLEAR(gen->gi_name);
  964. Py_CLEAR(gen->gi_qualname);
  965. Py_CLEAR(gen->gi_modulename);
  966. return 0;
  967. }
  968. static void __Pyx_Coroutine_dealloc(PyObject *self) {
  969. __pyx_CoroutineObject *gen = (__pyx_CoroutineObject *) self;
  970. PyObject_GC_UnTrack(gen);
  971. if (gen->gi_weakreflist != NULL)
  972. PyObject_ClearWeakRefs(self);
  973. if (gen->resume_label >= 0) {
  974. // Generator is paused or unstarted, so we need to close
  975. PyObject_GC_Track(self);
  976. #if PY_VERSION_HEX >= 0x030400a1 && CYTHON_USE_TP_FINALIZE
  977. if (PyObject_CallFinalizerFromDealloc(self))
  978. #else
  979. Py_TYPE(gen)->tp_del(self);
  980. if (self->ob_refcnt > 0)
  981. #endif
  982. {
  983. // resurrected. :(
  984. return;
  985. }
  986. PyObject_GC_UnTrack(self);
  987. }
  988. #ifdef __Pyx_AsyncGen_USED
  989. if (__Pyx_AsyncGen_CheckExact(self)) {
  990. /* We have to handle this case for asynchronous generators
  991. right here, because this code has to be between UNTRACK
  992. and GC_Del. */
  993. Py_CLEAR(((__pyx_PyAsyncGenObject*)self)->ag_finalizer);
  994. }
  995. #endif
  996. __Pyx_Coroutine_clear(self);
  997. PyObject_GC_Del(gen);
  998. }
  999. static void __Pyx_Coroutine_del(PyObject *self) {
  1000. PyObject *error_type, *error_value, *error_traceback;
  1001. __pyx_CoroutineObject *gen = (__pyx_CoroutineObject *) self;
  1002. __Pyx_PyThreadState_declare
  1003. if (gen->resume_label < 0) {
  1004. // already terminated => nothing to clean up
  1005. return;
  1006. }
  1007. #if !CYTHON_USE_TP_FINALIZE
  1008. // Temporarily resurrect the object.
  1009. assert(self->ob_refcnt == 0);
  1010. self->ob_refcnt = 1;
  1011. #endif
  1012. __Pyx_PyThreadState_assign
  1013. // Save the current exception, if any.
  1014. __Pyx_ErrFetch(&error_type, &error_value, &error_traceback);
  1015. #ifdef __Pyx_AsyncGen_USED
  1016. if (__Pyx_AsyncGen_CheckExact(self)) {
  1017. __pyx_PyAsyncGenObject *agen = (__pyx_PyAsyncGenObject*)self;
  1018. PyObject *finalizer = agen->ag_finalizer;
  1019. if (finalizer && !agen->ag_closed) {
  1020. PyObject *res = __Pyx_PyObject_CallOneArg(finalizer, self);
  1021. if (unlikely(!res)) {
  1022. PyErr_WriteUnraisable(self);
  1023. } else {
  1024. Py_DECREF(res);
  1025. }
  1026. // Restore the saved exception.
  1027. __Pyx_ErrRestore(error_type, error_value, error_traceback);
  1028. return;
  1029. }
  1030. }
  1031. #endif
  1032. if (unlikely(gen->resume_label == 0 && !error_value)) {
  1033. #ifdef __Pyx_Coroutine_USED
  1034. #ifdef __Pyx_Generator_USED
  1035. // only warn about (async) coroutines
  1036. if (!__Pyx_Generator_CheckExact(self))
  1037. #endif
  1038. {
  1039. // untrack dead object as we are executing Python code (which might trigger GC)
  1040. PyObject_GC_UnTrack(self);
  1041. #if PY_MAJOR_VERSION >= 3 /* PY_VERSION_HEX >= 0x03030000*/ || defined(PyErr_WarnFormat)
  1042. if (unlikely(PyErr_WarnFormat(PyExc_RuntimeWarning, 1, "coroutine '%.50S' was never awaited", gen->gi_qualname) < 0))
  1043. PyErr_WriteUnraisable(self);
  1044. #else
  1045. {PyObject *msg;
  1046. char *cmsg;
  1047. #if CYTHON_COMPILING_IN_PYPY
  1048. msg = NULL;
  1049. cmsg = (char*) "coroutine was never awaited";
  1050. #else
  1051. char *cname;
  1052. PyObject *qualname;
  1053. qualname = gen->gi_qualname;
  1054. cname = PyString_AS_STRING(qualname);
  1055. msg = PyString_FromFormat("coroutine '%.50s' was never awaited", cname);
  1056. if (unlikely(!msg)) {
  1057. PyErr_Clear();
  1058. cmsg = (char*) "coroutine was never awaited";
  1059. } else {
  1060. cmsg = PyString_AS_STRING(msg);
  1061. }
  1062. #endif
  1063. if (unlikely(PyErr_WarnEx(PyExc_RuntimeWarning, cmsg, 1) < 0))
  1064. PyErr_WriteUnraisable(self);
  1065. Py_XDECREF(msg);}
  1066. #endif
  1067. PyObject_GC_Track(self);
  1068. }
  1069. #endif /*__Pyx_Coroutine_USED*/
  1070. } else {
  1071. PyObject *res = __Pyx_Coroutine_Close(self);
  1072. if (unlikely(!res)) {
  1073. if (PyErr_Occurred())
  1074. PyErr_WriteUnraisable(self);
  1075. } else {
  1076. Py_DECREF(res);
  1077. }
  1078. }
  1079. // Restore the saved exception.
  1080. __Pyx_ErrRestore(error_type, error_value, error_traceback);
  1081. #if !CYTHON_USE_TP_FINALIZE
  1082. // Undo the temporary resurrection; can't use DECREF here, it would
  1083. // cause a recursive call.
  1084. assert(self->ob_refcnt > 0);
  1085. if (--self->ob_refcnt == 0) {
  1086. // this is the normal path out
  1087. return;
  1088. }
  1089. // close() resurrected it! Make it look like the original Py_DECREF
  1090. // never happened.
  1091. {
  1092. Py_ssize_t refcnt = self->ob_refcnt;
  1093. _Py_NewReference(self);
  1094. self->ob_refcnt = refcnt;
  1095. }
  1096. #if CYTHON_COMPILING_IN_CPYTHON
  1097. assert(PyType_IS_GC(self->ob_type) &&
  1098. _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
  1099. // If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
  1100. // we need to undo that.
  1101. _Py_DEC_REFTOTAL;
  1102. #endif
  1103. // If Py_TRACE_REFS, _Py_NewReference re-added self to the object
  1104. // chain, so no more to do there.
  1105. // If COUNT_ALLOCS, the original decref bumped tp_frees, and
  1106. // _Py_NewReference bumped tp_allocs: both of those need to be
  1107. // undone.
  1108. #ifdef COUNT_ALLOCS
  1109. --Py_TYPE(self)->tp_frees;
  1110. --Py_TYPE(self)->tp_allocs;
  1111. #endif
  1112. #endif
  1113. }
  1114. static PyObject *
  1115. __Pyx_Coroutine_get_name(__pyx_CoroutineObject *self)
  1116. {
  1117. PyObject *name = self->gi_name;
  1118. // avoid NULL pointer dereference during garbage collection
  1119. if (unlikely(!name)) name = Py_None;
  1120. Py_INCREF(name);
  1121. return name;
  1122. }
  1123. static int
  1124. __Pyx_Coroutine_set_name(__pyx_CoroutineObject *self, PyObject *value)
  1125. {
  1126. PyObject *tmp;
  1127. #if PY_MAJOR_VERSION >= 3
  1128. if (unlikely(value == NULL || !PyUnicode_Check(value))) {
  1129. #else
  1130. if (unlikely(value == NULL || !PyString_Check(value))) {
  1131. #endif
  1132. PyErr_SetString(PyExc_TypeError,
  1133. "__name__ must be set to a string object");
  1134. return -1;
  1135. }
  1136. tmp = self->gi_name;
  1137. Py_INCREF(value);
  1138. self->gi_name = value;
  1139. Py_XDECREF(tmp);
  1140. return 0;
  1141. }
  1142. static PyObject *
  1143. __Pyx_Coroutine_get_qualname(__pyx_CoroutineObject *self)
  1144. {
  1145. PyObject *name = self->gi_qualname;
  1146. // avoid NULL pointer dereference during garbage collection
  1147. if (unlikely(!name)) name = Py_None;
  1148. Py_INCREF(name);
  1149. return name;
  1150. }
  1151. static int
  1152. __Pyx_Coroutine_set_qualname(__pyx_CoroutineObject *self, PyObject *value)
  1153. {
  1154. PyObject *tmp;
  1155. #if PY_MAJOR_VERSION >= 3
  1156. if (unlikely(value == NULL || !PyUnicode_Check(value))) {
  1157. #else
  1158. if (unlikely(value == NULL || !PyString_Check(value))) {
  1159. #endif
  1160. PyErr_SetString(PyExc_TypeError,
  1161. "__qualname__ must be set to a string object");
  1162. return -1;
  1163. }
  1164. tmp = self->gi_qualname;
  1165. Py_INCREF(value);
  1166. self->gi_qualname = value;
  1167. Py_XDECREF(tmp);
  1168. return 0;
  1169. }
  1170. static __pyx_CoroutineObject *__Pyx__Coroutine_New(
  1171. PyTypeObject* type, __pyx_coroutine_body_t body, PyObject *code, PyObject *closure,
  1172. PyObject *name, PyObject *qualname, PyObject *module_name) {
  1173. __pyx_CoroutineObject *gen = PyObject_GC_New(__pyx_CoroutineObject, type);
  1174. if (unlikely(!gen))
  1175. return NULL;
  1176. return __Pyx__Coroutine_NewInit(gen, body, code, closure, name, qualname, module_name);
  1177. }
  1178. static __pyx_CoroutineObject *__Pyx__Coroutine_NewInit(
  1179. __pyx_CoroutineObject *gen, __pyx_coroutine_body_t body, PyObject *code, PyObject *closure,
  1180. PyObject *name, PyObject *qualname, PyObject *module_name) {
  1181. gen->body = body;
  1182. gen->closure = closure;
  1183. Py_XINCREF(closure);
  1184. gen->is_running = 0;
  1185. gen->resume_label = 0;
  1186. gen->classobj = NULL;
  1187. gen->yieldfrom = NULL;
  1188. gen->exc_type = NULL;
  1189. gen->exc_value = NULL;
  1190. gen->exc_traceback = NULL;
  1191. gen->gi_weakreflist = NULL;
  1192. Py_XINCREF(qualname);
  1193. gen->gi_qualname = qualname;
  1194. Py_XINCREF(name);
  1195. gen->gi_name = name;
  1196. Py_XINCREF(module_name);
  1197. gen->gi_modulename = module_name;
  1198. Py_XINCREF(code);
  1199. gen->gi_code = code;
  1200. PyObject_GC_Track(gen);
  1201. return gen;
  1202. }
  1203. //////////////////// Coroutine ////////////////////
  1204. //@requires: CoroutineBase
  1205. //@requires: PatchGeneratorABC
  1206. //@requires: ObjectHandling.c::PyObject_GenericGetAttrNoDict
  1207. static void __Pyx_CoroutineAwait_dealloc(PyObject *self) {
  1208. PyObject_GC_UnTrack(self);
  1209. Py_CLEAR(((__pyx_CoroutineAwaitObject*)self)->coroutine);
  1210. PyObject_GC_Del(self);
  1211. }
  1212. static int __Pyx_CoroutineAwait_traverse(__pyx_CoroutineAwaitObject *self, visitproc visit, void *arg) {
  1213. Py_VISIT(self->coroutine);
  1214. return 0;
  1215. }
  1216. static int __Pyx_CoroutineAwait_clear(__pyx_CoroutineAwaitObject *self) {
  1217. Py_CLEAR(self->coroutine);
  1218. return 0;
  1219. }
  1220. static PyObject *__Pyx_CoroutineAwait_Next(__pyx_CoroutineAwaitObject *self) {
  1221. return __Pyx_Generator_Next(self->coroutine);
  1222. }
  1223. static PyObject *__Pyx_CoroutineAwait_Send(__pyx_CoroutineAwaitObject *self, PyObject *value) {
  1224. return __Pyx_Coroutine_Send(self->coroutine, value);
  1225. }
  1226. static PyObject *__Pyx_CoroutineAwait_Throw(__pyx_CoroutineAwaitObject *self, PyObject *args) {
  1227. return __Pyx_Coroutine_Throw(self->coroutine, args);
  1228. }
  1229. static PyObject *__Pyx_CoroutineAwait_Close(__pyx_CoroutineAwaitObject *self) {
  1230. return __Pyx_Coroutine_Close(self->coroutine);
  1231. }
  1232. static PyObject *__Pyx_CoroutineAwait_self(PyObject *self) {
  1233. Py_INCREF(self);
  1234. return self;
  1235. }
  1236. #if !CYTHON_COMPILING_IN_PYPY
  1237. static PyObject *__Pyx_CoroutineAwait_no_new(CYTHON_UNUSED PyTypeObject *type, CYTHON_UNUSED PyObject *args, CYTHON_UNUSED PyObject *kwargs) {
  1238. PyErr_SetString(PyExc_TypeError, "cannot instantiate type, use 'await coroutine' instead");
  1239. return NULL;
  1240. }
  1241. #endif
  1242. static PyMethodDef __pyx_CoroutineAwait_methods[] = {
  1243. {"send", (PyCFunction) __Pyx_CoroutineAwait_Send, METH_O,
  1244. (char*) PyDoc_STR("send(arg) -> send 'arg' into coroutine,\nreturn next yielded value or raise StopIteration.")},
  1245. {"throw", (PyCFunction) __Pyx_CoroutineAwait_Throw, METH_VARARGS,
  1246. (char*) PyDoc_STR("throw(typ[,val[,tb]]) -> raise exception in coroutine,\nreturn next yielded value or raise StopIteration.")},
  1247. {"close", (PyCFunction) __Pyx_CoroutineAwait_Close, METH_NOARGS,
  1248. (char*) PyDoc_STR("close() -> raise GeneratorExit inside coroutine.")},
  1249. {0, 0, 0, 0}
  1250. };
  1251. static PyTypeObject __pyx_CoroutineAwaitType_type = {
  1252. PyVarObject_HEAD_INIT(0, 0)
  1253. "coroutine_wrapper", /*tp_name*/
  1254. sizeof(__pyx_CoroutineAwaitObject), /*tp_basicsize*/
  1255. 0, /*tp_itemsize*/
  1256. (destructor) __Pyx_CoroutineAwait_dealloc,/*tp_dealloc*/
  1257. 0, /*tp_print*/
  1258. 0, /*tp_getattr*/
  1259. 0, /*tp_setattr*/
  1260. 0, /*tp_as_async resp. tp_compare*/
  1261. 0, /*tp_repr*/
  1262. 0, /*tp_as_number*/
  1263. 0, /*tp_as_sequence*/
  1264. 0, /*tp_as_mapping*/
  1265. 0, /*tp_hash*/
  1266. 0, /*tp_call*/
  1267. 0, /*tp_str*/
  1268. 0, /*tp_getattro*/
  1269. 0, /*tp_setattro*/
  1270. 0, /*tp_as_buffer*/
  1271. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
  1272. PyDoc_STR("A wrapper object implementing __await__ for coroutines."), /*tp_doc*/
  1273. (traverseproc) __Pyx_CoroutineAwait_traverse, /*tp_traverse*/
  1274. (inquiry) __Pyx_CoroutineAwait_clear, /*tp_clear*/
  1275. 0, /*tp_richcompare*/
  1276. 0, /*tp_weaklistoffset*/
  1277. __Pyx_CoroutineAwait_self, /*tp_iter*/
  1278. (iternextfunc) __Pyx_CoroutineAwait_Next, /*tp_iternext*/
  1279. __pyx_CoroutineAwait_methods, /*tp_methods*/
  1280. 0 , /*tp_members*/
  1281. 0 , /*tp_getset*/
  1282. 0, /*tp_base*/
  1283. 0, /*tp_dict*/
  1284. 0, /*tp_descr_get*/
  1285. 0, /*tp_descr_set*/
  1286. 0, /*tp_dictoffset*/
  1287. 0, /*tp_init*/
  1288. 0, /*tp_alloc*/
  1289. #if !CYTHON_COMPILING_IN_PYPY
  1290. __Pyx_CoroutineAwait_no_new, /*tp_new*/
  1291. #else
  1292. 0, /*tp_new*/
  1293. #endif
  1294. 0, /*tp_free*/
  1295. 0, /*tp_is_gc*/
  1296. 0, /*tp_bases*/
  1297. 0, /*tp_mro*/
  1298. 0, /*tp_cache*/
  1299. 0, /*tp_subclasses*/
  1300. 0, /*tp_weaklist*/
  1301. 0, /*tp_del*/
  1302. 0, /*tp_version_tag*/
  1303. #if PY_VERSION_HEX >= 0x030400a1
  1304. 0, /*tp_finalize*/
  1305. #endif
  1306. };
  1307. static CYTHON_INLINE PyObject *__Pyx__Coroutine_await(PyObject *coroutine) {
  1308. __pyx_CoroutineAwaitObject *await = PyObject_GC_New(__pyx_CoroutineAwaitObject, __pyx_CoroutineAwaitType);
  1309. if (unlikely(!await)) return NULL;
  1310. Py_INCREF(coroutine);
  1311. await->coroutine = coroutine;
  1312. PyObject_GC_Track(await);
  1313. return (PyObject*)await;
  1314. }
  1315. static PyObject *__Pyx_Coroutine_await(PyObject *coroutine) {
  1316. if (unlikely(!coroutine || !__Pyx_Coroutine_Check(coroutine))) {
  1317. PyErr_SetString(PyExc_TypeError, "invalid input, expected coroutine");
  1318. return NULL;
  1319. }
  1320. return __Pyx__Coroutine_await(coroutine);
  1321. }
  1322. static PyObject *
  1323. __Pyx_Coroutine_get_frame(CYTHON_UNUSED __pyx_CoroutineObject *self)
  1324. {
  1325. // Fake implementation that always returns None, but at least does not raise an AttributeError.
  1326. Py_RETURN_NONE;
  1327. }
  1328. #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3 && PY_VERSION_HEX < 0x030500B1
  1329. static PyObject *__Pyx_Coroutine_compare(PyObject *obj, PyObject *other, int op) {
  1330. PyObject* result;
  1331. switch (op) {
  1332. case Py_EQ: result = (other == obj) ? Py_True : Py_False; break;
  1333. case Py_NE: result = (other != obj) ? Py_True : Py_False; break;
  1334. default:
  1335. result = Py_NotImplemented;
  1336. }
  1337. Py_INCREF(result);
  1338. return result;
  1339. }
  1340. #endif
  1341. static PyMethodDef __pyx_Coroutine_methods[] = {
  1342. {"send", (PyCFunction) __Pyx_Coroutine_Send, METH_O,
  1343. (char*) PyDoc_STR("send(arg) -> send 'arg' into coroutine,\nreturn next iterated value or raise StopIteration.")},
  1344. {"throw", (PyCFunction) __Pyx_Coroutine_Throw, METH_VARARGS,
  1345. (char*) PyDoc_STR("throw(typ[,val[,tb]]) -> raise exception in coroutine,\nreturn next iterated value or raise StopIteration.")},
  1346. {"close", (PyCFunction) __Pyx_Coroutine_Close, METH_NOARGS,
  1347. (char*) PyDoc_STR("close() -> raise GeneratorExit inside coroutine.")},
  1348. #if PY_VERSION_HEX < 0x030500B1
  1349. {"__await__", (PyCFunction) __Pyx_Coroutine_await, METH_NOARGS,
  1350. (char*) PyDoc_STR("__await__() -> return an iterator to be used in await expression.")},
  1351. #endif
  1352. {0, 0, 0, 0}
  1353. };
  1354. static PyMemberDef __pyx_Coroutine_memberlist[] = {
  1355. {(char *) "cr_running", T_BOOL, offsetof(__pyx_CoroutineObject, is_running), READONLY, NULL},
  1356. {(char*) "cr_await", T_OBJECT, offsetof(__pyx_CoroutineObject, yieldfrom), READONLY,
  1357. (char*) PyDoc_STR("object being awaited, or None")},
  1358. {(char*) "cr_code", T_OBJECT, offsetof(__pyx_CoroutineObject, gi_code), READONLY, NULL},
  1359. {(char *) "__module__", T_OBJECT, offsetof(__pyx_CoroutineObject, gi_modulename), PY_WRITE_RESTRICTED, 0},
  1360. {0, 0, 0, 0, 0}
  1361. };
  1362. static PyGetSetDef __pyx_Coroutine_getsets[] = {
  1363. {(char *) "__name__", (getter)__Pyx_Coroutine_get_name, (setter)__Pyx_Coroutine_set_name,
  1364. (char*) PyDoc_STR("name of the coroutine"), 0},
  1365. {(char *) "__qualname__", (getter)__Pyx_Coroutine_get_qualname, (setter)__Pyx_Coroutine_set_qualname,
  1366. (char*) PyDoc_STR("qualified name of the coroutine"), 0},
  1367. {(char *) "cr_frame", (getter)__Pyx_Coroutine_get_frame, NULL,
  1368. (char*) PyDoc_STR("Frame of the coroutine"), 0},
  1369. {0, 0, 0, 0, 0}
  1370. };
  1371. #if CYTHON_USE_ASYNC_SLOTS
  1372. static __Pyx_PyAsyncMethodsStruct __pyx_Coroutine_as_async = {
  1373. __Pyx_Coroutine_await, /*am_await*/
  1374. 0, /*am_aiter*/
  1375. 0, /*am_anext*/
  1376. };
  1377. #endif
  1378. static PyTypeObject __pyx_CoroutineType_type = {
  1379. PyVarObject_HEAD_INIT(0, 0)
  1380. "coroutine", /*tp_name*/
  1381. sizeof(__pyx_CoroutineObject), /*tp_basicsize*/
  1382. 0, /*tp_itemsize*/
  1383. (destructor) __Pyx_Coroutine_dealloc,/*tp_dealloc*/
  1384. 0, /*tp_print*/
  1385. 0, /*tp_getattr*/
  1386. 0, /*tp_setattr*/
  1387. #if CYTHON_USE_ASYNC_SLOTS
  1388. &__pyx_Coroutine_as_async, /*tp_as_async (tp_reserved) - Py3 only! */
  1389. #else
  1390. 0, /*tp_reserved*/
  1391. #endif
  1392. 0, /*tp_repr*/
  1393. 0, /*tp_as_number*/
  1394. 0, /*tp_as_sequence*/
  1395. 0, /*tp_as_mapping*/
  1396. 0, /*tp_hash*/
  1397. 0, /*tp_call*/
  1398. 0, /*tp_str*/
  1399. 0, /*tp_getattro*/
  1400. 0, /*tp_setattro*/
  1401. 0, /*tp_as_buffer*/
  1402. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/
  1403. 0, /*tp_doc*/
  1404. (traverseproc) __Pyx_Coroutine_traverse, /*tp_traverse*/
  1405. 0, /*tp_clear*/
  1406. #if CYTHON_USE_ASYNC_SLOTS && CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3 && PY_VERSION_HEX < 0x030500B1
  1407. // in order to (mis-)use tp_reserved above, we must also implement tp_richcompare
  1408. __Pyx_Coroutine_compare, /*tp_richcompare*/
  1409. #else
  1410. 0, /*tp_richcompare*/
  1411. #endif
  1412. offsetof(__pyx_CoroutineObject, gi_weakreflist), /*tp_weaklistoffset*/
  1413. // no tp_iter() as iterator is only available through __await__()
  1414. 0, /*tp_iter*/
  1415. 0, /*tp_iternext*/
  1416. __pyx_Coroutine_methods, /*tp_methods*/
  1417. __pyx_Coroutine_memberlist, /*tp_members*/
  1418. __pyx_Coroutine_getsets, /*tp_getset*/
  1419. 0, /*tp_base*/
  1420. 0, /*tp_dict*/
  1421. 0, /*tp_descr_get*/
  1422. 0, /*tp_descr_set*/
  1423. 0, /*tp_dictoffset*/
  1424. 0, /*tp_init*/
  1425. 0, /*tp_alloc*/
  1426. 0, /*tp_new*/
  1427. 0, /*tp_free*/
  1428. 0, /*tp_is_gc*/
  1429. 0, /*tp_bases*/
  1430. 0, /*tp_mro*/
  1431. 0, /*tp_cache*/
  1432. 0, /*tp_subclasses*/
  1433. 0, /*tp_weaklist*/
  1434. #if CYTHON_USE_TP_FINALIZE
  1435. 0, /*tp_del*/
  1436. #else
  1437. __Pyx_Coroutine_del, /*tp_del*/
  1438. #endif
  1439. 0, /*tp_version_tag*/
  1440. #if CYTHON_USE_TP_FINALIZE
  1441. __Pyx_Coroutine_del, /*tp_finalize*/
  1442. #elif PY_VERSION_HEX >= 0x030400a1
  1443. 0, /*tp_finalize*/
  1444. #endif
  1445. };
  1446. static int __pyx_Coroutine_init(void) {
  1447. // on Windows, C-API functions can't be used in slots statically
  1448. __pyx_CoroutineType_type.tp_getattro = __Pyx_PyObject_GenericGetAttrNoDict;
  1449. __pyx_CoroutineType = __Pyx_FetchCommonType(&__pyx_CoroutineType_type);
  1450. if (unlikely(!__pyx_CoroutineType))
  1451. return -1;
  1452. #ifdef __Pyx_IterableCoroutine_USED
  1453. if (unlikely(__pyx_IterableCoroutine_init() == -1))
  1454. return -1;
  1455. #endif
  1456. __pyx_CoroutineAwaitType = __Pyx_FetchCommonType(&__pyx_CoroutineAwaitType_type);
  1457. if (unlikely(!__pyx_CoroutineAwaitType))
  1458. return -1;
  1459. return 0;
  1460. }
  1461. //////////////////// IterableCoroutine.proto ////////////////////
  1462. #define __Pyx_IterableCoroutine_USED
  1463. static PyTypeObject *__pyx_IterableCoroutineType = 0;
  1464. #undef __Pyx_Coroutine_Check
  1465. #define __Pyx_Coroutine_Check(obj) (__Pyx_Coroutine_CheckExact(obj) || (Py_TYPE(obj) == __pyx_IterableCoroutineType))
  1466. #define __Pyx_IterableCoroutine_New(body, code, closure, name, qualname, module_name) \
  1467. __Pyx__Coroutine_New(__pyx_IterableCoroutineType, body, code, closure, name, qualname, module_name)
  1468. static int __pyx_IterableCoroutine_init(void);/*proto*/
  1469. //////////////////// IterableCoroutine ////////////////////
  1470. //@requires: Coroutine
  1471. //@requires: CommonStructures.c::FetchCommonType
  1472. static PyTypeObject __pyx_IterableCoroutineType_type = {
  1473. PyVarObject_HEAD_INIT(0, 0)
  1474. "iterable_coroutine", /*tp_name*/
  1475. sizeof(__pyx_CoroutineObject), /*tp_basicsize*/
  1476. 0, /*tp_itemsize*/
  1477. (destructor) __Pyx_Coroutine_dealloc,/*tp_dealloc*/
  1478. 0, /*tp_print*/
  1479. 0, /*tp_getattr*/
  1480. 0, /*tp_setattr*/
  1481. #if CYTHON_USE_ASYNC_SLOTS
  1482. &__pyx_Coroutine_as_async, /*tp_as_async (tp_reserved) - Py3 only! */
  1483. #else
  1484. 0, /*tp_reserved*/
  1485. #endif
  1486. 0, /*tp_repr*/
  1487. 0, /*tp_as_number*/
  1488. 0, /*tp_as_sequence*/
  1489. 0, /*tp_as_mapping*/
  1490. 0, /*tp_hash*/
  1491. 0, /*tp_call*/
  1492. 0, /*tp_str*/
  1493. 0, /*tp_getattro*/
  1494. 0, /*tp_setattro*/
  1495. 0, /*tp_as_buffer*/
  1496. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/
  1497. 0, /*tp_doc*/
  1498. (traverseproc) __Pyx_Coroutine_traverse, /*tp_traverse*/
  1499. 0, /*tp_clear*/
  1500. #if CYTHON_USE_ASYNC_SLOTS && CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3 && PY_VERSION_HEX < 0x030500B1
  1501. // in order to (mis-)use tp_reserved above, we must also implement tp_richcompare
  1502. __Pyx_Coroutine_compare, /*tp_richcompare*/
  1503. #else
  1504. 0, /*tp_richcompare*/
  1505. #endif
  1506. offsetof(__pyx_CoroutineObject, gi_weakreflist), /*tp_weaklistoffset*/
  1507. // enable iteration for legacy support of asyncio yield-from protocol
  1508. __Pyx_Coroutine_await, /*tp_iter*/
  1509. (iternextfunc) __Pyx_Generator_Next, /*tp_iternext*/
  1510. __pyx_Coroutine_methods, /*tp_methods*/
  1511. __pyx_Coroutine_memberlist, /*tp_members*/
  1512. __pyx_Coroutine_getsets, /*tp_getset*/
  1513. 0, /*tp_base*/
  1514. 0, /*tp_dict*/
  1515. 0, /*tp_descr_get*/
  1516. 0, /*tp_descr_set*/
  1517. 0, /*tp_dictoffset*/
  1518. 0, /*tp_init*/
  1519. 0, /*tp_alloc*/
  1520. 0, /*tp_new*/
  1521. 0, /*tp_free*/
  1522. 0, /*tp_is_gc*/
  1523. 0, /*tp_bases*/
  1524. 0, /*tp_mro*/
  1525. 0, /*tp_cache*/
  1526. 0, /*tp_subclasses*/
  1527. 0, /*tp_weaklist*/
  1528. #if PY_VERSION_HEX >= 0x030400a1
  1529. 0, /*tp_del*/
  1530. #else
  1531. __Pyx_Coroutine_del, /*tp_del*/
  1532. #endif
  1533. 0, /*tp_version_tag*/
  1534. #if PY_VERSION_HEX >= 0x030400a1
  1535. __Pyx_Coroutine_del, /*tp_finalize*/
  1536. #endif
  1537. };
  1538. static int __pyx_IterableCoroutine_init(void) {
  1539. __pyx_IterableCoroutineType_type.tp_getattro = __Pyx_PyObject_GenericGetAttrNoDict;
  1540. __pyx_IterableCoroutineType = __Pyx_FetchCommonType(&__pyx_IterableCoroutineType_type);
  1541. if (unlikely(!__pyx_IterableCoroutineType))
  1542. return -1;
  1543. return 0;
  1544. }
  1545. //////////////////// Generator ////////////////////
  1546. //@requires: CoroutineBase
  1547. //@requires: PatchGeneratorABC
  1548. //@requires: ObjectHandling.c::PyObject_GenericGetAttrNoDict
  1549. static PyMethodDef __pyx_Generator_methods[] = {
  1550. {"send", (PyCFunction) __Pyx_Coroutine_Send, METH_O,
  1551. (char*) PyDoc_STR("send(arg) -> send 'arg' into generator,\nreturn next yielded value or raise StopIteration.")},
  1552. {"throw", (PyCFunction) __Pyx_Coroutine_Throw, METH_VARARGS,
  1553. (char*) PyDoc_STR("throw(typ[,val[,tb]]) -> raise exception in generator,\nreturn next yielded value or raise StopIteration.")},
  1554. {"close", (PyCFunction) __Pyx_Coroutine_Close, METH_NOARGS,
  1555. (char*) PyDoc_STR("close() -> raise GeneratorExit inside generator.")},
  1556. {0, 0, 0, 0}
  1557. };
  1558. static PyMemberDef __pyx_Generator_memberlist[] = {
  1559. {(char *) "gi_running", T_BOOL, offsetof(__pyx_CoroutineObject, is_running), READONLY, NULL},
  1560. {(char*) "gi_yieldfrom", T_OBJECT, offsetof(__pyx_CoroutineObject, yieldfrom), READONLY,
  1561. (char*) PyDoc_STR("object being iterated by 'yield from', or None")},
  1562. {(char*) "gi_code", T_OBJECT, offsetof(__pyx_CoroutineObject, gi_code), READONLY, NULL},
  1563. {0, 0, 0, 0, 0}
  1564. };
  1565. static PyGetSetDef __pyx_Generator_getsets[] = {
  1566. {(char *) "__name__", (getter)__Pyx_Coroutine_get_name, (setter)__Pyx_Coroutine_set_name,
  1567. (char*) PyDoc_STR("name of the generator"), 0},
  1568. {(char *) "__qualname__", (getter)__Pyx_Coroutine_get_qualname, (setter)__Pyx_Coroutine_set_qualname,
  1569. (char*) PyDoc_STR("qualified name of the generator"), 0},
  1570. {0, 0, 0, 0, 0}
  1571. };
  1572. static PyTypeObject __pyx_GeneratorType_type = {
  1573. PyVarObject_HEAD_INIT(0, 0)
  1574. "generator", /*tp_name*/
  1575. sizeof(__pyx_CoroutineObject), /*tp_basicsize*/
  1576. 0, /*tp_itemsize*/
  1577. (destructor) __Pyx_Coroutine_dealloc,/*tp_dealloc*/
  1578. 0, /*tp_print*/
  1579. 0, /*tp_getattr*/
  1580. 0, /*tp_setattr*/
  1581. 0, /*tp_compare / tp_as_async*/
  1582. 0, /*tp_repr*/
  1583. 0, /*tp_as_number*/
  1584. 0, /*tp_as_sequence*/
  1585. 0, /*tp_as_mapping*/
  1586. 0, /*tp_hash*/
  1587. 0, /*tp_call*/
  1588. 0, /*tp_str*/
  1589. 0, /*tp_getattro*/
  1590. 0, /*tp_setattro*/
  1591. 0, /*tp_as_buffer*/
  1592. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/
  1593. 0, /*tp_doc*/
  1594. (traverseproc) __Pyx_Coroutine_traverse, /*tp_traverse*/
  1595. 0, /*tp_clear*/
  1596. 0, /*tp_richcompare*/
  1597. offsetof(__pyx_CoroutineObject, gi_weakreflist), /*tp_weaklistoffset*/
  1598. 0, /*tp_iter*/
  1599. (iternextfunc) __Pyx_Generator_Next, /*tp_iternext*/
  1600. __pyx_Generator_methods, /*tp_methods*/
  1601. __pyx_Generator_memberlist, /*tp_members*/
  1602. __pyx_Generator_getsets, /*tp_getset*/
  1603. 0, /*tp_base*/
  1604. 0, /*tp_dict*/
  1605. 0, /*tp_descr_get*/
  1606. 0, /*tp_descr_set*/
  1607. 0, /*tp_dictoffset*/
  1608. 0, /*tp_init*/
  1609. 0, /*tp_alloc*/
  1610. 0, /*tp_new*/
  1611. 0, /*tp_free*/
  1612. 0, /*tp_is_gc*/
  1613. 0, /*tp_bases*/
  1614. 0, /*tp_mro*/
  1615. 0, /*tp_cache*/
  1616. 0, /*tp_subclasses*/
  1617. 0, /*tp_weaklist*/
  1618. #if CYTHON_USE_TP_FINALIZE
  1619. 0, /*tp_del*/
  1620. #else
  1621. __Pyx_Coroutine_del, /*tp_del*/
  1622. #endif
  1623. 0, /*tp_version_tag*/
  1624. #if CYTHON_USE_TP_FINALIZE
  1625. __Pyx_Coroutine_del, /*tp_finalize*/
  1626. #elif PY_VERSION_HEX >= 0x030400a1
  1627. 0, /*tp_finalize*/
  1628. #endif
  1629. };
  1630. static int __pyx_Generator_init(void) {
  1631. // on Windows, C-API functions can't be used in slots statically
  1632. __pyx_GeneratorType_type.tp_getattro = __Pyx_PyObject_GenericGetAttrNoDict;
  1633. __pyx_GeneratorType_type.tp_iter = PyObject_SelfIter;
  1634. __pyx_GeneratorType = __Pyx_FetchCommonType(&__pyx_GeneratorType_type);
  1635. if (unlikely(!__pyx_GeneratorType)) {
  1636. return -1;
  1637. }
  1638. return 0;
  1639. }
  1640. /////////////// ReturnWithStopIteration.proto ///////////////
  1641. #define __Pyx_ReturnWithStopIteration(value) \
  1642. if (value == Py_None) PyErr_SetNone(PyExc_StopIteration); else __Pyx__ReturnWithStopIteration(value)
  1643. static void __Pyx__ReturnWithStopIteration(PyObject* value); /*proto*/
  1644. /////////////// ReturnWithStopIteration ///////////////
  1645. //@requires: Exceptions.c::PyErrFetchRestore
  1646. //@requires: Exceptions.c::PyThreadStateGet
  1647. //@substitute: naming
  1648. // 1) Instantiating an exception just to pass back a value is costly.
  1649. // 2) CPython 3.3 <= x < 3.5b1 crash in yield-from when the StopIteration is not instantiated.
  1650. // 3) Passing a tuple as value into PyErr_SetObject() passes its items on as arguments.
  1651. // 4) Passing an exception as value will interpret it as an exception on unpacking and raise it (or unpack its value).
  1652. // 5) If there is currently an exception being handled, we need to chain it.
  1653. static void __Pyx__ReturnWithStopIteration(PyObject* value) {
  1654. PyObject *exc, *args;
  1655. #if CYTHON_COMPILING_IN_CPYTHON || CYTHON_COMPILING_IN_PYSTON
  1656. __Pyx_PyThreadState_declare
  1657. if ((PY_VERSION_HEX >= 0x03030000 && PY_VERSION_HEX < 0x030500B1)
  1658. || unlikely(PyTuple_Check(value) || PyExceptionInstance_Check(value))) {
  1659. args = PyTuple_New(1);
  1660. if (unlikely(!args)) return;
  1661. Py_INCREF(value);
  1662. PyTuple_SET_ITEM(args, 0, value);
  1663. exc = PyType_Type.tp_call(PyExc_StopIteration, args, NULL);
  1664. Py_DECREF(args);
  1665. if (!exc) return;
  1666. } else {
  1667. // it's safe to avoid instantiating the exception
  1668. Py_INCREF(value);
  1669. exc = value;
  1670. }
  1671. #if CYTHON_FAST_THREAD_STATE
  1672. __Pyx_PyThreadState_assign
  1673. #if PY_VERSION_HEX >= 0x030700A2
  1674. if (!$local_tstate_cname->exc_state.exc_type)
  1675. #else
  1676. if (!$local_tstate_cname->exc_type)
  1677. #endif
  1678. {
  1679. // no chaining needed => avoid the overhead in PyErr_SetObject()
  1680. Py_INCREF(PyExc_StopIteration);
  1681. __Pyx_ErrRestore(PyExc_StopIteration, exc, NULL);
  1682. return;
  1683. }
  1684. #endif
  1685. #else
  1686. args = PyTuple_Pack(1, value);
  1687. if (unlikely(!args)) return;
  1688. exc = PyObject_Call(PyExc_StopIteration, args, NULL);
  1689. Py_DECREF(args);
  1690. if (unlikely(!exc)) return;
  1691. #endif
  1692. PyErr_SetObject(PyExc_StopIteration, exc);
  1693. Py_DECREF(exc);
  1694. }
  1695. //////////////////// PatchModuleWithCoroutine.proto ////////////////////
  1696. static PyObject* __Pyx_Coroutine_patch_module(PyObject* module, const char* py_code); /*proto*/
  1697. //////////////////// PatchModuleWithCoroutine ////////////////////
  1698. //@substitute: naming
  1699. static PyObject* __Pyx_Coroutine_patch_module(PyObject* module, const char* py_code) {
  1700. #if defined(__Pyx_Generator_USED) || defined(__Pyx_Coroutine_USED)
  1701. int result;
  1702. PyObject *globals, *result_obj;
  1703. globals = PyDict_New(); if (unlikely(!globals)) goto ignore;
  1704. result = PyDict_SetItemString(globals, "_cython_coroutine_type",
  1705. #ifdef __Pyx_Coroutine_USED
  1706. (PyObject*)__pyx_CoroutineType);
  1707. #else
  1708. Py_None);
  1709. #endif
  1710. if (unlikely(result < 0)) goto ignore;
  1711. result = PyDict_SetItemString(globals, "_cython_generator_type",
  1712. #ifdef __Pyx_Generator_USED
  1713. (PyObject*)__pyx_GeneratorType);
  1714. #else
  1715. Py_None);
  1716. #endif
  1717. if (unlikely(result < 0)) goto ignore;
  1718. if (unlikely(PyDict_SetItemString(globals, "_module", module) < 0)) goto ignore;
  1719. if (unlikely(PyDict_SetItemString(globals, "__builtins__", $builtins_cname) < 0)) goto ignore;
  1720. result_obj = PyRun_String(py_code, Py_file_input, globals, globals);
  1721. if (unlikely(!result_obj)) goto ignore;
  1722. Py_DECREF(result_obj);
  1723. Py_DECREF(globals);
  1724. return module;
  1725. ignore:
  1726. Py_XDECREF(globals);
  1727. PyErr_WriteUnraisable(module);
  1728. if (unlikely(PyErr_WarnEx(PyExc_RuntimeWarning, "Cython module failed to patch module with custom type", 1) < 0)) {
  1729. Py_DECREF(module);
  1730. module = NULL;
  1731. }
  1732. #else
  1733. // avoid "unused" warning
  1734. py_code++;
  1735. #endif
  1736. return module;
  1737. }
  1738. //////////////////// PatchGeneratorABC.proto ////////////////////
  1739. // register with Generator/Coroutine ABCs in 'collections.abc'
  1740. // see https://bugs.python.org/issue24018
  1741. static int __Pyx_patch_abc(void); /*proto*/
  1742. //////////////////// PatchGeneratorABC ////////////////////
  1743. //@requires: PatchModuleWithCoroutine
  1744. #ifndef CYTHON_REGISTER_ABCS
  1745. #define CYTHON_REGISTER_ABCS 1
  1746. #endif
  1747. #if defined(__Pyx_Generator_USED) || defined(__Pyx_Coroutine_USED)
  1748. static PyObject* __Pyx_patch_abc_module(PyObject *module); /*proto*/
  1749. static PyObject* __Pyx_patch_abc_module(PyObject *module) {
  1750. module = __Pyx_Coroutine_patch_module(
  1751. module, CSTRING("""\
  1752. if _cython_generator_type is not None:
  1753. try: Generator = _module.Generator
  1754. except AttributeError: pass
  1755. else: Generator.register(_cython_generator_type)
  1756. if _cython_coroutine_type is not None:
  1757. try: Coroutine = _module.Coroutine
  1758. except AttributeError: pass
  1759. else: Coroutine.register(_cython_coroutine_type)
  1760. """)
  1761. );
  1762. return module;
  1763. }
  1764. #endif
  1765. static int __Pyx_patch_abc(void) {
  1766. #if defined(__Pyx_Generator_USED) || defined(__Pyx_Coroutine_USED)
  1767. static int abc_patched = 0;
  1768. if (CYTHON_REGISTER_ABCS && !abc_patched) {
  1769. PyObject *module;
  1770. module = PyImport_ImportModule((PY_MAJOR_VERSION >= 3) ? "collections.abc" : "collections");
  1771. if (!module) {
  1772. PyErr_WriteUnraisable(NULL);
  1773. if (unlikely(PyErr_WarnEx(PyExc_RuntimeWarning,
  1774. ((PY_MAJOR_VERSION >= 3) ?
  1775. "Cython module failed to register with collections.abc module" :
  1776. "Cython module failed to register with collections module"), 1) < 0)) {
  1777. return -1;
  1778. }
  1779. } else {
  1780. module = __Pyx_patch_abc_module(module);
  1781. abc_patched = 1;
  1782. if (unlikely(!module))
  1783. return -1;
  1784. Py_DECREF(module);
  1785. }
  1786. // also register with "backports_abc" module if available, just in case
  1787. module = PyImport_ImportModule("backports_abc");
  1788. if (module) {
  1789. module = __Pyx_patch_abc_module(module);
  1790. Py_XDECREF(module);
  1791. }
  1792. if (!module) {
  1793. PyErr_Clear();
  1794. }
  1795. }
  1796. #else
  1797. // avoid "unused" warning for __Pyx_Coroutine_patch_module()
  1798. if ((0)) __Pyx_Coroutine_patch_module(NULL, NULL);
  1799. #endif
  1800. return 0;
  1801. }
  1802. //////////////////// PatchAsyncIO.proto ////////////////////
  1803. // run after importing "asyncio" to patch Cython generator support into it
  1804. static PyObject* __Pyx_patch_asyncio(PyObject* module); /*proto*/
  1805. //////////////////// PatchAsyncIO ////////////////////
  1806. //@requires: ImportExport.c::Import
  1807. //@requires: PatchModuleWithCoroutine
  1808. //@requires: PatchInspect
  1809. static PyObject* __Pyx_patch_asyncio(PyObject* module) {
  1810. #if PY_VERSION_HEX < 0x030500B2 && \
  1811. (defined(__Pyx_Coroutine_USED) || defined(__Pyx_Generator_USED)) && \
  1812. (!defined(CYTHON_PATCH_ASYNCIO) || CYTHON_PATCH_ASYNCIO)
  1813. PyObject *patch_module = NULL;
  1814. static int asyncio_patched = 0;
  1815. if (unlikely((!asyncio_patched) && module)) {
  1816. PyObject *package;
  1817. package = __Pyx_Import(PYIDENT("asyncio.coroutines"), NULL, 0);
  1818. if (package) {
  1819. patch_module = __Pyx_Coroutine_patch_module(
  1820. PyObject_GetAttrString(package, "coroutines"), CSTRING("""\
  1821. try:
  1822. coro_types = _module._COROUTINE_TYPES
  1823. except AttributeError: pass
  1824. else:
  1825. if _cython_coroutine_type is not None and _cython_coroutine_type not in coro_types:
  1826. coro_types = tuple(coro_types) + (_cython_coroutine_type,)
  1827. if _cython_generator_type is not None and _cython_generator_type not in coro_types:
  1828. coro_types = tuple(coro_types) + (_cython_generator_type,)
  1829. _module._COROUTINE_TYPES = coro_types
  1830. """)
  1831. );
  1832. } else {
  1833. PyErr_Clear();
  1834. // Always enable fallback: even if we compile against 3.4.2, we might be running on 3.4.1 at some point.
  1835. //#if PY_VERSION_HEX < 0x03040200
  1836. // Py3.4.1 used to have asyncio.tasks instead of asyncio.coroutines
  1837. package = __Pyx_Import(PYIDENT("asyncio.tasks"), NULL, 0);
  1838. if (unlikely(!package)) goto asyncio_done;
  1839. patch_module = __Pyx_Coroutine_patch_module(
  1840. PyObject_GetAttrString(package, "tasks"), CSTRING("""\
  1841. if hasattr(_module, 'iscoroutine'):
  1842. old_types = getattr(_module.iscoroutine, '_cython_coroutine_types', None)
  1843. if old_types is None or not isinstance(old_types, set):
  1844. old_types = set()
  1845. def cy_wrap(orig_func, type=type, cython_coroutine_types=old_types):
  1846. def cy_iscoroutine(obj): return type(obj) in cython_coroutine_types or orig_func(obj)
  1847. cy_iscoroutine._cython_coroutine_types = cython_coroutine_types
  1848. return cy_iscoroutine
  1849. _module.iscoroutine = cy_wrap(_module.iscoroutine)
  1850. if _cython_coroutine_type is not None:
  1851. old_types.add(_cython_coroutine_type)
  1852. if _cython_generator_type is not None:
  1853. old_types.add(_cython_generator_type)
  1854. """)
  1855. );
  1856. //#endif
  1857. // Py < 0x03040200
  1858. }
  1859. Py_DECREF(package);
  1860. if (unlikely(!patch_module)) goto ignore;
  1861. //#if PY_VERSION_HEX < 0x03040200
  1862. asyncio_done:
  1863. PyErr_Clear();
  1864. //#endif
  1865. asyncio_patched = 1;
  1866. #ifdef __Pyx_Generator_USED
  1867. // now patch inspect.isgenerator() by looking up the imported module in the patched asyncio module
  1868. {
  1869. PyObject *inspect_module;
  1870. if (patch_module) {
  1871. inspect_module = PyObject_GetAttr(patch_module, PYIDENT("inspect"));
  1872. Py_DECREF(patch_module);
  1873. } else {
  1874. inspect_module = __Pyx_Import(PYIDENT("inspect"), NULL, 0);
  1875. }
  1876. if (unlikely(!inspect_module)) goto ignore;
  1877. inspect_module = __Pyx_patch_inspect(inspect_module);
  1878. if (unlikely(!inspect_module)) {
  1879. Py_DECREF(module);
  1880. module = NULL;
  1881. }
  1882. Py_XDECREF(inspect_module);
  1883. }
  1884. #else
  1885. // avoid "unused" warning for __Pyx_patch_inspect()
  1886. if ((0)) return __Pyx_patch_inspect(module);
  1887. #endif
  1888. }
  1889. return module;
  1890. ignore:
  1891. PyErr_WriteUnraisable(module);
  1892. if (unlikely(PyErr_WarnEx(PyExc_RuntimeWarning, "Cython module failed to patch asyncio package with custom generator type", 1) < 0)) {
  1893. Py_DECREF(module);
  1894. module = NULL;
  1895. }
  1896. #else
  1897. // avoid "unused" warning for __Pyx_Coroutine_patch_module()
  1898. if ((0)) return __Pyx_patch_inspect(__Pyx_Coroutine_patch_module(module, NULL));
  1899. #endif
  1900. return module;
  1901. }
  1902. //////////////////// PatchInspect.proto ////////////////////
  1903. // run after importing "inspect" to patch Cython generator support into it
  1904. static PyObject* __Pyx_patch_inspect(PyObject* module); /*proto*/
  1905. //////////////////// PatchInspect ////////////////////
  1906. //@requires: PatchModuleWithCoroutine
  1907. static PyObject* __Pyx_patch_inspect(PyObject* module) {
  1908. #if defined(__Pyx_Generator_USED) && (!defined(CYTHON_PATCH_INSPECT) || CYTHON_PATCH_INSPECT)
  1909. static int inspect_patched = 0;
  1910. if (unlikely((!inspect_patched) && module)) {
  1911. module = __Pyx_Coroutine_patch_module(
  1912. module, CSTRING("""\
  1913. old_types = getattr(_module.isgenerator, '_cython_generator_types', None)
  1914. if old_types is None or not isinstance(old_types, set):
  1915. old_types = set()
  1916. def cy_wrap(orig_func, type=type, cython_generator_types=old_types):
  1917. def cy_isgenerator(obj): return type(obj) in cython_generator_types or orig_func(obj)
  1918. cy_isgenerator._cython_generator_types = cython_generator_types
  1919. return cy_isgenerator
  1920. _module.isgenerator = cy_wrap(_module.isgenerator)
  1921. old_types.add(_cython_generator_type)
  1922. """)
  1923. );
  1924. inspect_patched = 1;
  1925. }
  1926. #else
  1927. // avoid "unused" warning for __Pyx_Coroutine_patch_module()
  1928. if ((0)) return __Pyx_Coroutine_patch_module(module, NULL);
  1929. #endif
  1930. return module;
  1931. }
  1932. //////////////////// StopAsyncIteration.proto ////////////////////
  1933. #define __Pyx_StopAsyncIteration_USED
  1934. static PyObject *__Pyx_PyExc_StopAsyncIteration;
  1935. static int __pyx_StopAsyncIteration_init(void); /*proto*/
  1936. //////////////////// StopAsyncIteration ////////////////////
  1937. #if PY_VERSION_HEX < 0x030500B1
  1938. static PyTypeObject __Pyx__PyExc_StopAsyncIteration_type = {
  1939. PyVarObject_HEAD_INIT(0, 0)
  1940. "StopAsyncIteration", /*tp_name*/
  1941. sizeof(PyBaseExceptionObject), /*tp_basicsize*/
  1942. 0, /*tp_itemsize*/
  1943. 0, /*tp_dealloc*/
  1944. 0, /*tp_print*/
  1945. 0, /*tp_getattr*/
  1946. 0, /*tp_setattr*/
  1947. 0, /*tp_compare / reserved*/
  1948. 0, /*tp_repr*/
  1949. 0, /*tp_as_number*/
  1950. 0, /*tp_as_sequence*/
  1951. 0, /*tp_as_mapping*/
  1952. 0, /*tp_hash*/
  1953. 0, /*tp_call*/
  1954. 0, /*tp_str*/
  1955. 0, /*tp_getattro*/
  1956. 0, /*tp_setattro*/
  1957. 0, /*tp_as_buffer*/
  1958. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
  1959. PyDoc_STR("Signal the end from iterator.__anext__()."), /*tp_doc*/
  1960. 0, /*tp_traverse*/
  1961. 0, /*tp_clear*/
  1962. 0, /*tp_richcompare*/
  1963. 0, /*tp_weaklistoffset*/
  1964. 0, /*tp_iter*/
  1965. 0, /*tp_iternext*/
  1966. 0, /*tp_methods*/
  1967. 0, /*tp_members*/
  1968. 0, /*tp_getset*/
  1969. 0, /*tp_base*/
  1970. 0, /*tp_dict*/
  1971. 0, /*tp_descr_get*/
  1972. 0, /*tp_descr_set*/
  1973. 0, /*tp_dictoffset*/
  1974. 0, /*tp_init*/
  1975. 0, /*tp_alloc*/
  1976. 0, /*tp_new*/
  1977. 0, /*tp_free*/
  1978. 0, /*tp_is_gc*/
  1979. 0, /*tp_bases*/
  1980. 0, /*tp_mro*/
  1981. 0, /*tp_cache*/
  1982. 0, /*tp_subclasses*/
  1983. 0, /*tp_weaklist*/
  1984. 0, /*tp_del*/
  1985. 0, /*tp_version_tag*/
  1986. #if PY_VERSION_HEX >= 0x030400a1
  1987. 0, /*tp_finalize*/
  1988. #endif
  1989. };
  1990. #endif
  1991. static int __pyx_StopAsyncIteration_init(void) {
  1992. #if PY_VERSION_HEX >= 0x030500B1
  1993. __Pyx_PyExc_StopAsyncIteration = PyExc_StopAsyncIteration;
  1994. #else
  1995. PyObject *builtins = PyEval_GetBuiltins();
  1996. if (likely(builtins)) {
  1997. PyObject *exc = PyMapping_GetItemString(builtins, (char*) "StopAsyncIteration");
  1998. if (exc) {
  1999. __Pyx_PyExc_StopAsyncIteration = exc;
  2000. return 0;
  2001. }
  2002. }
  2003. PyErr_Clear();
  2004. __Pyx__PyExc_StopAsyncIteration_type.tp_traverse = ((PyTypeObject*)PyExc_BaseException)->tp_traverse;
  2005. __Pyx__PyExc_StopAsyncIteration_type.tp_clear = ((PyTypeObject*)PyExc_BaseException)->tp_clear;
  2006. __Pyx__PyExc_StopAsyncIteration_type.tp_dictoffset = ((PyTypeObject*)PyExc_BaseException)->tp_dictoffset;
  2007. __Pyx__PyExc_StopAsyncIteration_type.tp_base = (PyTypeObject*)PyExc_Exception;
  2008. __Pyx_PyExc_StopAsyncIteration = (PyObject*) __Pyx_FetchCommonType(&__Pyx__PyExc_StopAsyncIteration_type);
  2009. if (unlikely(!__Pyx_PyExc_StopAsyncIteration))
  2010. return -1;
  2011. if (builtins && unlikely(PyMapping_SetItemString(builtins, (char*) "StopAsyncIteration", __Pyx_PyExc_StopAsyncIteration) < 0))
  2012. return -1;
  2013. #endif
  2014. return 0;
  2015. }