Coroutine.c 88 KB

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