_asynciomodule.c 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840
  1. #ifndef Py_BUILD_CORE_BUILTIN
  2. # define Py_BUILD_CORE_MODULE 1
  3. #endif
  4. #include "Python.h"
  5. #include "pycore_pyerrors.h" // _PyErr_ClearExcState()
  6. #include "pycore_pystate.h" // _PyThreadState_GET()
  7. #include "pycore_runtime_init.h" // _Py_ID()
  8. #include "pycore_moduleobject.h" // _PyModule_GetState()
  9. #include "structmember.h" // PyMemberDef
  10. #include <stddef.h> // offsetof()
  11. /*[clinic input]
  12. module _asyncio
  13. [clinic start generated code]*/
  14. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=8fd17862aa989c69]*/
  15. #define FI_FREELIST_MAXLEN 255
  16. typedef struct futureiterobject futureiterobject;
  17. /* State of the _asyncio module */
  18. typedef struct {
  19. PyTypeObject *FutureIterType;
  20. PyTypeObject *TaskStepMethWrapper_Type;
  21. PyTypeObject *FutureType;
  22. PyTypeObject *TaskType;
  23. PyObject *asyncio_mod;
  24. PyObject *context_kwname;
  25. /* Dictionary containing tasks that are currently active in
  26. all running event loops. {EventLoop: Task} */
  27. PyObject *current_tasks;
  28. /* WeakSet containing all tasks scheduled to run on event loops. */
  29. PyObject *scheduled_tasks;
  30. /* Set containing all eagerly executing tasks. */
  31. PyObject *eager_tasks;
  32. /* An isinstance type cache for the 'is_coroutine()' function. */
  33. PyObject *iscoroutine_typecache;
  34. /* Imports from asyncio.events. */
  35. PyObject *asyncio_get_event_loop_policy;
  36. /* Imports from asyncio.base_futures. */
  37. PyObject *asyncio_future_repr_func;
  38. /* Imports from asyncio.exceptions. */
  39. PyObject *asyncio_CancelledError;
  40. PyObject *asyncio_InvalidStateError;
  41. /* Imports from asyncio.base_tasks. */
  42. PyObject *asyncio_task_get_stack_func;
  43. PyObject *asyncio_task_print_stack_func;
  44. PyObject *asyncio_task_repr_func;
  45. /* Imports from asyncio.coroutines. */
  46. PyObject *asyncio_iscoroutine_func;
  47. /* Imports from traceback. */
  48. PyObject *traceback_extract_stack;
  49. PyObject *cached_running_loop; // Borrowed reference
  50. volatile uint64_t cached_running_loop_tsid;
  51. /* Counter for autogenerated Task names */
  52. uint64_t task_name_counter;
  53. futureiterobject *fi_freelist;
  54. Py_ssize_t fi_freelist_len;
  55. } asyncio_state;
  56. static inline asyncio_state *
  57. get_asyncio_state(PyObject *mod)
  58. {
  59. asyncio_state *state = _PyModule_GetState(mod);
  60. assert(state != NULL);
  61. return state;
  62. }
  63. static inline asyncio_state *
  64. get_asyncio_state_by_cls(PyTypeObject *cls)
  65. {
  66. asyncio_state *state = (asyncio_state *)_PyType_GetModuleState(cls);
  67. assert(state != NULL);
  68. return state;
  69. }
  70. static struct PyModuleDef _asynciomodule;
  71. static inline asyncio_state *
  72. get_asyncio_state_by_def(PyObject *self)
  73. {
  74. PyTypeObject *tp = Py_TYPE(self);
  75. PyObject *mod = PyType_GetModuleByDef(tp, &_asynciomodule);
  76. assert(mod != NULL);
  77. return get_asyncio_state(mod);
  78. }
  79. typedef enum {
  80. STATE_PENDING,
  81. STATE_CANCELLED,
  82. STATE_FINISHED
  83. } fut_state;
  84. #define FutureObj_HEAD(prefix) \
  85. PyObject_HEAD \
  86. PyObject *prefix##_loop; \
  87. PyObject *prefix##_callback0; \
  88. PyObject *prefix##_context0; \
  89. PyObject *prefix##_callbacks; \
  90. PyObject *prefix##_exception; \
  91. PyObject *prefix##_exception_tb; \
  92. PyObject *prefix##_result; \
  93. PyObject *prefix##_source_tb; \
  94. PyObject *prefix##_cancel_msg; \
  95. fut_state prefix##_state; \
  96. int prefix##_log_tb; \
  97. int prefix##_blocking; \
  98. PyObject *dict; \
  99. PyObject *prefix##_weakreflist; \
  100. PyObject *prefix##_cancelled_exc;
  101. typedef struct {
  102. FutureObj_HEAD(fut)
  103. } FutureObj;
  104. typedef struct {
  105. FutureObj_HEAD(task)
  106. PyObject *task_fut_waiter;
  107. PyObject *task_coro;
  108. PyObject *task_name;
  109. PyObject *task_context;
  110. int task_must_cancel;
  111. int task_log_destroy_pending;
  112. int task_num_cancels_requested;
  113. } TaskObj;
  114. typedef struct {
  115. PyObject_HEAD
  116. TaskObj *sw_task;
  117. PyObject *sw_arg;
  118. } TaskStepMethWrapper;
  119. #define Future_CheckExact(state, obj) Py_IS_TYPE(obj, state->FutureType)
  120. #define Task_CheckExact(state, obj) Py_IS_TYPE(obj, state->TaskType)
  121. #define Future_Check(state, obj) PyObject_TypeCheck(obj, state->FutureType)
  122. #define Task_Check(state, obj) PyObject_TypeCheck(obj, state->TaskType)
  123. #include "clinic/_asynciomodule.c.h"
  124. /*[clinic input]
  125. class _asyncio.Future "FutureObj *" "&Future_Type"
  126. [clinic start generated code]*/
  127. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=00d3e4abca711e0f]*/
  128. /* Get FutureIter from Future */
  129. static PyObject * future_new_iter(PyObject *);
  130. static PyObject *
  131. task_step_handle_result_impl(asyncio_state *state, TaskObj *task, PyObject *result);
  132. static int
  133. _is_coroutine(asyncio_state *state, PyObject *coro)
  134. {
  135. /* 'coro' is not a native coroutine, call asyncio.iscoroutine()
  136. to check if it's another coroutine flavour.
  137. Do this check after 'future_init()'; in case we need to raise
  138. an error, __del__ needs a properly initialized object.
  139. */
  140. PyObject *res = PyObject_CallOneArg(state->asyncio_iscoroutine_func, coro);
  141. if (res == NULL) {
  142. return -1;
  143. }
  144. int is_res_true = PyObject_IsTrue(res);
  145. Py_DECREF(res);
  146. if (is_res_true <= 0) {
  147. return is_res_true;
  148. }
  149. if (PySet_GET_SIZE(state->iscoroutine_typecache) < 100) {
  150. /* Just in case we don't want to cache more than 100
  151. positive types. That shouldn't ever happen, unless
  152. someone stressing the system on purpose.
  153. */
  154. if (PySet_Add(state->iscoroutine_typecache, (PyObject*) Py_TYPE(coro))) {
  155. return -1;
  156. }
  157. }
  158. return 1;
  159. }
  160. static inline int
  161. is_coroutine(asyncio_state *state, PyObject *coro)
  162. {
  163. if (PyCoro_CheckExact(coro)) {
  164. return 1;
  165. }
  166. /* Check if `type(coro)` is in the cache.
  167. Caching makes is_coroutine() function almost as fast as
  168. PyCoro_CheckExact() for non-native coroutine-like objects
  169. (like coroutines compiled with Cython).
  170. asyncio.iscoroutine() has its own type caching mechanism.
  171. This cache allows us to avoid the cost of even calling
  172. a pure-Python function in 99.9% cases.
  173. */
  174. int has_it = PySet_Contains(
  175. state->iscoroutine_typecache, (PyObject*) Py_TYPE(coro));
  176. if (has_it == 0) {
  177. /* type(coro) is not in iscoroutine_typecache */
  178. return _is_coroutine(state, coro);
  179. }
  180. /* either an error has occurred or
  181. type(coro) is in iscoroutine_typecache
  182. */
  183. return has_it;
  184. }
  185. static PyObject *
  186. get_future_loop(asyncio_state *state, PyObject *fut)
  187. {
  188. /* Implementation of `asyncio.futures._get_loop` */
  189. PyObject *getloop;
  190. if (Future_CheckExact(state, fut) || Task_CheckExact(state, fut)) {
  191. PyObject *loop = ((FutureObj *)fut)->fut_loop;
  192. return Py_NewRef(loop);
  193. }
  194. if (_PyObject_LookupAttr(fut, &_Py_ID(get_loop), &getloop) < 0) {
  195. return NULL;
  196. }
  197. if (getloop != NULL) {
  198. PyObject *res = PyObject_CallNoArgs(getloop);
  199. Py_DECREF(getloop);
  200. return res;
  201. }
  202. return PyObject_GetAttr(fut, &_Py_ID(_loop));
  203. }
  204. static int
  205. get_running_loop(asyncio_state *state, PyObject **loop)
  206. {
  207. PyObject *rl;
  208. PyThreadState *ts = _PyThreadState_GET();
  209. uint64_t ts_id = PyThreadState_GetID(ts);
  210. if (state->cached_running_loop_tsid == ts_id &&
  211. state->cached_running_loop != NULL)
  212. {
  213. // Fast path, check the cache.
  214. rl = state->cached_running_loop;
  215. }
  216. else {
  217. PyObject *ts_dict = _PyThreadState_GetDict(ts); // borrowed
  218. if (ts_dict == NULL) {
  219. goto not_found;
  220. }
  221. rl = PyDict_GetItemWithError(
  222. ts_dict, &_Py_ID(__asyncio_running_event_loop__)); // borrowed
  223. if (rl == NULL) {
  224. if (PyErr_Occurred()) {
  225. goto error;
  226. }
  227. else {
  228. goto not_found;
  229. }
  230. }
  231. state->cached_running_loop = rl;
  232. state->cached_running_loop_tsid = ts_id;
  233. }
  234. if (rl == Py_None) {
  235. goto not_found;
  236. }
  237. *loop = Py_NewRef(rl);
  238. return 0;
  239. not_found:
  240. *loop = NULL;
  241. return 0;
  242. error:
  243. *loop = NULL;
  244. return -1;
  245. }
  246. static int
  247. set_running_loop(asyncio_state *state, PyObject *loop)
  248. {
  249. PyObject *ts_dict = NULL;
  250. PyThreadState *tstate = _PyThreadState_GET();
  251. if (tstate != NULL) {
  252. ts_dict = _PyThreadState_GetDict(tstate); // borrowed
  253. }
  254. if (ts_dict == NULL) {
  255. PyErr_SetString(
  256. PyExc_RuntimeError, "thread-local storage is not available");
  257. return -1;
  258. }
  259. if (PyDict_SetItem(
  260. ts_dict, &_Py_ID(__asyncio_running_event_loop__), loop) < 0)
  261. {
  262. return -1;
  263. }
  264. state->cached_running_loop = loop; // borrowed, kept alive by ts_dict
  265. state->cached_running_loop_tsid = PyThreadState_GetID(tstate);
  266. return 0;
  267. }
  268. static PyObject *
  269. get_event_loop(asyncio_state *state)
  270. {
  271. PyObject *loop;
  272. PyObject *policy;
  273. if (get_running_loop(state, &loop)) {
  274. return NULL;
  275. }
  276. if (loop != NULL) {
  277. return loop;
  278. }
  279. policy = PyObject_CallNoArgs(state->asyncio_get_event_loop_policy);
  280. if (policy == NULL) {
  281. return NULL;
  282. }
  283. loop = PyObject_CallMethodNoArgs(policy, &_Py_ID(get_event_loop));
  284. Py_DECREF(policy);
  285. return loop;
  286. }
  287. static int
  288. call_soon(asyncio_state *state, PyObject *loop, PyObject *func, PyObject *arg,
  289. PyObject *ctx)
  290. {
  291. PyObject *handle;
  292. if (ctx == NULL) {
  293. PyObject *stack[] = {loop, func, arg};
  294. size_t nargsf = 3 | PY_VECTORCALL_ARGUMENTS_OFFSET;
  295. handle = PyObject_VectorcallMethod(&_Py_ID(call_soon), stack, nargsf, NULL);
  296. }
  297. else {
  298. /* All refs in 'stack' are borrowed. */
  299. PyObject *stack[4];
  300. size_t nargs = 2;
  301. stack[0] = loop;
  302. stack[1] = func;
  303. if (arg != NULL) {
  304. stack[2] = arg;
  305. nargs++;
  306. }
  307. stack[nargs] = (PyObject *)ctx;
  308. size_t nargsf = nargs | PY_VECTORCALL_ARGUMENTS_OFFSET;
  309. handle = PyObject_VectorcallMethod(&_Py_ID(call_soon), stack, nargsf,
  310. state->context_kwname);
  311. }
  312. if (handle == NULL) {
  313. return -1;
  314. }
  315. Py_DECREF(handle);
  316. return 0;
  317. }
  318. static inline int
  319. future_is_alive(FutureObj *fut)
  320. {
  321. return fut->fut_loop != NULL;
  322. }
  323. static inline int
  324. future_ensure_alive(FutureObj *fut)
  325. {
  326. if (!future_is_alive(fut)) {
  327. PyErr_SetString(PyExc_RuntimeError,
  328. "Future object is not initialized.");
  329. return -1;
  330. }
  331. return 0;
  332. }
  333. #define ENSURE_FUTURE_ALIVE(state, fut) \
  334. do { \
  335. assert(Future_Check(state, fut) || Task_Check(state, fut)); \
  336. (void)state; \
  337. if (future_ensure_alive((FutureObj*)fut)) { \
  338. return NULL; \
  339. } \
  340. } while(0);
  341. static int
  342. future_schedule_callbacks(asyncio_state *state, FutureObj *fut)
  343. {
  344. Py_ssize_t len;
  345. Py_ssize_t i;
  346. if (fut->fut_callback0 != NULL) {
  347. /* There's a 1st callback */
  348. int ret = call_soon(state,
  349. fut->fut_loop, fut->fut_callback0,
  350. (PyObject *)fut, fut->fut_context0);
  351. Py_CLEAR(fut->fut_callback0);
  352. Py_CLEAR(fut->fut_context0);
  353. if (ret) {
  354. /* If an error occurs in pure-Python implementation,
  355. all callbacks are cleared. */
  356. Py_CLEAR(fut->fut_callbacks);
  357. return ret;
  358. }
  359. /* we called the first callback, now try calling
  360. callbacks from the 'fut_callbacks' list. */
  361. }
  362. if (fut->fut_callbacks == NULL) {
  363. /* No more callbacks, return. */
  364. return 0;
  365. }
  366. len = PyList_GET_SIZE(fut->fut_callbacks);
  367. if (len == 0) {
  368. /* The list of callbacks was empty; clear it and return. */
  369. Py_CLEAR(fut->fut_callbacks);
  370. return 0;
  371. }
  372. for (i = 0; i < len; i++) {
  373. PyObject *cb_tup = PyList_GET_ITEM(fut->fut_callbacks, i);
  374. PyObject *cb = PyTuple_GET_ITEM(cb_tup, 0);
  375. PyObject *ctx = PyTuple_GET_ITEM(cb_tup, 1);
  376. if (call_soon(state, fut->fut_loop, cb, (PyObject *)fut, ctx)) {
  377. /* If an error occurs in pure-Python implementation,
  378. all callbacks are cleared. */
  379. Py_CLEAR(fut->fut_callbacks);
  380. return -1;
  381. }
  382. }
  383. Py_CLEAR(fut->fut_callbacks);
  384. return 0;
  385. }
  386. static int
  387. future_init(FutureObj *fut, PyObject *loop)
  388. {
  389. PyObject *res;
  390. int is_true;
  391. // Same to FutureObj_clear() but not clearing fut->dict
  392. Py_CLEAR(fut->fut_loop);
  393. Py_CLEAR(fut->fut_callback0);
  394. Py_CLEAR(fut->fut_context0);
  395. Py_CLEAR(fut->fut_callbacks);
  396. Py_CLEAR(fut->fut_result);
  397. Py_CLEAR(fut->fut_exception);
  398. Py_CLEAR(fut->fut_exception_tb);
  399. Py_CLEAR(fut->fut_source_tb);
  400. Py_CLEAR(fut->fut_cancel_msg);
  401. Py_CLEAR(fut->fut_cancelled_exc);
  402. fut->fut_state = STATE_PENDING;
  403. fut->fut_log_tb = 0;
  404. fut->fut_blocking = 0;
  405. if (loop == Py_None) {
  406. asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut);
  407. loop = get_event_loop(state);
  408. if (loop == NULL) {
  409. return -1;
  410. }
  411. }
  412. else {
  413. Py_INCREF(loop);
  414. }
  415. fut->fut_loop = loop;
  416. res = PyObject_CallMethodNoArgs(fut->fut_loop, &_Py_ID(get_debug));
  417. if (res == NULL) {
  418. return -1;
  419. }
  420. is_true = PyObject_IsTrue(res);
  421. Py_DECREF(res);
  422. if (is_true < 0) {
  423. return -1;
  424. }
  425. if (is_true && !_Py_IsInterpreterFinalizing(PyInterpreterState_Get())) {
  426. /* Only try to capture the traceback if the interpreter is not being
  427. finalized. The original motivation to add a `_Py_IsFinalizing()`
  428. call was to prevent SIGSEGV when a Future is created in a __del__
  429. method, which is called during the interpreter shutdown and the
  430. traceback module is already unloaded.
  431. */
  432. asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut);
  433. fut->fut_source_tb = PyObject_CallNoArgs(state->traceback_extract_stack);
  434. if (fut->fut_source_tb == NULL) {
  435. return -1;
  436. }
  437. }
  438. return 0;
  439. }
  440. static PyObject *
  441. future_set_result(asyncio_state *state, FutureObj *fut, PyObject *res)
  442. {
  443. if (future_ensure_alive(fut)) {
  444. return NULL;
  445. }
  446. if (fut->fut_state != STATE_PENDING) {
  447. PyErr_SetString(state->asyncio_InvalidStateError, "invalid state");
  448. return NULL;
  449. }
  450. assert(!fut->fut_result);
  451. fut->fut_result = Py_NewRef(res);
  452. fut->fut_state = STATE_FINISHED;
  453. if (future_schedule_callbacks(state, fut) == -1) {
  454. return NULL;
  455. }
  456. Py_RETURN_NONE;
  457. }
  458. static PyObject *
  459. future_set_exception(asyncio_state *state, FutureObj *fut, PyObject *exc)
  460. {
  461. PyObject *exc_val = NULL;
  462. if (fut->fut_state != STATE_PENDING) {
  463. PyErr_SetString(state->asyncio_InvalidStateError, "invalid state");
  464. return NULL;
  465. }
  466. if (PyExceptionClass_Check(exc)) {
  467. exc_val = PyObject_CallNoArgs(exc);
  468. if (exc_val == NULL) {
  469. return NULL;
  470. }
  471. if (fut->fut_state != STATE_PENDING) {
  472. Py_DECREF(exc_val);
  473. PyErr_SetString(state->asyncio_InvalidStateError, "invalid state");
  474. return NULL;
  475. }
  476. }
  477. else {
  478. exc_val = Py_NewRef(exc);
  479. }
  480. if (!PyExceptionInstance_Check(exc_val)) {
  481. Py_DECREF(exc_val);
  482. PyErr_SetString(PyExc_TypeError, "invalid exception object");
  483. return NULL;
  484. }
  485. if (PyErr_GivenExceptionMatches(exc_val, PyExc_StopIteration)) {
  486. const char *msg = "StopIteration interacts badly with "
  487. "generators and cannot be raised into a "
  488. "Future";
  489. PyObject *message = PyUnicode_FromString(msg);
  490. if (message == NULL) {
  491. Py_DECREF(exc_val);
  492. return NULL;
  493. }
  494. PyObject *err = PyObject_CallOneArg(PyExc_RuntimeError, message);
  495. Py_DECREF(message);
  496. if (err == NULL) {
  497. Py_DECREF(exc_val);
  498. return NULL;
  499. }
  500. assert(PyExceptionInstance_Check(err));
  501. PyException_SetCause(err, Py_NewRef(exc_val));
  502. PyException_SetContext(err, Py_NewRef(exc_val));
  503. Py_DECREF(exc_val);
  504. exc_val = err;
  505. }
  506. assert(!fut->fut_exception);
  507. assert(!fut->fut_exception_tb);
  508. fut->fut_exception = exc_val;
  509. fut->fut_exception_tb = PyException_GetTraceback(exc_val);
  510. fut->fut_state = STATE_FINISHED;
  511. if (future_schedule_callbacks(state, fut) == -1) {
  512. return NULL;
  513. }
  514. fut->fut_log_tb = 1;
  515. Py_RETURN_NONE;
  516. }
  517. static PyObject *
  518. create_cancelled_error(asyncio_state *state, FutureObj *fut)
  519. {
  520. PyObject *exc;
  521. if (fut->fut_cancelled_exc != NULL) {
  522. /* transfer ownership */
  523. exc = fut->fut_cancelled_exc;
  524. fut->fut_cancelled_exc = NULL;
  525. return exc;
  526. }
  527. PyObject *msg = fut->fut_cancel_msg;
  528. if (msg == NULL || msg == Py_None) {
  529. exc = PyObject_CallNoArgs(state->asyncio_CancelledError);
  530. } else {
  531. exc = PyObject_CallOneArg(state->asyncio_CancelledError, msg);
  532. }
  533. return exc;
  534. }
  535. static void
  536. future_set_cancelled_error(asyncio_state *state, FutureObj *fut)
  537. {
  538. PyObject *exc = create_cancelled_error(state, fut);
  539. if (exc == NULL) {
  540. return;
  541. }
  542. PyErr_SetObject(state->asyncio_CancelledError, exc);
  543. Py_DECREF(exc);
  544. }
  545. static int
  546. future_get_result(asyncio_state *state, FutureObj *fut, PyObject **result)
  547. {
  548. if (fut->fut_state == STATE_CANCELLED) {
  549. future_set_cancelled_error(state, fut);
  550. return -1;
  551. }
  552. if (fut->fut_state != STATE_FINISHED) {
  553. PyErr_SetString(state->asyncio_InvalidStateError,
  554. "Result is not set.");
  555. return -1;
  556. }
  557. fut->fut_log_tb = 0;
  558. if (fut->fut_exception != NULL) {
  559. PyObject *tb = fut->fut_exception_tb;
  560. if (tb == NULL) {
  561. tb = Py_None;
  562. }
  563. if (PyException_SetTraceback(fut->fut_exception, tb) < 0) {
  564. return -1;
  565. }
  566. *result = Py_NewRef(fut->fut_exception);
  567. Py_CLEAR(fut->fut_exception_tb);
  568. return 1;
  569. }
  570. *result = Py_NewRef(fut->fut_result);
  571. return 0;
  572. }
  573. static PyObject *
  574. future_add_done_callback(asyncio_state *state, FutureObj *fut, PyObject *arg,
  575. PyObject *ctx)
  576. {
  577. if (!future_is_alive(fut)) {
  578. PyErr_SetString(PyExc_RuntimeError, "uninitialized Future object");
  579. return NULL;
  580. }
  581. if (fut->fut_state != STATE_PENDING) {
  582. /* The future is done/cancelled, so schedule the callback
  583. right away. */
  584. if (call_soon(state, fut->fut_loop, arg, (PyObject*) fut, ctx)) {
  585. return NULL;
  586. }
  587. }
  588. else {
  589. /* The future is pending, add a callback.
  590. Callbacks in the future object are stored as follows:
  591. callback0 -- a pointer to the first callback
  592. callbacks -- a list of 2nd, 3rd, ... callbacks
  593. Invariants:
  594. * callbacks != NULL:
  595. There are some callbacks in in the list. Just
  596. add the new callback to it.
  597. * callbacks == NULL and callback0 == NULL:
  598. This is the first callback. Set it to callback0.
  599. * callbacks == NULL and callback0 != NULL:
  600. This is a second callback. Initialize callbacks
  601. with a new list and add the new callback to it.
  602. */
  603. if (fut->fut_callbacks == NULL && fut->fut_callback0 == NULL) {
  604. fut->fut_callback0 = Py_NewRef(arg);
  605. fut->fut_context0 = Py_NewRef(ctx);
  606. }
  607. else {
  608. PyObject *tup = PyTuple_New(2);
  609. if (tup == NULL) {
  610. return NULL;
  611. }
  612. Py_INCREF(arg);
  613. PyTuple_SET_ITEM(tup, 0, arg);
  614. Py_INCREF(ctx);
  615. PyTuple_SET_ITEM(tup, 1, (PyObject *)ctx);
  616. if (fut->fut_callbacks != NULL) {
  617. int err = PyList_Append(fut->fut_callbacks, tup);
  618. if (err) {
  619. Py_DECREF(tup);
  620. return NULL;
  621. }
  622. Py_DECREF(tup);
  623. }
  624. else {
  625. fut->fut_callbacks = PyList_New(1);
  626. if (fut->fut_callbacks == NULL) {
  627. Py_DECREF(tup);
  628. return NULL;
  629. }
  630. PyList_SET_ITEM(fut->fut_callbacks, 0, tup); /* borrow */
  631. }
  632. }
  633. }
  634. Py_RETURN_NONE;
  635. }
  636. static PyObject *
  637. future_cancel(asyncio_state *state, FutureObj *fut, PyObject *msg)
  638. {
  639. fut->fut_log_tb = 0;
  640. if (fut->fut_state != STATE_PENDING) {
  641. Py_RETURN_FALSE;
  642. }
  643. fut->fut_state = STATE_CANCELLED;
  644. Py_XINCREF(msg);
  645. Py_XSETREF(fut->fut_cancel_msg, msg);
  646. if (future_schedule_callbacks(state, fut) == -1) {
  647. return NULL;
  648. }
  649. Py_RETURN_TRUE;
  650. }
  651. /*[clinic input]
  652. _asyncio.Future.__init__
  653. *
  654. loop: object = None
  655. This class is *almost* compatible with concurrent.futures.Future.
  656. Differences:
  657. - result() and exception() do not take a timeout argument and
  658. raise an exception when the future isn't done yet.
  659. - Callbacks registered with add_done_callback() are always called
  660. via the event loop's call_soon_threadsafe().
  661. - This class is not compatible with the wait() and as_completed()
  662. methods in the concurrent.futures package.
  663. [clinic start generated code]*/
  664. static int
  665. _asyncio_Future___init___impl(FutureObj *self, PyObject *loop)
  666. /*[clinic end generated code: output=9ed75799eaccb5d6 input=89af317082bc0bf8]*/
  667. {
  668. return future_init(self, loop);
  669. }
  670. static int
  671. FutureObj_clear(FutureObj *fut)
  672. {
  673. Py_CLEAR(fut->fut_loop);
  674. Py_CLEAR(fut->fut_callback0);
  675. Py_CLEAR(fut->fut_context0);
  676. Py_CLEAR(fut->fut_callbacks);
  677. Py_CLEAR(fut->fut_result);
  678. Py_CLEAR(fut->fut_exception);
  679. Py_CLEAR(fut->fut_exception_tb);
  680. Py_CLEAR(fut->fut_source_tb);
  681. Py_CLEAR(fut->fut_cancel_msg);
  682. Py_CLEAR(fut->fut_cancelled_exc);
  683. Py_CLEAR(fut->dict);
  684. return 0;
  685. }
  686. static int
  687. FutureObj_traverse(FutureObj *fut, visitproc visit, void *arg)
  688. {
  689. Py_VISIT(Py_TYPE(fut));
  690. Py_VISIT(fut->fut_loop);
  691. Py_VISIT(fut->fut_callback0);
  692. Py_VISIT(fut->fut_context0);
  693. Py_VISIT(fut->fut_callbacks);
  694. Py_VISIT(fut->fut_result);
  695. Py_VISIT(fut->fut_exception);
  696. Py_VISIT(fut->fut_exception_tb);
  697. Py_VISIT(fut->fut_source_tb);
  698. Py_VISIT(fut->fut_cancel_msg);
  699. Py_VISIT(fut->fut_cancelled_exc);
  700. Py_VISIT(fut->dict);
  701. return 0;
  702. }
  703. /*[clinic input]
  704. _asyncio.Future.result
  705. Return the result this future represents.
  706. If the future has been cancelled, raises CancelledError. If the
  707. future's result isn't yet available, raises InvalidStateError. If
  708. the future is done and has an exception set, this exception is raised.
  709. [clinic start generated code]*/
  710. static PyObject *
  711. _asyncio_Future_result_impl(FutureObj *self)
  712. /*[clinic end generated code: output=f35f940936a4b1e5 input=49ecf9cf5ec50dc5]*/
  713. {
  714. asyncio_state *state = get_asyncio_state_by_def((PyObject *)self);
  715. PyObject *result;
  716. if (!future_is_alive(self)) {
  717. PyErr_SetString(state->asyncio_InvalidStateError,
  718. "Future object is not initialized.");
  719. return NULL;
  720. }
  721. int res = future_get_result(state, self, &result);
  722. if (res == -1) {
  723. return NULL;
  724. }
  725. if (res == 0) {
  726. return result;
  727. }
  728. assert(res == 1);
  729. PyErr_SetObject(PyExceptionInstance_Class(result), result);
  730. Py_DECREF(result);
  731. return NULL;
  732. }
  733. /*[clinic input]
  734. _asyncio.Future.exception
  735. cls: defining_class
  736. /
  737. Return the exception that was set on this future.
  738. The exception (or None if no exception was set) is returned only if
  739. the future is done. If the future has been cancelled, raises
  740. CancelledError. If the future isn't done yet, raises
  741. InvalidStateError.
  742. [clinic start generated code]*/
  743. static PyObject *
  744. _asyncio_Future_exception_impl(FutureObj *self, PyTypeObject *cls)
  745. /*[clinic end generated code: output=ce75576b187c905b input=3faf15c22acdb60d]*/
  746. {
  747. if (!future_is_alive(self)) {
  748. asyncio_state *state = get_asyncio_state_by_cls(cls);
  749. PyErr_SetString(state->asyncio_InvalidStateError,
  750. "Future object is not initialized.");
  751. return NULL;
  752. }
  753. if (self->fut_state == STATE_CANCELLED) {
  754. asyncio_state *state = get_asyncio_state_by_cls(cls);
  755. future_set_cancelled_error(state, self);
  756. return NULL;
  757. }
  758. if (self->fut_state != STATE_FINISHED) {
  759. asyncio_state *state = get_asyncio_state_by_cls(cls);
  760. PyErr_SetString(state->asyncio_InvalidStateError,
  761. "Exception is not set.");
  762. return NULL;
  763. }
  764. if (self->fut_exception != NULL) {
  765. self->fut_log_tb = 0;
  766. return Py_NewRef(self->fut_exception);
  767. }
  768. Py_RETURN_NONE;
  769. }
  770. /*[clinic input]
  771. _asyncio.Future.set_result
  772. cls: defining_class
  773. result: object
  774. /
  775. Mark the future done and set its result.
  776. If the future is already done when this method is called, raises
  777. InvalidStateError.
  778. [clinic start generated code]*/
  779. static PyObject *
  780. _asyncio_Future_set_result_impl(FutureObj *self, PyTypeObject *cls,
  781. PyObject *result)
  782. /*[clinic end generated code: output=99afbbe78f99c32d input=d5a41c1e353acc2e]*/
  783. {
  784. asyncio_state *state = get_asyncio_state_by_cls(cls);
  785. ENSURE_FUTURE_ALIVE(state, self)
  786. return future_set_result(state, self, result);
  787. }
  788. /*[clinic input]
  789. _asyncio.Future.set_exception
  790. cls: defining_class
  791. exception: object
  792. /
  793. Mark the future done and set an exception.
  794. If the future is already done when this method is called, raises
  795. InvalidStateError.
  796. [clinic start generated code]*/
  797. static PyObject *
  798. _asyncio_Future_set_exception_impl(FutureObj *self, PyTypeObject *cls,
  799. PyObject *exception)
  800. /*[clinic end generated code: output=0a5e8b5a52f058d6 input=a245cd49d3df939b]*/
  801. {
  802. asyncio_state *state = get_asyncio_state_by_cls(cls);
  803. ENSURE_FUTURE_ALIVE(state, self)
  804. return future_set_exception(state, self, exception);
  805. }
  806. /*[clinic input]
  807. _asyncio.Future.add_done_callback
  808. cls: defining_class
  809. fn: object
  810. /
  811. *
  812. context: object = NULL
  813. Add a callback to be run when the future becomes done.
  814. The callback is called with a single argument - the future object. If
  815. the future is already done when this is called, the callback is
  816. scheduled with call_soon.
  817. [clinic start generated code]*/
  818. static PyObject *
  819. _asyncio_Future_add_done_callback_impl(FutureObj *self, PyTypeObject *cls,
  820. PyObject *fn, PyObject *context)
  821. /*[clinic end generated code: output=922e9a4cbd601167 input=599261c521458cc2]*/
  822. {
  823. asyncio_state *state = get_asyncio_state_by_cls(cls);
  824. if (context == NULL) {
  825. context = PyContext_CopyCurrent();
  826. if (context == NULL) {
  827. return NULL;
  828. }
  829. PyObject *res = future_add_done_callback(state, self, fn, context);
  830. Py_DECREF(context);
  831. return res;
  832. }
  833. return future_add_done_callback(state, self, fn, context);
  834. }
  835. /*[clinic input]
  836. _asyncio.Future.remove_done_callback
  837. cls: defining_class
  838. fn: object
  839. /
  840. Remove all instances of a callback from the "call when done" list.
  841. Returns the number of callbacks removed.
  842. [clinic start generated code]*/
  843. static PyObject *
  844. _asyncio_Future_remove_done_callback_impl(FutureObj *self, PyTypeObject *cls,
  845. PyObject *fn)
  846. /*[clinic end generated code: output=2da35ccabfe41b98 input=c7518709b86fc747]*/
  847. {
  848. PyObject *newlist;
  849. Py_ssize_t len, i, j=0;
  850. Py_ssize_t cleared_callback0 = 0;
  851. asyncio_state *state = get_asyncio_state_by_cls(cls);
  852. ENSURE_FUTURE_ALIVE(state, self)
  853. if (self->fut_callback0 != NULL) {
  854. int cmp = PyObject_RichCompareBool(self->fut_callback0, fn, Py_EQ);
  855. if (cmp == -1) {
  856. return NULL;
  857. }
  858. if (cmp == 1) {
  859. /* callback0 == fn */
  860. Py_CLEAR(self->fut_callback0);
  861. Py_CLEAR(self->fut_context0);
  862. cleared_callback0 = 1;
  863. }
  864. }
  865. if (self->fut_callbacks == NULL) {
  866. return PyLong_FromSsize_t(cleared_callback0);
  867. }
  868. len = PyList_GET_SIZE(self->fut_callbacks);
  869. if (len == 0) {
  870. Py_CLEAR(self->fut_callbacks);
  871. return PyLong_FromSsize_t(cleared_callback0);
  872. }
  873. if (len == 1) {
  874. PyObject *cb_tup = PyList_GET_ITEM(self->fut_callbacks, 0);
  875. int cmp = PyObject_RichCompareBool(
  876. PyTuple_GET_ITEM(cb_tup, 0), fn, Py_EQ);
  877. if (cmp == -1) {
  878. return NULL;
  879. }
  880. if (cmp == 1) {
  881. /* callbacks[0] == fn */
  882. Py_CLEAR(self->fut_callbacks);
  883. return PyLong_FromSsize_t(1 + cleared_callback0);
  884. }
  885. /* callbacks[0] != fn and len(callbacks) == 1 */
  886. return PyLong_FromSsize_t(cleared_callback0);
  887. }
  888. newlist = PyList_New(len);
  889. if (newlist == NULL) {
  890. return NULL;
  891. }
  892. // Beware: PyObject_RichCompareBool below may change fut_callbacks.
  893. // See GH-97592.
  894. for (i = 0;
  895. self->fut_callbacks != NULL && i < PyList_GET_SIZE(self->fut_callbacks);
  896. i++) {
  897. int ret;
  898. PyObject *item = PyList_GET_ITEM(self->fut_callbacks, i);
  899. Py_INCREF(item);
  900. ret = PyObject_RichCompareBool(PyTuple_GET_ITEM(item, 0), fn, Py_EQ);
  901. if (ret == 0) {
  902. if (j < len) {
  903. PyList_SET_ITEM(newlist, j, item);
  904. j++;
  905. continue;
  906. }
  907. ret = PyList_Append(newlist, item);
  908. }
  909. Py_DECREF(item);
  910. if (ret < 0) {
  911. goto fail;
  912. }
  913. }
  914. // Note: fut_callbacks may have been cleared.
  915. if (j == 0 || self->fut_callbacks == NULL) {
  916. Py_CLEAR(self->fut_callbacks);
  917. Py_DECREF(newlist);
  918. return PyLong_FromSsize_t(len + cleared_callback0);
  919. }
  920. if (j < len) {
  921. Py_SET_SIZE(newlist, j);
  922. }
  923. j = PyList_GET_SIZE(newlist);
  924. len = PyList_GET_SIZE(self->fut_callbacks);
  925. if (j != len) {
  926. if (PyList_SetSlice(self->fut_callbacks, 0, len, newlist) < 0) {
  927. goto fail;
  928. }
  929. }
  930. Py_DECREF(newlist);
  931. return PyLong_FromSsize_t(len - j + cleared_callback0);
  932. fail:
  933. Py_DECREF(newlist);
  934. return NULL;
  935. }
  936. /*[clinic input]
  937. _asyncio.Future.cancel
  938. cls: defining_class
  939. /
  940. msg: object = None
  941. Cancel the future and schedule callbacks.
  942. If the future is already done or cancelled, return False. Otherwise,
  943. change the future's state to cancelled, schedule the callbacks and
  944. return True.
  945. [clinic start generated code]*/
  946. static PyObject *
  947. _asyncio_Future_cancel_impl(FutureObj *self, PyTypeObject *cls,
  948. PyObject *msg)
  949. /*[clinic end generated code: output=074956f35904b034 input=bba8f8b786941a94]*/
  950. {
  951. asyncio_state *state = get_asyncio_state_by_cls(cls);
  952. ENSURE_FUTURE_ALIVE(state, self)
  953. return future_cancel(state, self, msg);
  954. }
  955. /*[clinic input]
  956. _asyncio.Future.cancelled
  957. Return True if the future was cancelled.
  958. [clinic start generated code]*/
  959. static PyObject *
  960. _asyncio_Future_cancelled_impl(FutureObj *self)
  961. /*[clinic end generated code: output=145197ced586357d input=943ab8b7b7b17e45]*/
  962. {
  963. if (future_is_alive(self) && self->fut_state == STATE_CANCELLED) {
  964. Py_RETURN_TRUE;
  965. }
  966. else {
  967. Py_RETURN_FALSE;
  968. }
  969. }
  970. /*[clinic input]
  971. _asyncio.Future.done
  972. Return True if the future is done.
  973. Done means either that a result / exception are available, or that the
  974. future was cancelled.
  975. [clinic start generated code]*/
  976. static PyObject *
  977. _asyncio_Future_done_impl(FutureObj *self)
  978. /*[clinic end generated code: output=244c5ac351145096 input=28d7b23fdb65d2ac]*/
  979. {
  980. if (!future_is_alive(self) || self->fut_state == STATE_PENDING) {
  981. Py_RETURN_FALSE;
  982. }
  983. else {
  984. Py_RETURN_TRUE;
  985. }
  986. }
  987. /*[clinic input]
  988. _asyncio.Future.get_loop
  989. cls: defining_class
  990. /
  991. Return the event loop the Future is bound to.
  992. [clinic start generated code]*/
  993. static PyObject *
  994. _asyncio_Future_get_loop_impl(FutureObj *self, PyTypeObject *cls)
  995. /*[clinic end generated code: output=f50ea6c374d9ee97 input=163c2c498b45a1f0]*/
  996. {
  997. asyncio_state *state = get_asyncio_state_by_cls(cls);
  998. ENSURE_FUTURE_ALIVE(state, self)
  999. return Py_NewRef(self->fut_loop);
  1000. }
  1001. static PyObject *
  1002. FutureObj_get_blocking(FutureObj *fut, void *Py_UNUSED(ignored))
  1003. {
  1004. if (future_is_alive(fut) && fut->fut_blocking) {
  1005. Py_RETURN_TRUE;
  1006. }
  1007. else {
  1008. Py_RETURN_FALSE;
  1009. }
  1010. }
  1011. static int
  1012. FutureObj_set_blocking(FutureObj *fut, PyObject *val, void *Py_UNUSED(ignored))
  1013. {
  1014. if (future_ensure_alive(fut)) {
  1015. return -1;
  1016. }
  1017. if (val == NULL) {
  1018. PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
  1019. return -1;
  1020. }
  1021. int is_true = PyObject_IsTrue(val);
  1022. if (is_true < 0) {
  1023. return -1;
  1024. }
  1025. fut->fut_blocking = is_true;
  1026. return 0;
  1027. }
  1028. static PyObject *
  1029. FutureObj_get_log_traceback(FutureObj *fut, void *Py_UNUSED(ignored))
  1030. {
  1031. asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut);
  1032. ENSURE_FUTURE_ALIVE(state, fut)
  1033. if (fut->fut_log_tb) {
  1034. Py_RETURN_TRUE;
  1035. }
  1036. else {
  1037. Py_RETURN_FALSE;
  1038. }
  1039. }
  1040. static int
  1041. FutureObj_set_log_traceback(FutureObj *fut, PyObject *val, void *Py_UNUSED(ignored))
  1042. {
  1043. if (val == NULL) {
  1044. PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
  1045. return -1;
  1046. }
  1047. int is_true = PyObject_IsTrue(val);
  1048. if (is_true < 0) {
  1049. return -1;
  1050. }
  1051. if (is_true) {
  1052. PyErr_SetString(PyExc_ValueError,
  1053. "_log_traceback can only be set to False");
  1054. return -1;
  1055. }
  1056. fut->fut_log_tb = is_true;
  1057. return 0;
  1058. }
  1059. static PyObject *
  1060. FutureObj_get_loop(FutureObj *fut, void *Py_UNUSED(ignored))
  1061. {
  1062. if (!future_is_alive(fut)) {
  1063. Py_RETURN_NONE;
  1064. }
  1065. return Py_NewRef(fut->fut_loop);
  1066. }
  1067. static PyObject *
  1068. FutureObj_get_callbacks(FutureObj *fut, void *Py_UNUSED(ignored))
  1069. {
  1070. asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut);
  1071. Py_ssize_t i;
  1072. ENSURE_FUTURE_ALIVE(state, fut)
  1073. if (fut->fut_callback0 == NULL) {
  1074. if (fut->fut_callbacks == NULL) {
  1075. Py_RETURN_NONE;
  1076. }
  1077. return Py_NewRef(fut->fut_callbacks);
  1078. }
  1079. Py_ssize_t len = 1;
  1080. if (fut->fut_callbacks != NULL) {
  1081. len += PyList_GET_SIZE(fut->fut_callbacks);
  1082. }
  1083. PyObject *new_list = PyList_New(len);
  1084. if (new_list == NULL) {
  1085. return NULL;
  1086. }
  1087. PyObject *tup0 = PyTuple_New(2);
  1088. if (tup0 == NULL) {
  1089. Py_DECREF(new_list);
  1090. return NULL;
  1091. }
  1092. Py_INCREF(fut->fut_callback0);
  1093. PyTuple_SET_ITEM(tup0, 0, fut->fut_callback0);
  1094. assert(fut->fut_context0 != NULL);
  1095. Py_INCREF(fut->fut_context0);
  1096. PyTuple_SET_ITEM(tup0, 1, (PyObject *)fut->fut_context0);
  1097. PyList_SET_ITEM(new_list, 0, tup0);
  1098. if (fut->fut_callbacks != NULL) {
  1099. for (i = 0; i < PyList_GET_SIZE(fut->fut_callbacks); i++) {
  1100. PyObject *cb = PyList_GET_ITEM(fut->fut_callbacks, i);
  1101. Py_INCREF(cb);
  1102. PyList_SET_ITEM(new_list, i + 1, cb);
  1103. }
  1104. }
  1105. return new_list;
  1106. }
  1107. static PyObject *
  1108. FutureObj_get_result(FutureObj *fut, void *Py_UNUSED(ignored))
  1109. {
  1110. asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut);
  1111. ENSURE_FUTURE_ALIVE(state, fut)
  1112. if (fut->fut_result == NULL) {
  1113. Py_RETURN_NONE;
  1114. }
  1115. return Py_NewRef(fut->fut_result);
  1116. }
  1117. static PyObject *
  1118. FutureObj_get_exception(FutureObj *fut, void *Py_UNUSED(ignored))
  1119. {
  1120. asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut);
  1121. ENSURE_FUTURE_ALIVE(state, fut)
  1122. if (fut->fut_exception == NULL) {
  1123. Py_RETURN_NONE;
  1124. }
  1125. return Py_NewRef(fut->fut_exception);
  1126. }
  1127. static PyObject *
  1128. FutureObj_get_source_traceback(FutureObj *fut, void *Py_UNUSED(ignored))
  1129. {
  1130. if (!future_is_alive(fut) || fut->fut_source_tb == NULL) {
  1131. Py_RETURN_NONE;
  1132. }
  1133. return Py_NewRef(fut->fut_source_tb);
  1134. }
  1135. static PyObject *
  1136. FutureObj_get_cancel_message(FutureObj *fut, void *Py_UNUSED(ignored))
  1137. {
  1138. if (fut->fut_cancel_msg == NULL) {
  1139. Py_RETURN_NONE;
  1140. }
  1141. return Py_NewRef(fut->fut_cancel_msg);
  1142. }
  1143. static int
  1144. FutureObj_set_cancel_message(FutureObj *fut, PyObject *msg,
  1145. void *Py_UNUSED(ignored))
  1146. {
  1147. if (msg == NULL) {
  1148. PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
  1149. return -1;
  1150. }
  1151. Py_INCREF(msg);
  1152. Py_XSETREF(fut->fut_cancel_msg, msg);
  1153. return 0;
  1154. }
  1155. static PyObject *
  1156. FutureObj_get_state(FutureObj *fut, void *Py_UNUSED(ignored))
  1157. {
  1158. asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut);
  1159. PyObject *ret = NULL;
  1160. ENSURE_FUTURE_ALIVE(state, fut)
  1161. switch (fut->fut_state) {
  1162. case STATE_PENDING:
  1163. ret = &_Py_ID(PENDING);
  1164. break;
  1165. case STATE_CANCELLED:
  1166. ret = &_Py_ID(CANCELLED);
  1167. break;
  1168. case STATE_FINISHED:
  1169. ret = &_Py_ID(FINISHED);
  1170. break;
  1171. default:
  1172. assert (0);
  1173. }
  1174. return Py_XNewRef(ret);
  1175. }
  1176. static PyObject *
  1177. FutureObj_repr(FutureObj *fut)
  1178. {
  1179. asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut);
  1180. ENSURE_FUTURE_ALIVE(state, fut)
  1181. return PyObject_CallOneArg(state->asyncio_future_repr_func, (PyObject *)fut);
  1182. }
  1183. /*[clinic input]
  1184. _asyncio.Future._make_cancelled_error
  1185. Create the CancelledError to raise if the Future is cancelled.
  1186. This should only be called once when handling a cancellation since
  1187. it erases the context exception value.
  1188. [clinic start generated code]*/
  1189. static PyObject *
  1190. _asyncio_Future__make_cancelled_error_impl(FutureObj *self)
  1191. /*[clinic end generated code: output=a5df276f6c1213de input=ac6effe4ba795ecc]*/
  1192. {
  1193. asyncio_state *state = get_asyncio_state_by_def((PyObject *)self);
  1194. return create_cancelled_error(state, self);
  1195. }
  1196. static void
  1197. FutureObj_finalize(FutureObj *fut)
  1198. {
  1199. PyObject *context;
  1200. PyObject *message = NULL;
  1201. PyObject *func;
  1202. if (!fut->fut_log_tb) {
  1203. return;
  1204. }
  1205. assert(fut->fut_exception != NULL);
  1206. fut->fut_log_tb = 0;
  1207. /* Save the current exception, if any. */
  1208. PyObject *exc = PyErr_GetRaisedException();
  1209. context = PyDict_New();
  1210. if (context == NULL) {
  1211. goto finally;
  1212. }
  1213. message = PyUnicode_FromFormat(
  1214. "%s exception was never retrieved", _PyType_Name(Py_TYPE(fut)));
  1215. if (message == NULL) {
  1216. goto finally;
  1217. }
  1218. if (PyDict_SetItem(context, &_Py_ID(message), message) < 0 ||
  1219. PyDict_SetItem(context, &_Py_ID(exception), fut->fut_exception) < 0 ||
  1220. PyDict_SetItem(context, &_Py_ID(future), (PyObject*)fut) < 0) {
  1221. goto finally;
  1222. }
  1223. if (fut->fut_source_tb != NULL) {
  1224. if (PyDict_SetItem(context, &_Py_ID(source_traceback),
  1225. fut->fut_source_tb) < 0) {
  1226. goto finally;
  1227. }
  1228. }
  1229. func = PyObject_GetAttr(fut->fut_loop, &_Py_ID(call_exception_handler));
  1230. if (func != NULL) {
  1231. PyObject *res = PyObject_CallOneArg(func, context);
  1232. if (res == NULL) {
  1233. PyErr_WriteUnraisable(func);
  1234. }
  1235. else {
  1236. Py_DECREF(res);
  1237. }
  1238. Py_DECREF(func);
  1239. }
  1240. finally:
  1241. Py_XDECREF(context);
  1242. Py_XDECREF(message);
  1243. /* Restore the saved exception. */
  1244. PyErr_SetRaisedException(exc);
  1245. }
  1246. static PyMethodDef FutureType_methods[] = {
  1247. _ASYNCIO_FUTURE_RESULT_METHODDEF
  1248. _ASYNCIO_FUTURE_EXCEPTION_METHODDEF
  1249. _ASYNCIO_FUTURE_SET_RESULT_METHODDEF
  1250. _ASYNCIO_FUTURE_SET_EXCEPTION_METHODDEF
  1251. _ASYNCIO_FUTURE_ADD_DONE_CALLBACK_METHODDEF
  1252. _ASYNCIO_FUTURE_REMOVE_DONE_CALLBACK_METHODDEF
  1253. _ASYNCIO_FUTURE_CANCEL_METHODDEF
  1254. _ASYNCIO_FUTURE_CANCELLED_METHODDEF
  1255. _ASYNCIO_FUTURE_DONE_METHODDEF
  1256. _ASYNCIO_FUTURE_GET_LOOP_METHODDEF
  1257. _ASYNCIO_FUTURE__MAKE_CANCELLED_ERROR_METHODDEF
  1258. {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
  1259. {NULL, NULL} /* Sentinel */
  1260. };
  1261. static PyMemberDef FutureType_members[] = {
  1262. {"__weaklistoffset__", T_PYSSIZET, offsetof(FutureObj, fut_weakreflist), READONLY},
  1263. {"__dictoffset__", T_PYSSIZET, offsetof(FutureObj, dict), READONLY},
  1264. {NULL},
  1265. };
  1266. #define FUTURE_COMMON_GETSETLIST \
  1267. {"_state", (getter)FutureObj_get_state, NULL, NULL}, \
  1268. {"_asyncio_future_blocking", (getter)FutureObj_get_blocking, \
  1269. (setter)FutureObj_set_blocking, NULL}, \
  1270. {"_loop", (getter)FutureObj_get_loop, NULL, NULL}, \
  1271. {"_callbacks", (getter)FutureObj_get_callbacks, NULL, NULL}, \
  1272. {"_result", (getter)FutureObj_get_result, NULL, NULL}, \
  1273. {"_exception", (getter)FutureObj_get_exception, NULL, NULL}, \
  1274. {"_log_traceback", (getter)FutureObj_get_log_traceback, \
  1275. (setter)FutureObj_set_log_traceback, NULL}, \
  1276. {"_source_traceback", (getter)FutureObj_get_source_traceback, \
  1277. NULL, NULL}, \
  1278. {"_cancel_message", (getter)FutureObj_get_cancel_message, \
  1279. (setter)FutureObj_set_cancel_message, NULL},
  1280. static PyGetSetDef FutureType_getsetlist[] = {
  1281. FUTURE_COMMON_GETSETLIST
  1282. {NULL} /* Sentinel */
  1283. };
  1284. static void FutureObj_dealloc(PyObject *self);
  1285. static PyType_Slot Future_slots[] = {
  1286. {Py_tp_dealloc, FutureObj_dealloc},
  1287. {Py_tp_repr, (reprfunc)FutureObj_repr},
  1288. {Py_tp_doc, (void *)_asyncio_Future___init____doc__},
  1289. {Py_tp_traverse, (traverseproc)FutureObj_traverse},
  1290. {Py_tp_clear, (inquiry)FutureObj_clear},
  1291. {Py_tp_iter, (getiterfunc)future_new_iter},
  1292. {Py_tp_methods, FutureType_methods},
  1293. {Py_tp_members, FutureType_members},
  1294. {Py_tp_getset, FutureType_getsetlist},
  1295. {Py_tp_init, (initproc)_asyncio_Future___init__},
  1296. {Py_tp_new, PyType_GenericNew},
  1297. {Py_tp_finalize, (destructor)FutureObj_finalize},
  1298. // async slots
  1299. {Py_am_await, (unaryfunc)future_new_iter},
  1300. {0, NULL},
  1301. };
  1302. static PyType_Spec Future_spec = {
  1303. .name = "_asyncio.Future",
  1304. .basicsize = sizeof(FutureObj),
  1305. .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE |
  1306. Py_TPFLAGS_IMMUTABLETYPE),
  1307. .slots = Future_slots,
  1308. };
  1309. static void
  1310. FutureObj_dealloc(PyObject *self)
  1311. {
  1312. FutureObj *fut = (FutureObj *)self;
  1313. if (PyObject_CallFinalizerFromDealloc(self) < 0) {
  1314. // resurrected.
  1315. return;
  1316. }
  1317. PyTypeObject *tp = Py_TYPE(fut);
  1318. PyObject_GC_UnTrack(self);
  1319. if (fut->fut_weakreflist != NULL) {
  1320. PyObject_ClearWeakRefs(self);
  1321. }
  1322. (void)FutureObj_clear(fut);
  1323. tp->tp_free(fut);
  1324. Py_DECREF(tp);
  1325. }
  1326. /*********************** Future Iterator **************************/
  1327. typedef struct futureiterobject {
  1328. PyObject_HEAD
  1329. FutureObj *future;
  1330. } futureiterobject;
  1331. static void
  1332. FutureIter_dealloc(futureiterobject *it)
  1333. {
  1334. PyTypeObject *tp = Py_TYPE(it);
  1335. // FutureIter is a heap type so any subclass must also be a heap type.
  1336. assert(_PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE));
  1337. PyObject *module = ((PyHeapTypeObject*)tp)->ht_module;
  1338. asyncio_state *state = NULL;
  1339. PyObject_GC_UnTrack(it);
  1340. tp->tp_clear((PyObject *)it);
  1341. // GH-115874: We can't use PyType_GetModuleByDef here as the type might have
  1342. // already been cleared, which is also why we must check if ht_module != NULL.
  1343. // Due to this restriction, subclasses that belong to a different module
  1344. // will not be able to use the free list.
  1345. if (module && _PyModule_GetDef(module) == &_asynciomodule) {
  1346. state = get_asyncio_state(module);
  1347. }
  1348. if (state && state->fi_freelist_len < FI_FREELIST_MAXLEN) {
  1349. state->fi_freelist_len++;
  1350. it->future = (FutureObj*) state->fi_freelist;
  1351. state->fi_freelist = it;
  1352. }
  1353. else {
  1354. PyObject_GC_Del(it);
  1355. Py_DECREF(tp);
  1356. }
  1357. }
  1358. static PySendResult
  1359. FutureIter_am_send(futureiterobject *it,
  1360. PyObject *Py_UNUSED(arg),
  1361. PyObject **result)
  1362. {
  1363. /* arg is unused, see the comment on FutureIter_send for clarification */
  1364. PyObject *res;
  1365. FutureObj *fut = it->future;
  1366. *result = NULL;
  1367. if (fut == NULL) {
  1368. return PYGEN_ERROR;
  1369. }
  1370. if (fut->fut_state == STATE_PENDING) {
  1371. if (!fut->fut_blocking) {
  1372. fut->fut_blocking = 1;
  1373. *result = Py_NewRef(fut);
  1374. return PYGEN_NEXT;
  1375. }
  1376. PyErr_SetString(PyExc_RuntimeError,
  1377. "await wasn't used with future");
  1378. return PYGEN_ERROR;
  1379. }
  1380. it->future = NULL;
  1381. res = _asyncio_Future_result_impl(fut);
  1382. if (res != NULL) {
  1383. Py_DECREF(fut);
  1384. *result = res;
  1385. return PYGEN_RETURN;
  1386. }
  1387. Py_DECREF(fut);
  1388. return PYGEN_ERROR;
  1389. }
  1390. static PyObject *
  1391. FutureIter_iternext(futureiterobject *it)
  1392. {
  1393. PyObject *result;
  1394. switch (FutureIter_am_send(it, Py_None, &result)) {
  1395. case PYGEN_RETURN:
  1396. (void)_PyGen_SetStopIterationValue(result);
  1397. Py_DECREF(result);
  1398. return NULL;
  1399. case PYGEN_NEXT:
  1400. return result;
  1401. case PYGEN_ERROR:
  1402. return NULL;
  1403. default:
  1404. Py_UNREACHABLE();
  1405. }
  1406. }
  1407. static PyObject *
  1408. FutureIter_send(futureiterobject *self, PyObject *unused)
  1409. {
  1410. /* Future.__iter__ doesn't care about values that are pushed to the
  1411. * generator, it just returns self.result().
  1412. */
  1413. return FutureIter_iternext(self);
  1414. }
  1415. static PyObject *
  1416. FutureIter_throw(futureiterobject *self, PyObject *const *args, Py_ssize_t nargs)
  1417. {
  1418. PyObject *type, *val = NULL, *tb = NULL;
  1419. if (!_PyArg_CheckPositional("throw", nargs, 1, 3)) {
  1420. return NULL;
  1421. }
  1422. if (nargs > 1) {
  1423. if (PyErr_WarnEx(PyExc_DeprecationWarning,
  1424. "the (type, exc, tb) signature of throw() is deprecated, "
  1425. "use the single-arg signature instead.",
  1426. 1) < 0) {
  1427. return NULL;
  1428. }
  1429. }
  1430. type = args[0];
  1431. if (nargs == 3) {
  1432. val = args[1];
  1433. tb = args[2];
  1434. }
  1435. else if (nargs == 2) {
  1436. val = args[1];
  1437. }
  1438. if (val == Py_None) {
  1439. val = NULL;
  1440. }
  1441. if (tb == Py_None ) {
  1442. tb = NULL;
  1443. } else if (tb != NULL && !PyTraceBack_Check(tb)) {
  1444. PyErr_SetString(PyExc_TypeError, "throw() third argument must be a traceback");
  1445. return NULL;
  1446. }
  1447. Py_INCREF(type);
  1448. Py_XINCREF(val);
  1449. Py_XINCREF(tb);
  1450. if (PyExceptionClass_Check(type)) {
  1451. PyErr_NormalizeException(&type, &val, &tb);
  1452. /* No need to call PyException_SetTraceback since we'll be calling
  1453. PyErr_Restore for `type`, `val`, and `tb`. */
  1454. } else if (PyExceptionInstance_Check(type)) {
  1455. if (val) {
  1456. PyErr_SetString(PyExc_TypeError,
  1457. "instance exception may not have a separate value");
  1458. goto fail;
  1459. }
  1460. val = type;
  1461. type = PyExceptionInstance_Class(type);
  1462. Py_INCREF(type);
  1463. if (tb == NULL)
  1464. tb = PyException_GetTraceback(val);
  1465. } else {
  1466. PyErr_SetString(PyExc_TypeError,
  1467. "exceptions must be classes deriving BaseException or "
  1468. "instances of such a class");
  1469. goto fail;
  1470. }
  1471. Py_CLEAR(self->future);
  1472. PyErr_Restore(type, val, tb);
  1473. return NULL;
  1474. fail:
  1475. Py_DECREF(type);
  1476. Py_XDECREF(val);
  1477. Py_XDECREF(tb);
  1478. return NULL;
  1479. }
  1480. static int
  1481. FutureIter_clear(futureiterobject *it)
  1482. {
  1483. Py_CLEAR(it->future);
  1484. return 0;
  1485. }
  1486. static PyObject *
  1487. FutureIter_close(futureiterobject *self, PyObject *arg)
  1488. {
  1489. (void)FutureIter_clear(self);
  1490. Py_RETURN_NONE;
  1491. }
  1492. static int
  1493. FutureIter_traverse(futureiterobject *it, visitproc visit, void *arg)
  1494. {
  1495. Py_VISIT(Py_TYPE(it));
  1496. Py_VISIT(it->future);
  1497. return 0;
  1498. }
  1499. static PyMethodDef FutureIter_methods[] = {
  1500. {"send", (PyCFunction)FutureIter_send, METH_O, NULL},
  1501. {"throw", _PyCFunction_CAST(FutureIter_throw), METH_FASTCALL, NULL},
  1502. {"close", (PyCFunction)FutureIter_close, METH_NOARGS, NULL},
  1503. {NULL, NULL} /* Sentinel */
  1504. };
  1505. static PyType_Slot FutureIter_slots[] = {
  1506. {Py_tp_dealloc, (destructor)FutureIter_dealloc},
  1507. {Py_tp_getattro, PyObject_GenericGetAttr},
  1508. {Py_tp_traverse, (traverseproc)FutureIter_traverse},
  1509. {Py_tp_clear, FutureIter_clear},
  1510. {Py_tp_iter, PyObject_SelfIter},
  1511. {Py_tp_iternext, (iternextfunc)FutureIter_iternext},
  1512. {Py_tp_methods, FutureIter_methods},
  1513. // async methods
  1514. {Py_am_send, (sendfunc)FutureIter_am_send},
  1515. {0, NULL},
  1516. };
  1517. static PyType_Spec FutureIter_spec = {
  1518. .name = "_asyncio.FutureIter",
  1519. .basicsize = sizeof(futureiterobject),
  1520. .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
  1521. Py_TPFLAGS_IMMUTABLETYPE),
  1522. .slots = FutureIter_slots,
  1523. };
  1524. static PyObject *
  1525. future_new_iter(PyObject *fut)
  1526. {
  1527. futureiterobject *it;
  1528. asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut);
  1529. ENSURE_FUTURE_ALIVE(state, fut)
  1530. if (state->fi_freelist_len) {
  1531. state->fi_freelist_len--;
  1532. it = state->fi_freelist;
  1533. state->fi_freelist = (futureiterobject*) it->future;
  1534. it->future = NULL;
  1535. _Py_NewReference((PyObject*) it);
  1536. }
  1537. else {
  1538. it = PyObject_GC_New(futureiterobject, state->FutureIterType);
  1539. if (it == NULL) {
  1540. return NULL;
  1541. }
  1542. }
  1543. it->future = (FutureObj*)Py_NewRef(fut);
  1544. PyObject_GC_Track(it);
  1545. return (PyObject*)it;
  1546. }
  1547. /*********************** Task **************************/
  1548. /*[clinic input]
  1549. class _asyncio.Task "TaskObj *" "&Task_Type"
  1550. [clinic start generated code]*/
  1551. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=719dcef0fcc03b37]*/
  1552. static int task_call_step_soon(asyncio_state *state, TaskObj *, PyObject *);
  1553. static PyObject * task_wakeup(TaskObj *, PyObject *);
  1554. static PyObject * task_step(asyncio_state *, TaskObj *, PyObject *);
  1555. static int task_eager_start(asyncio_state *state, TaskObj *task);
  1556. /* ----- Task._step wrapper */
  1557. static int
  1558. TaskStepMethWrapper_clear(TaskStepMethWrapper *o)
  1559. {
  1560. Py_CLEAR(o->sw_task);
  1561. Py_CLEAR(o->sw_arg);
  1562. return 0;
  1563. }
  1564. static void
  1565. TaskStepMethWrapper_dealloc(TaskStepMethWrapper *o)
  1566. {
  1567. PyTypeObject *tp = Py_TYPE(o);
  1568. PyObject_GC_UnTrack(o);
  1569. (void)TaskStepMethWrapper_clear(o);
  1570. Py_TYPE(o)->tp_free(o);
  1571. Py_DECREF(tp);
  1572. }
  1573. static PyObject *
  1574. TaskStepMethWrapper_call(TaskStepMethWrapper *o,
  1575. PyObject *args, PyObject *kwds)
  1576. {
  1577. if (kwds != NULL && PyDict_GET_SIZE(kwds) != 0) {
  1578. PyErr_SetString(PyExc_TypeError, "function takes no keyword arguments");
  1579. return NULL;
  1580. }
  1581. if (args != NULL && PyTuple_GET_SIZE(args) != 0) {
  1582. PyErr_SetString(PyExc_TypeError, "function takes no positional arguments");
  1583. return NULL;
  1584. }
  1585. asyncio_state *state = get_asyncio_state_by_def((PyObject *)o);
  1586. return task_step(state, o->sw_task, o->sw_arg);
  1587. }
  1588. static int
  1589. TaskStepMethWrapper_traverse(TaskStepMethWrapper *o,
  1590. visitproc visit, void *arg)
  1591. {
  1592. Py_VISIT(Py_TYPE(o));
  1593. Py_VISIT(o->sw_task);
  1594. Py_VISIT(o->sw_arg);
  1595. return 0;
  1596. }
  1597. static PyObject *
  1598. TaskStepMethWrapper_get___self__(TaskStepMethWrapper *o, void *Py_UNUSED(ignored))
  1599. {
  1600. if (o->sw_task) {
  1601. return Py_NewRef(o->sw_task);
  1602. }
  1603. Py_RETURN_NONE;
  1604. }
  1605. static PyGetSetDef TaskStepMethWrapper_getsetlist[] = {
  1606. {"__self__", (getter)TaskStepMethWrapper_get___self__, NULL, NULL},
  1607. {NULL} /* Sentinel */
  1608. };
  1609. static PyType_Slot TaskStepMethWrapper_slots[] = {
  1610. {Py_tp_getset, TaskStepMethWrapper_getsetlist},
  1611. {Py_tp_dealloc, (destructor)TaskStepMethWrapper_dealloc},
  1612. {Py_tp_call, (ternaryfunc)TaskStepMethWrapper_call},
  1613. {Py_tp_getattro, PyObject_GenericGetAttr},
  1614. {Py_tp_traverse, (traverseproc)TaskStepMethWrapper_traverse},
  1615. {Py_tp_clear, (inquiry)TaskStepMethWrapper_clear},
  1616. {0, NULL},
  1617. };
  1618. static PyType_Spec TaskStepMethWrapper_spec = {
  1619. .name = "_asyncio.TaskStepMethWrapper",
  1620. .basicsize = sizeof(TaskStepMethWrapper),
  1621. .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
  1622. Py_TPFLAGS_IMMUTABLETYPE),
  1623. .slots = TaskStepMethWrapper_slots,
  1624. };
  1625. static PyObject *
  1626. TaskStepMethWrapper_new(TaskObj *task, PyObject *arg)
  1627. {
  1628. asyncio_state *state = get_asyncio_state_by_def((PyObject *)task);
  1629. TaskStepMethWrapper *o;
  1630. o = PyObject_GC_New(TaskStepMethWrapper, state->TaskStepMethWrapper_Type);
  1631. if (o == NULL) {
  1632. return NULL;
  1633. }
  1634. o->sw_task = (TaskObj*)Py_NewRef(task);
  1635. o->sw_arg = Py_XNewRef(arg);
  1636. PyObject_GC_Track(o);
  1637. return (PyObject*) o;
  1638. }
  1639. /* ----- Task._wakeup implementation */
  1640. static PyMethodDef TaskWakeupDef = {
  1641. "task_wakeup",
  1642. (PyCFunction)task_wakeup,
  1643. METH_O,
  1644. NULL
  1645. };
  1646. /* ----- Task introspection helpers */
  1647. static int
  1648. register_task(asyncio_state *state, PyObject *task)
  1649. {
  1650. PyObject *res = PyObject_CallMethodOneArg(state->scheduled_tasks,
  1651. &_Py_ID(add), task);
  1652. if (res == NULL) {
  1653. return -1;
  1654. }
  1655. Py_DECREF(res);
  1656. return 0;
  1657. }
  1658. static int
  1659. register_eager_task(asyncio_state *state, PyObject *task)
  1660. {
  1661. return PySet_Add(state->eager_tasks, task);
  1662. }
  1663. static int
  1664. unregister_task(asyncio_state *state, PyObject *task)
  1665. {
  1666. PyObject *res = PyObject_CallMethodOneArg(state->scheduled_tasks,
  1667. &_Py_ID(discard), task);
  1668. if (res == NULL) {
  1669. return -1;
  1670. }
  1671. Py_DECREF(res);
  1672. return 0;
  1673. }
  1674. static int
  1675. unregister_eager_task(asyncio_state *state, PyObject *task)
  1676. {
  1677. return PySet_Discard(state->eager_tasks, task);
  1678. }
  1679. static int
  1680. enter_task(asyncio_state *state, PyObject *loop, PyObject *task)
  1681. {
  1682. PyObject *item;
  1683. Py_hash_t hash;
  1684. hash = PyObject_Hash(loop);
  1685. if (hash == -1) {
  1686. return -1;
  1687. }
  1688. item = _PyDict_GetItem_KnownHash(state->current_tasks, loop, hash);
  1689. if (item != NULL) {
  1690. Py_INCREF(item);
  1691. PyErr_Format(
  1692. PyExc_RuntimeError,
  1693. "Cannot enter into task %R while another " \
  1694. "task %R is being executed.",
  1695. task, item, NULL);
  1696. Py_DECREF(item);
  1697. return -1;
  1698. }
  1699. if (PyErr_Occurred()) {
  1700. return -1;
  1701. }
  1702. return _PyDict_SetItem_KnownHash(state->current_tasks, loop, task, hash);
  1703. }
  1704. static int
  1705. leave_task(asyncio_state *state, PyObject *loop, PyObject *task)
  1706. /*[clinic end generated code: output=0ebf6db4b858fb41 input=51296a46313d1ad8]*/
  1707. {
  1708. PyObject *item;
  1709. Py_hash_t hash;
  1710. hash = PyObject_Hash(loop);
  1711. if (hash == -1) {
  1712. return -1;
  1713. }
  1714. item = _PyDict_GetItem_KnownHash(state->current_tasks, loop, hash);
  1715. if (item != task) {
  1716. if (item == NULL) {
  1717. /* Not entered, replace with None */
  1718. item = Py_None;
  1719. }
  1720. PyErr_Format(
  1721. PyExc_RuntimeError,
  1722. "Leaving task %R does not match the current task %R.",
  1723. task, item, NULL);
  1724. return -1;
  1725. }
  1726. return _PyDict_DelItem_KnownHash(state->current_tasks, loop, hash);
  1727. }
  1728. static PyObject *
  1729. swap_current_task(asyncio_state *state, PyObject *loop, PyObject *task)
  1730. {
  1731. PyObject *prev_task;
  1732. Py_hash_t hash;
  1733. hash = PyObject_Hash(loop);
  1734. if (hash == -1) {
  1735. return NULL;
  1736. }
  1737. prev_task = _PyDict_GetItem_KnownHash(state->current_tasks, loop, hash);
  1738. if (prev_task == NULL) {
  1739. if (PyErr_Occurred()) {
  1740. return NULL;
  1741. }
  1742. prev_task = Py_None;
  1743. }
  1744. Py_INCREF(prev_task);
  1745. if (task == Py_None) {
  1746. if (_PyDict_DelItem_KnownHash(state->current_tasks, loop, hash) == -1) {
  1747. goto error;
  1748. }
  1749. } else {
  1750. if (_PyDict_SetItem_KnownHash(state->current_tasks, loop, task, hash) == -1) {
  1751. goto error;
  1752. }
  1753. }
  1754. return prev_task;
  1755. error:
  1756. Py_DECREF(prev_task);
  1757. return NULL;
  1758. }
  1759. /* ----- Task */
  1760. /*[clinic input]
  1761. _asyncio.Task.__init__
  1762. coro: object
  1763. *
  1764. loop: object = None
  1765. name: object = None
  1766. context: object = None
  1767. eager_start: bool = False
  1768. A coroutine wrapped in a Future.
  1769. [clinic start generated code]*/
  1770. static int
  1771. _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop,
  1772. PyObject *name, PyObject *context,
  1773. int eager_start)
  1774. /*[clinic end generated code: output=7aced2d27836f1a1 input=18e3f113a51b829d]*/
  1775. {
  1776. if (future_init((FutureObj*)self, loop)) {
  1777. return -1;
  1778. }
  1779. asyncio_state *state = get_asyncio_state_by_def((PyObject *)self);
  1780. int is_coro = is_coroutine(state, coro);
  1781. if (is_coro == -1) {
  1782. return -1;
  1783. }
  1784. if (is_coro == 0) {
  1785. self->task_log_destroy_pending = 0;
  1786. PyErr_Format(PyExc_TypeError,
  1787. "a coroutine was expected, got %R",
  1788. coro, NULL);
  1789. return -1;
  1790. }
  1791. if (context == Py_None) {
  1792. Py_XSETREF(self->task_context, PyContext_CopyCurrent());
  1793. if (self->task_context == NULL) {
  1794. return -1;
  1795. }
  1796. } else {
  1797. self->task_context = Py_NewRef(context);
  1798. }
  1799. Py_CLEAR(self->task_fut_waiter);
  1800. self->task_must_cancel = 0;
  1801. self->task_log_destroy_pending = 1;
  1802. self->task_num_cancels_requested = 0;
  1803. Py_INCREF(coro);
  1804. Py_XSETREF(self->task_coro, coro);
  1805. if (name == Py_None) {
  1806. // optimization: defer task name formatting
  1807. // store the task counter as PyLong in the name
  1808. // for deferred formatting in get_name
  1809. name = PyLong_FromUnsignedLongLong(++state->task_name_counter);
  1810. } else if (!PyUnicode_CheckExact(name)) {
  1811. name = PyObject_Str(name);
  1812. } else {
  1813. Py_INCREF(name);
  1814. }
  1815. Py_XSETREF(self->task_name, name);
  1816. if (self->task_name == NULL) {
  1817. return -1;
  1818. }
  1819. if (eager_start) {
  1820. PyObject *res = PyObject_CallMethodNoArgs(loop, &_Py_ID(is_running));
  1821. if (res == NULL) {
  1822. return -1;
  1823. }
  1824. int is_loop_running = Py_IsTrue(res);
  1825. Py_DECREF(res);
  1826. if (is_loop_running) {
  1827. if (task_eager_start(state, self)) {
  1828. return -1;
  1829. }
  1830. return 0;
  1831. }
  1832. }
  1833. if (task_call_step_soon(state, self, NULL)) {
  1834. return -1;
  1835. }
  1836. return register_task(state, (PyObject*)self);
  1837. }
  1838. static int
  1839. TaskObj_clear(TaskObj *task)
  1840. {
  1841. (void)FutureObj_clear((FutureObj*) task);
  1842. Py_CLEAR(task->task_context);
  1843. Py_CLEAR(task->task_coro);
  1844. Py_CLEAR(task->task_name);
  1845. Py_CLEAR(task->task_fut_waiter);
  1846. return 0;
  1847. }
  1848. static int
  1849. TaskObj_traverse(TaskObj *task, visitproc visit, void *arg)
  1850. {
  1851. Py_VISIT(Py_TYPE(task));
  1852. Py_VISIT(task->task_context);
  1853. Py_VISIT(task->task_coro);
  1854. Py_VISIT(task->task_name);
  1855. Py_VISIT(task->task_fut_waiter);
  1856. FutureObj *fut = (FutureObj *)task;
  1857. Py_VISIT(fut->fut_loop);
  1858. Py_VISIT(fut->fut_callback0);
  1859. Py_VISIT(fut->fut_context0);
  1860. Py_VISIT(fut->fut_callbacks);
  1861. Py_VISIT(fut->fut_result);
  1862. Py_VISIT(fut->fut_exception);
  1863. Py_VISIT(fut->fut_exception_tb);
  1864. Py_VISIT(fut->fut_source_tb);
  1865. Py_VISIT(fut->fut_cancel_msg);
  1866. Py_VISIT(fut->fut_cancelled_exc);
  1867. Py_VISIT(fut->dict);
  1868. return 0;
  1869. }
  1870. static PyObject *
  1871. TaskObj_get_log_destroy_pending(TaskObj *task, void *Py_UNUSED(ignored))
  1872. {
  1873. if (task->task_log_destroy_pending) {
  1874. Py_RETURN_TRUE;
  1875. }
  1876. else {
  1877. Py_RETURN_FALSE;
  1878. }
  1879. }
  1880. static int
  1881. TaskObj_set_log_destroy_pending(TaskObj *task, PyObject *val, void *Py_UNUSED(ignored))
  1882. {
  1883. if (val == NULL) {
  1884. PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
  1885. return -1;
  1886. }
  1887. int is_true = PyObject_IsTrue(val);
  1888. if (is_true < 0) {
  1889. return -1;
  1890. }
  1891. task->task_log_destroy_pending = is_true;
  1892. return 0;
  1893. }
  1894. static PyObject *
  1895. TaskObj_get_must_cancel(TaskObj *task, void *Py_UNUSED(ignored))
  1896. {
  1897. if (task->task_must_cancel) {
  1898. Py_RETURN_TRUE;
  1899. }
  1900. else {
  1901. Py_RETURN_FALSE;
  1902. }
  1903. }
  1904. static PyObject *
  1905. TaskObj_get_coro(TaskObj *task, void *Py_UNUSED(ignored))
  1906. {
  1907. if (task->task_coro) {
  1908. return Py_NewRef(task->task_coro);
  1909. }
  1910. Py_RETURN_NONE;
  1911. }
  1912. static PyObject *
  1913. TaskObj_get_fut_waiter(TaskObj *task, void *Py_UNUSED(ignored))
  1914. {
  1915. if (task->task_fut_waiter) {
  1916. return Py_NewRef(task->task_fut_waiter);
  1917. }
  1918. Py_RETURN_NONE;
  1919. }
  1920. static PyObject *
  1921. TaskObj_repr(TaskObj *task)
  1922. {
  1923. asyncio_state *state = get_asyncio_state_by_def((PyObject *)task);
  1924. return PyObject_CallOneArg(state->asyncio_task_repr_func,
  1925. (PyObject *)task);
  1926. }
  1927. /*[clinic input]
  1928. _asyncio.Task._make_cancelled_error
  1929. Create the CancelledError to raise if the Task is cancelled.
  1930. This should only be called once when handling a cancellation since
  1931. it erases the context exception value.
  1932. [clinic start generated code]*/
  1933. static PyObject *
  1934. _asyncio_Task__make_cancelled_error_impl(TaskObj *self)
  1935. /*[clinic end generated code: output=55a819e8b4276fab input=52c0e32de8e2f840]*/
  1936. {
  1937. FutureObj *fut = (FutureObj*)self;
  1938. return _asyncio_Future__make_cancelled_error_impl(fut);
  1939. }
  1940. /*[clinic input]
  1941. _asyncio.Task.cancel
  1942. msg: object = None
  1943. Request that this task cancel itself.
  1944. This arranges for a CancelledError to be thrown into the
  1945. wrapped coroutine on the next cycle through the event loop.
  1946. The coroutine then has a chance to clean up or even deny
  1947. the request using try/except/finally.
  1948. Unlike Future.cancel, this does not guarantee that the
  1949. task will be cancelled: the exception might be caught and
  1950. acted upon, delaying cancellation of the task or preventing
  1951. cancellation completely. The task may also return a value or
  1952. raise a different exception.
  1953. Immediately after this method is called, Task.cancelled() will
  1954. not return True (unless the task was already cancelled). A
  1955. task will be marked as cancelled when the wrapped coroutine
  1956. terminates with a CancelledError exception (even if cancel()
  1957. was not called).
  1958. This also increases the task's count of cancellation requests.
  1959. [clinic start generated code]*/
  1960. static PyObject *
  1961. _asyncio_Task_cancel_impl(TaskObj *self, PyObject *msg)
  1962. /*[clinic end generated code: output=c66b60d41c74f9f1 input=7bb51bf25974c783]*/
  1963. {
  1964. self->task_log_tb = 0;
  1965. if (self->task_state != STATE_PENDING) {
  1966. Py_RETURN_FALSE;
  1967. }
  1968. self->task_num_cancels_requested += 1;
  1969. // These three lines are controversial. See discussion starting at
  1970. // https://github.com/python/cpython/pull/31394#issuecomment-1053545331
  1971. // and corresponding code in tasks.py.
  1972. // if (self->task_num_cancels_requested > 1) {
  1973. // Py_RETURN_FALSE;
  1974. // }
  1975. if (self->task_fut_waiter) {
  1976. PyObject *res;
  1977. int is_true;
  1978. res = PyObject_CallMethodOneArg(self->task_fut_waiter,
  1979. &_Py_ID(cancel), msg);
  1980. if (res == NULL) {
  1981. return NULL;
  1982. }
  1983. is_true = PyObject_IsTrue(res);
  1984. Py_DECREF(res);
  1985. if (is_true < 0) {
  1986. return NULL;
  1987. }
  1988. if (is_true) {
  1989. Py_RETURN_TRUE;
  1990. }
  1991. }
  1992. self->task_must_cancel = 1;
  1993. Py_XINCREF(msg);
  1994. Py_XSETREF(self->task_cancel_msg, msg);
  1995. Py_RETURN_TRUE;
  1996. }
  1997. /*[clinic input]
  1998. _asyncio.Task.cancelling
  1999. Return the count of the task's cancellation requests.
  2000. This count is incremented when .cancel() is called
  2001. and may be decremented using .uncancel().
  2002. [clinic start generated code]*/
  2003. static PyObject *
  2004. _asyncio_Task_cancelling_impl(TaskObj *self)
  2005. /*[clinic end generated code: output=803b3af96f917d7e input=b625224d310cbb17]*/
  2006. /*[clinic end generated code]*/
  2007. {
  2008. return PyLong_FromLong(self->task_num_cancels_requested);
  2009. }
  2010. /*[clinic input]
  2011. _asyncio.Task.uncancel
  2012. Decrement the task's count of cancellation requests.
  2013. This should be used by tasks that catch CancelledError
  2014. and wish to continue indefinitely until they are cancelled again.
  2015. Returns the remaining number of cancellation requests.
  2016. [clinic start generated code]*/
  2017. static PyObject *
  2018. _asyncio_Task_uncancel_impl(TaskObj *self)
  2019. /*[clinic end generated code: output=58184d236a817d3c input=68f81a4b90b46be2]*/
  2020. /*[clinic end generated code]*/
  2021. {
  2022. if (self->task_num_cancels_requested > 0) {
  2023. self->task_num_cancels_requested -= 1;
  2024. }
  2025. return PyLong_FromLong(self->task_num_cancels_requested);
  2026. }
  2027. /*[clinic input]
  2028. _asyncio.Task.get_stack
  2029. cls: defining_class
  2030. /
  2031. *
  2032. limit: object = None
  2033. Return the list of stack frames for this task's coroutine.
  2034. If the coroutine is not done, this returns the stack where it is
  2035. suspended. If the coroutine has completed successfully or was
  2036. cancelled, this returns an empty list. If the coroutine was
  2037. terminated by an exception, this returns the list of traceback
  2038. frames.
  2039. The frames are always ordered from oldest to newest.
  2040. The optional limit gives the maximum number of frames to
  2041. return; by default all available frames are returned. Its
  2042. meaning differs depending on whether a stack or a traceback is
  2043. returned: the newest frames of a stack are returned, but the
  2044. oldest frames of a traceback are returned. (This matches the
  2045. behavior of the traceback module.)
  2046. For reasons beyond our control, only one stack frame is
  2047. returned for a suspended coroutine.
  2048. [clinic start generated code]*/
  2049. static PyObject *
  2050. _asyncio_Task_get_stack_impl(TaskObj *self, PyTypeObject *cls,
  2051. PyObject *limit)
  2052. /*[clinic end generated code: output=6774dfc10d3857fa input=8e01c9b2618ae953]*/
  2053. {
  2054. asyncio_state *state = get_asyncio_state_by_cls(cls);
  2055. PyObject *stack[] = {(PyObject *)self, limit};
  2056. return PyObject_Vectorcall(state->asyncio_task_get_stack_func,
  2057. stack, 2, NULL);
  2058. }
  2059. /*[clinic input]
  2060. _asyncio.Task.print_stack
  2061. cls: defining_class
  2062. /
  2063. *
  2064. limit: object = None
  2065. file: object = None
  2066. Print the stack or traceback for this task's coroutine.
  2067. This produces output similar to that of the traceback module,
  2068. for the frames retrieved by get_stack(). The limit argument
  2069. is passed to get_stack(). The file argument is an I/O stream
  2070. to which the output is written; by default output is written
  2071. to sys.stderr.
  2072. [clinic start generated code]*/
  2073. static PyObject *
  2074. _asyncio_Task_print_stack_impl(TaskObj *self, PyTypeObject *cls,
  2075. PyObject *limit, PyObject *file)
  2076. /*[clinic end generated code: output=b38affe9289ec826 input=150b35ba2d3a7dee]*/
  2077. {
  2078. asyncio_state *state = get_asyncio_state_by_cls(cls);
  2079. PyObject *stack[] = {(PyObject *)self, limit, file};
  2080. return PyObject_Vectorcall(state->asyncio_task_print_stack_func,
  2081. stack, 3, NULL);
  2082. }
  2083. /*[clinic input]
  2084. _asyncio.Task.set_result
  2085. result: object
  2086. /
  2087. [clinic start generated code]*/
  2088. static PyObject *
  2089. _asyncio_Task_set_result(TaskObj *self, PyObject *result)
  2090. /*[clinic end generated code: output=1dcae308bfcba318 input=9d1a00c07be41bab]*/
  2091. {
  2092. PyErr_SetString(PyExc_RuntimeError,
  2093. "Task does not support set_result operation");
  2094. return NULL;
  2095. }
  2096. /*[clinic input]
  2097. _asyncio.Task.set_exception
  2098. exception: object
  2099. /
  2100. [clinic start generated code]*/
  2101. static PyObject *
  2102. _asyncio_Task_set_exception(TaskObj *self, PyObject *exception)
  2103. /*[clinic end generated code: output=bc377fc28067303d input=9a8f65c83dcf893a]*/
  2104. {
  2105. PyErr_SetString(PyExc_RuntimeError,
  2106. "Task does not support set_exception operation");
  2107. return NULL;
  2108. }
  2109. /*[clinic input]
  2110. _asyncio.Task.get_coro
  2111. [clinic start generated code]*/
  2112. static PyObject *
  2113. _asyncio_Task_get_coro_impl(TaskObj *self)
  2114. /*[clinic end generated code: output=bcac27c8cc6c8073 input=d2e8606c42a7b403]*/
  2115. {
  2116. if (self->task_coro) {
  2117. return Py_NewRef(self->task_coro);
  2118. }
  2119. Py_RETURN_NONE;
  2120. }
  2121. /*[clinic input]
  2122. _asyncio.Task.get_context
  2123. [clinic start generated code]*/
  2124. static PyObject *
  2125. _asyncio_Task_get_context_impl(TaskObj *self)
  2126. /*[clinic end generated code: output=6996f53d3dc01aef input=87c0b209b8fceeeb]*/
  2127. {
  2128. return Py_NewRef(self->task_context);
  2129. }
  2130. /*[clinic input]
  2131. _asyncio.Task.get_name
  2132. [clinic start generated code]*/
  2133. static PyObject *
  2134. _asyncio_Task_get_name_impl(TaskObj *self)
  2135. /*[clinic end generated code: output=0ecf1570c3b37a8f input=a4a6595d12f4f0f8]*/
  2136. {
  2137. if (self->task_name) {
  2138. if (PyLong_CheckExact(self->task_name)) {
  2139. PyObject *name = PyUnicode_FromFormat("Task-%S", self->task_name);
  2140. if (name == NULL) {
  2141. return NULL;
  2142. }
  2143. Py_SETREF(self->task_name, name);
  2144. }
  2145. return Py_NewRef(self->task_name);
  2146. }
  2147. Py_RETURN_NONE;
  2148. }
  2149. /*[clinic input]
  2150. _asyncio.Task.set_name
  2151. value: object
  2152. /
  2153. [clinic start generated code]*/
  2154. static PyObject *
  2155. _asyncio_Task_set_name(TaskObj *self, PyObject *value)
  2156. /*[clinic end generated code: output=138a8d51e32057d6 input=a8359b6e65f8fd31]*/
  2157. {
  2158. if (!PyUnicode_CheckExact(value)) {
  2159. value = PyObject_Str(value);
  2160. if (value == NULL) {
  2161. return NULL;
  2162. }
  2163. } else {
  2164. Py_INCREF(value);
  2165. }
  2166. Py_XSETREF(self->task_name, value);
  2167. Py_RETURN_NONE;
  2168. }
  2169. static void
  2170. TaskObj_finalize(TaskObj *task)
  2171. {
  2172. PyObject *context;
  2173. PyObject *message = NULL;
  2174. PyObject *func;
  2175. if (task->task_state != STATE_PENDING || !task->task_log_destroy_pending) {
  2176. goto done;
  2177. }
  2178. /* Save the current exception, if any. */
  2179. PyObject *exc = PyErr_GetRaisedException();
  2180. context = PyDict_New();
  2181. if (context == NULL) {
  2182. goto finally;
  2183. }
  2184. message = PyUnicode_FromString("Task was destroyed but it is pending!");
  2185. if (message == NULL) {
  2186. goto finally;
  2187. }
  2188. if (PyDict_SetItem(context, &_Py_ID(message), message) < 0 ||
  2189. PyDict_SetItem(context, &_Py_ID(task), (PyObject*)task) < 0)
  2190. {
  2191. goto finally;
  2192. }
  2193. if (task->task_source_tb != NULL) {
  2194. if (PyDict_SetItem(context, &_Py_ID(source_traceback),
  2195. task->task_source_tb) < 0)
  2196. {
  2197. goto finally;
  2198. }
  2199. }
  2200. func = PyObject_GetAttr(task->task_loop, &_Py_ID(call_exception_handler));
  2201. if (func != NULL) {
  2202. PyObject *res = PyObject_CallOneArg(func, context);
  2203. if (res == NULL) {
  2204. PyErr_WriteUnraisable(func);
  2205. }
  2206. else {
  2207. Py_DECREF(res);
  2208. }
  2209. Py_DECREF(func);
  2210. }
  2211. finally:
  2212. Py_XDECREF(context);
  2213. Py_XDECREF(message);
  2214. /* Restore the saved exception. */
  2215. PyErr_SetRaisedException(exc);
  2216. done:
  2217. FutureObj_finalize((FutureObj*)task);
  2218. }
  2219. static void TaskObj_dealloc(PyObject *); /* Needs Task_CheckExact */
  2220. static PyMethodDef TaskType_methods[] = {
  2221. _ASYNCIO_FUTURE_RESULT_METHODDEF
  2222. _ASYNCIO_FUTURE_EXCEPTION_METHODDEF
  2223. _ASYNCIO_FUTURE_ADD_DONE_CALLBACK_METHODDEF
  2224. _ASYNCIO_FUTURE_REMOVE_DONE_CALLBACK_METHODDEF
  2225. _ASYNCIO_FUTURE_CANCELLED_METHODDEF
  2226. _ASYNCIO_FUTURE_DONE_METHODDEF
  2227. _ASYNCIO_TASK_SET_RESULT_METHODDEF
  2228. _ASYNCIO_TASK_SET_EXCEPTION_METHODDEF
  2229. _ASYNCIO_TASK_CANCEL_METHODDEF
  2230. _ASYNCIO_TASK_CANCELLING_METHODDEF
  2231. _ASYNCIO_TASK_UNCANCEL_METHODDEF
  2232. _ASYNCIO_TASK_GET_STACK_METHODDEF
  2233. _ASYNCIO_TASK_PRINT_STACK_METHODDEF
  2234. _ASYNCIO_TASK__MAKE_CANCELLED_ERROR_METHODDEF
  2235. _ASYNCIO_TASK_GET_NAME_METHODDEF
  2236. _ASYNCIO_TASK_SET_NAME_METHODDEF
  2237. _ASYNCIO_TASK_GET_CORO_METHODDEF
  2238. _ASYNCIO_TASK_GET_CONTEXT_METHODDEF
  2239. {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
  2240. {NULL, NULL} /* Sentinel */
  2241. };
  2242. static PyMemberDef TaskType_members[] = {
  2243. {"__weaklistoffset__", T_PYSSIZET, offsetof(TaskObj, task_weakreflist), READONLY},
  2244. {"__dictoffset__", T_PYSSIZET, offsetof(TaskObj, dict), READONLY},
  2245. {NULL},
  2246. };
  2247. static PyGetSetDef TaskType_getsetlist[] = {
  2248. FUTURE_COMMON_GETSETLIST
  2249. {"_log_destroy_pending", (getter)TaskObj_get_log_destroy_pending,
  2250. (setter)TaskObj_set_log_destroy_pending, NULL},
  2251. {"_must_cancel", (getter)TaskObj_get_must_cancel, NULL, NULL},
  2252. {"_coro", (getter)TaskObj_get_coro, NULL, NULL},
  2253. {"_fut_waiter", (getter)TaskObj_get_fut_waiter, NULL, NULL},
  2254. {NULL} /* Sentinel */
  2255. };
  2256. static PyType_Slot Task_slots[] = {
  2257. {Py_tp_dealloc, TaskObj_dealloc},
  2258. {Py_tp_repr, (reprfunc)TaskObj_repr},
  2259. {Py_tp_doc, (void *)_asyncio_Task___init____doc__},
  2260. {Py_tp_traverse, (traverseproc)TaskObj_traverse},
  2261. {Py_tp_clear, (inquiry)TaskObj_clear},
  2262. {Py_tp_iter, (getiterfunc)future_new_iter},
  2263. {Py_tp_methods, TaskType_methods},
  2264. {Py_tp_members, TaskType_members},
  2265. {Py_tp_getset, TaskType_getsetlist},
  2266. {Py_tp_init, (initproc)_asyncio_Task___init__},
  2267. {Py_tp_new, PyType_GenericNew},
  2268. {Py_tp_finalize, (destructor)TaskObj_finalize},
  2269. // async slots
  2270. {Py_am_await, (unaryfunc)future_new_iter},
  2271. {0, NULL},
  2272. };
  2273. static PyType_Spec Task_spec = {
  2274. .name = "_asyncio.Task",
  2275. .basicsize = sizeof(TaskObj),
  2276. .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE |
  2277. Py_TPFLAGS_IMMUTABLETYPE),
  2278. .slots = Task_slots,
  2279. };
  2280. static void
  2281. TaskObj_dealloc(PyObject *self)
  2282. {
  2283. TaskObj *task = (TaskObj *)self;
  2284. if (PyObject_CallFinalizerFromDealloc(self) < 0) {
  2285. // resurrected.
  2286. return;
  2287. }
  2288. PyTypeObject *tp = Py_TYPE(task);
  2289. PyObject_GC_UnTrack(self);
  2290. if (task->task_weakreflist != NULL) {
  2291. PyObject_ClearWeakRefs(self);
  2292. }
  2293. (void)TaskObj_clear(task);
  2294. tp->tp_free(task);
  2295. Py_DECREF(tp);
  2296. }
  2297. static int
  2298. task_call_step_soon(asyncio_state *state, TaskObj *task, PyObject *arg)
  2299. {
  2300. PyObject *cb = TaskStepMethWrapper_new(task, arg);
  2301. if (cb == NULL) {
  2302. return -1;
  2303. }
  2304. int ret = call_soon(state, task->task_loop, cb, NULL, task->task_context);
  2305. Py_DECREF(cb);
  2306. return ret;
  2307. }
  2308. static PyObject *
  2309. task_set_error_soon(asyncio_state *state, TaskObj *task, PyObject *et,
  2310. const char *format, ...)
  2311. {
  2312. PyObject* msg;
  2313. va_list vargs;
  2314. va_start(vargs, format);
  2315. msg = PyUnicode_FromFormatV(format, vargs);
  2316. va_end(vargs);
  2317. if (msg == NULL) {
  2318. return NULL;
  2319. }
  2320. PyObject *e = PyObject_CallOneArg(et, msg);
  2321. Py_DECREF(msg);
  2322. if (e == NULL) {
  2323. return NULL;
  2324. }
  2325. if (task_call_step_soon(state, task, e) == -1) {
  2326. Py_DECREF(e);
  2327. return NULL;
  2328. }
  2329. Py_DECREF(e);
  2330. Py_RETURN_NONE;
  2331. }
  2332. static inline int
  2333. gen_status_from_result(PyObject **result)
  2334. {
  2335. if (*result != NULL) {
  2336. return PYGEN_NEXT;
  2337. }
  2338. if (_PyGen_FetchStopIterationValue(result) == 0) {
  2339. return PYGEN_RETURN;
  2340. }
  2341. assert(PyErr_Occurred());
  2342. return PYGEN_ERROR;
  2343. }
  2344. static PyObject *
  2345. task_step_impl(asyncio_state *state, TaskObj *task, PyObject *exc)
  2346. {
  2347. int res;
  2348. int clear_exc = 0;
  2349. PyObject *result = NULL;
  2350. PyObject *coro;
  2351. PyObject *o;
  2352. if (task->task_state != STATE_PENDING) {
  2353. PyErr_Format(state->asyncio_InvalidStateError,
  2354. "_step(): already done: %R %R",
  2355. task,
  2356. exc ? exc : Py_None);
  2357. goto fail;
  2358. }
  2359. if (task->task_must_cancel) {
  2360. assert(exc != Py_None);
  2361. if (exc) {
  2362. /* Check if exc is a CancelledError */
  2363. res = PyObject_IsInstance(exc, state->asyncio_CancelledError);
  2364. if (res == -1) {
  2365. /* An error occurred, abort */
  2366. goto fail;
  2367. }
  2368. if (res == 0) {
  2369. /* exc is not CancelledError; reset it to NULL */
  2370. exc = NULL;
  2371. }
  2372. }
  2373. if (!exc) {
  2374. /* exc was not a CancelledError */
  2375. exc = create_cancelled_error(state, (FutureObj*)task);
  2376. if (!exc) {
  2377. goto fail;
  2378. }
  2379. clear_exc = 1;
  2380. }
  2381. task->task_must_cancel = 0;
  2382. }
  2383. Py_CLEAR(task->task_fut_waiter);
  2384. coro = task->task_coro;
  2385. if (coro == NULL) {
  2386. PyErr_SetString(PyExc_RuntimeError, "uninitialized Task object");
  2387. if (clear_exc) {
  2388. /* We created 'exc' during this call */
  2389. Py_DECREF(exc);
  2390. }
  2391. return NULL;
  2392. }
  2393. int gen_status = PYGEN_ERROR;
  2394. if (exc == NULL) {
  2395. gen_status = PyIter_Send(coro, Py_None, &result);
  2396. }
  2397. else {
  2398. result = PyObject_CallMethodOneArg(coro, &_Py_ID(throw), exc);
  2399. gen_status = gen_status_from_result(&result);
  2400. if (clear_exc) {
  2401. /* We created 'exc' during this call */
  2402. Py_DECREF(exc);
  2403. }
  2404. }
  2405. if (gen_status == PYGEN_RETURN || gen_status == PYGEN_ERROR) {
  2406. if (result != NULL) {
  2407. /* The error is StopIteration and that means that
  2408. the underlying coroutine has resolved */
  2409. PyObject *tmp;
  2410. if (task->task_must_cancel) {
  2411. // Task is cancelled right before coro stops.
  2412. task->task_must_cancel = 0;
  2413. tmp = future_cancel(state, (FutureObj*)task,
  2414. task->task_cancel_msg);
  2415. }
  2416. else {
  2417. tmp = future_set_result(state, (FutureObj*)task, result);
  2418. }
  2419. Py_DECREF(result);
  2420. if (tmp == NULL) {
  2421. return NULL;
  2422. }
  2423. Py_DECREF(tmp);
  2424. Py_RETURN_NONE;
  2425. }
  2426. if (PyErr_ExceptionMatches(state->asyncio_CancelledError)) {
  2427. /* CancelledError */
  2428. PyObject *exc = PyErr_GetRaisedException();
  2429. assert(exc);
  2430. FutureObj *fut = (FutureObj*)task;
  2431. /* transfer ownership */
  2432. fut->fut_cancelled_exc = exc;
  2433. return future_cancel(state, fut, NULL);
  2434. }
  2435. /* Some other exception; pop it and call Task.set_exception() */
  2436. PyObject *exc = PyErr_GetRaisedException();
  2437. assert(exc);
  2438. o = future_set_exception(state, (FutureObj*)task, exc);
  2439. if (!o) {
  2440. /* An exception in Task.set_exception() */
  2441. Py_DECREF(exc);
  2442. goto fail;
  2443. }
  2444. assert(o == Py_None);
  2445. Py_DECREF(o);
  2446. if (PyErr_GivenExceptionMatches(exc, PyExc_KeyboardInterrupt) ||
  2447. PyErr_GivenExceptionMatches(exc, PyExc_SystemExit))
  2448. {
  2449. /* We've got a KeyboardInterrupt or a SystemError; re-raise it */
  2450. PyErr_SetRaisedException(exc);
  2451. goto fail;
  2452. }
  2453. Py_DECREF(exc);
  2454. Py_RETURN_NONE;
  2455. }
  2456. PyObject *ret = task_step_handle_result_impl(state, task, result);
  2457. return ret;
  2458. fail:
  2459. return NULL;
  2460. }
  2461. static PyObject *
  2462. task_step_handle_result_impl(asyncio_state *state, TaskObj *task, PyObject *result)
  2463. {
  2464. int res;
  2465. PyObject *o;
  2466. if (result == (PyObject*)task) {
  2467. /* We have a task that wants to await on itself */
  2468. goto self_await;
  2469. }
  2470. /* Check if `result` is FutureObj or TaskObj (and not a subclass) */
  2471. if (Future_CheckExact(state, result) || Task_CheckExact(state, result)) {
  2472. PyObject *wrapper;
  2473. PyObject *tmp;
  2474. FutureObj *fut = (FutureObj*)result;
  2475. /* Check if `result` future is attached to a different loop */
  2476. if (fut->fut_loop != task->task_loop) {
  2477. goto different_loop;
  2478. }
  2479. if (!fut->fut_blocking) {
  2480. goto yield_insteadof_yf;
  2481. }
  2482. fut->fut_blocking = 0;
  2483. /* result.add_done_callback(task._wakeup) */
  2484. wrapper = PyCFunction_New(&TaskWakeupDef, (PyObject *)task);
  2485. if (wrapper == NULL) {
  2486. goto fail;
  2487. }
  2488. tmp = future_add_done_callback(state,
  2489. (FutureObj*)result, wrapper, task->task_context);
  2490. Py_DECREF(wrapper);
  2491. if (tmp == NULL) {
  2492. goto fail;
  2493. }
  2494. Py_DECREF(tmp);
  2495. /* task._fut_waiter = result */
  2496. task->task_fut_waiter = result; /* no incref is necessary */
  2497. if (task->task_must_cancel) {
  2498. PyObject *r;
  2499. int is_true;
  2500. r = PyObject_CallMethodOneArg(result, &_Py_ID(cancel),
  2501. task->task_cancel_msg);
  2502. if (r == NULL) {
  2503. return NULL;
  2504. }
  2505. is_true = PyObject_IsTrue(r);
  2506. Py_DECREF(r);
  2507. if (is_true < 0) {
  2508. return NULL;
  2509. }
  2510. else if (is_true) {
  2511. task->task_must_cancel = 0;
  2512. }
  2513. }
  2514. Py_RETURN_NONE;
  2515. }
  2516. /* Check if `result` is None */
  2517. if (result == Py_None) {
  2518. /* Bare yield relinquishes control for one event loop iteration. */
  2519. if (task_call_step_soon(state, task, NULL)) {
  2520. goto fail;
  2521. }
  2522. return result;
  2523. }
  2524. /* Check if `result` is a Future-compatible object */
  2525. if (_PyObject_LookupAttr(result, &_Py_ID(_asyncio_future_blocking), &o) < 0) {
  2526. goto fail;
  2527. }
  2528. if (o != NULL && o != Py_None) {
  2529. /* `result` is a Future-compatible object */
  2530. PyObject *wrapper;
  2531. PyObject *tmp;
  2532. int blocking = PyObject_IsTrue(o);
  2533. Py_DECREF(o);
  2534. if (blocking < 0) {
  2535. goto fail;
  2536. }
  2537. /* Check if `result` future is attached to a different loop */
  2538. PyObject *oloop = get_future_loop(state, result);
  2539. if (oloop == NULL) {
  2540. goto fail;
  2541. }
  2542. if (oloop != task->task_loop) {
  2543. Py_DECREF(oloop);
  2544. goto different_loop;
  2545. }
  2546. Py_DECREF(oloop);
  2547. if (!blocking) {
  2548. goto yield_insteadof_yf;
  2549. }
  2550. /* result._asyncio_future_blocking = False */
  2551. if (PyObject_SetAttr(
  2552. result, &_Py_ID(_asyncio_future_blocking), Py_False) == -1) {
  2553. goto fail;
  2554. }
  2555. wrapper = PyCFunction_New(&TaskWakeupDef, (PyObject *)task);
  2556. if (wrapper == NULL) {
  2557. goto fail;
  2558. }
  2559. /* result.add_done_callback(task._wakeup) */
  2560. PyObject *add_cb = PyObject_GetAttr(
  2561. result, &_Py_ID(add_done_callback));
  2562. if (add_cb == NULL) {
  2563. Py_DECREF(wrapper);
  2564. goto fail;
  2565. }
  2566. PyObject *stack[2];
  2567. stack[0] = wrapper;
  2568. stack[1] = (PyObject *)task->task_context;
  2569. EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, add_cb);
  2570. tmp = PyObject_Vectorcall(add_cb, stack, 1, state->context_kwname);
  2571. Py_DECREF(add_cb);
  2572. Py_DECREF(wrapper);
  2573. if (tmp == NULL) {
  2574. goto fail;
  2575. }
  2576. Py_DECREF(tmp);
  2577. /* task._fut_waiter = result */
  2578. task->task_fut_waiter = result; /* no incref is necessary */
  2579. if (task->task_must_cancel) {
  2580. PyObject *r;
  2581. int is_true;
  2582. r = PyObject_CallMethodOneArg(result, &_Py_ID(cancel),
  2583. task->task_cancel_msg);
  2584. if (r == NULL) {
  2585. return NULL;
  2586. }
  2587. is_true = PyObject_IsTrue(r);
  2588. Py_DECREF(r);
  2589. if (is_true < 0) {
  2590. return NULL;
  2591. }
  2592. else if (is_true) {
  2593. task->task_must_cancel = 0;
  2594. }
  2595. }
  2596. Py_RETURN_NONE;
  2597. }
  2598. Py_XDECREF(o);
  2599. /* Check if `result` is a generator */
  2600. res = PyObject_IsInstance(result, (PyObject*)&PyGen_Type);
  2601. if (res < 0) {
  2602. goto fail;
  2603. }
  2604. if (res) {
  2605. /* `result` is a generator */
  2606. o = task_set_error_soon(
  2607. state, task, PyExc_RuntimeError,
  2608. "yield was used instead of yield from for "
  2609. "generator in task %R with %R", task, result);
  2610. Py_DECREF(result);
  2611. return o;
  2612. }
  2613. /* The `result` is none of the above */
  2614. o = task_set_error_soon(
  2615. state, task, PyExc_RuntimeError, "Task got bad yield: %R", result);
  2616. Py_DECREF(result);
  2617. return o;
  2618. self_await:
  2619. o = task_set_error_soon(
  2620. state, task, PyExc_RuntimeError,
  2621. "Task cannot await on itself: %R", task);
  2622. Py_DECREF(result);
  2623. return o;
  2624. yield_insteadof_yf:
  2625. o = task_set_error_soon(
  2626. state, task, PyExc_RuntimeError,
  2627. "yield was used instead of yield from "
  2628. "in task %R with %R",
  2629. task, result);
  2630. Py_DECREF(result);
  2631. return o;
  2632. different_loop:
  2633. o = task_set_error_soon(
  2634. state, task, PyExc_RuntimeError,
  2635. "Task %R got Future %R attached to a different loop",
  2636. task, result);
  2637. Py_DECREF(result);
  2638. return o;
  2639. fail:
  2640. Py_XDECREF(result);
  2641. return NULL;
  2642. }
  2643. static PyObject *
  2644. task_step(asyncio_state *state, TaskObj *task, PyObject *exc)
  2645. {
  2646. PyObject *res;
  2647. if (enter_task(state, task->task_loop, (PyObject*)task) < 0) {
  2648. return NULL;
  2649. }
  2650. res = task_step_impl(state, task, exc);
  2651. if (res == NULL) {
  2652. PyObject *exc = PyErr_GetRaisedException();
  2653. leave_task(state, task->task_loop, (PyObject*)task);
  2654. _PyErr_ChainExceptions1(exc);
  2655. return NULL;
  2656. }
  2657. else {
  2658. if (leave_task(state, task->task_loop, (PyObject*)task) < 0) {
  2659. Py_DECREF(res);
  2660. return NULL;
  2661. }
  2662. else {
  2663. return res;
  2664. }
  2665. }
  2666. }
  2667. static int
  2668. task_eager_start(asyncio_state *state, TaskObj *task)
  2669. {
  2670. assert(task != NULL);
  2671. PyObject *prevtask = swap_current_task(state, task->task_loop, (PyObject *)task);
  2672. if (prevtask == NULL) {
  2673. return -1;
  2674. }
  2675. if (register_eager_task(state, (PyObject *)task) == -1) {
  2676. Py_DECREF(prevtask);
  2677. return -1;
  2678. }
  2679. if (PyContext_Enter(task->task_context) == -1) {
  2680. Py_DECREF(prevtask);
  2681. return -1;
  2682. }
  2683. int retval = 0;
  2684. PyObject *stepres = task_step_impl(state, task, NULL);
  2685. if (stepres == NULL) {
  2686. PyObject *exc = PyErr_GetRaisedException();
  2687. _PyErr_ChainExceptions1(exc);
  2688. retval = -1;
  2689. } else {
  2690. Py_DECREF(stepres);
  2691. }
  2692. PyObject *curtask = swap_current_task(state, task->task_loop, prevtask);
  2693. Py_DECREF(prevtask);
  2694. if (curtask == NULL) {
  2695. retval = -1;
  2696. } else {
  2697. assert(curtask == (PyObject *)task);
  2698. Py_DECREF(curtask);
  2699. }
  2700. if (unregister_eager_task(state, (PyObject *)task) == -1) {
  2701. retval = -1;
  2702. }
  2703. if (PyContext_Exit(task->task_context) == -1) {
  2704. retval = -1;
  2705. }
  2706. if (task->task_state == STATE_PENDING) {
  2707. if (register_task(state, (PyObject *)task) == -1) {
  2708. retval = -1;
  2709. }
  2710. } else {
  2711. // This seems to really help performance on pyperformance benchmarks
  2712. Py_CLEAR(task->task_coro);
  2713. }
  2714. return retval;
  2715. }
  2716. static PyObject *
  2717. task_wakeup(TaskObj *task, PyObject *o)
  2718. {
  2719. PyObject *result;
  2720. assert(o);
  2721. asyncio_state *state = get_asyncio_state_by_def((PyObject *)task);
  2722. if (Future_CheckExact(state, o) || Task_CheckExact(state, o)) {
  2723. PyObject *fut_result = NULL;
  2724. int res = future_get_result(state, (FutureObj*)o, &fut_result);
  2725. switch(res) {
  2726. case -1:
  2727. assert(fut_result == NULL);
  2728. break; /* exception raised */
  2729. case 0:
  2730. Py_DECREF(fut_result);
  2731. return task_step(state, task, NULL);
  2732. default:
  2733. assert(res == 1);
  2734. result = task_step(state, task, fut_result);
  2735. Py_DECREF(fut_result);
  2736. return result;
  2737. }
  2738. }
  2739. else {
  2740. PyObject *fut_result = PyObject_CallMethod(o, "result", NULL);
  2741. if (fut_result != NULL) {
  2742. Py_DECREF(fut_result);
  2743. return task_step(state, task, NULL);
  2744. }
  2745. /* exception raised */
  2746. }
  2747. PyObject *exc = PyErr_GetRaisedException();
  2748. assert(exc);
  2749. result = task_step(state, task, exc);
  2750. Py_DECREF(exc);
  2751. return result;
  2752. }
  2753. /*********************** Functions **************************/
  2754. /*[clinic input]
  2755. _asyncio._get_running_loop
  2756. Return the running event loop or None.
  2757. This is a low-level function intended to be used by event loops.
  2758. This function is thread-specific.
  2759. [clinic start generated code]*/
  2760. static PyObject *
  2761. _asyncio__get_running_loop_impl(PyObject *module)
  2762. /*[clinic end generated code: output=b4390af721411a0a input=0a21627e25a4bd43]*/
  2763. {
  2764. PyObject *loop;
  2765. asyncio_state *state = get_asyncio_state(module);
  2766. if (get_running_loop(state, &loop)) {
  2767. return NULL;
  2768. }
  2769. if (loop == NULL) {
  2770. /* There's no currently running event loop */
  2771. Py_RETURN_NONE;
  2772. }
  2773. return loop;
  2774. }
  2775. /*[clinic input]
  2776. _asyncio._set_running_loop
  2777. loop: 'O'
  2778. /
  2779. Set the running event loop.
  2780. This is a low-level function intended to be used by event loops.
  2781. This function is thread-specific.
  2782. [clinic start generated code]*/
  2783. static PyObject *
  2784. _asyncio__set_running_loop(PyObject *module, PyObject *loop)
  2785. /*[clinic end generated code: output=ae56bf7a28ca189a input=4c9720233d606604]*/
  2786. {
  2787. asyncio_state *state = get_asyncio_state(module);
  2788. if (set_running_loop(state, loop)) {
  2789. return NULL;
  2790. }
  2791. Py_RETURN_NONE;
  2792. }
  2793. /*[clinic input]
  2794. _asyncio.get_event_loop
  2795. Return an asyncio event loop.
  2796. When called from a coroutine or a callback (e.g. scheduled with
  2797. call_soon or similar API), this function will always return the
  2798. running event loop.
  2799. If there is no running event loop set, the function will return
  2800. the result of `get_event_loop_policy().get_event_loop()` call.
  2801. [clinic start generated code]*/
  2802. static PyObject *
  2803. _asyncio_get_event_loop_impl(PyObject *module)
  2804. /*[clinic end generated code: output=2a2d8b2f824c648b input=9364bf2916c8655d]*/
  2805. {
  2806. asyncio_state *state = get_asyncio_state(module);
  2807. return get_event_loop(state);
  2808. }
  2809. /*[clinic input]
  2810. _asyncio.get_running_loop
  2811. Return the running event loop. Raise a RuntimeError if there is none.
  2812. This function is thread-specific.
  2813. [clinic start generated code]*/
  2814. static PyObject *
  2815. _asyncio_get_running_loop_impl(PyObject *module)
  2816. /*[clinic end generated code: output=c247b5f9e529530e input=2a3bf02ba39f173d]*/
  2817. {
  2818. PyObject *loop;
  2819. asyncio_state *state = get_asyncio_state(module);
  2820. if (get_running_loop(state, &loop)) {
  2821. return NULL;
  2822. }
  2823. if (loop == NULL) {
  2824. /* There's no currently running event loop */
  2825. PyErr_SetString(
  2826. PyExc_RuntimeError, "no running event loop");
  2827. }
  2828. return loop;
  2829. }
  2830. /*[clinic input]
  2831. _asyncio._register_task
  2832. task: object
  2833. Register a new task in asyncio as executed by loop.
  2834. Returns None.
  2835. [clinic start generated code]*/
  2836. static PyObject *
  2837. _asyncio__register_task_impl(PyObject *module, PyObject *task)
  2838. /*[clinic end generated code: output=8672dadd69a7d4e2 input=21075aaea14dfbad]*/
  2839. {
  2840. asyncio_state *state = get_asyncio_state(module);
  2841. if (register_task(state, task) < 0) {
  2842. return NULL;
  2843. }
  2844. Py_RETURN_NONE;
  2845. }
  2846. /*[clinic input]
  2847. _asyncio._register_eager_task
  2848. task: object
  2849. Register a new task in asyncio as executed by loop.
  2850. Returns None.
  2851. [clinic start generated code]*/
  2852. static PyObject *
  2853. _asyncio__register_eager_task_impl(PyObject *module, PyObject *task)
  2854. /*[clinic end generated code: output=dfe1d45367c73f1a input=237f684683398c51]*/
  2855. {
  2856. asyncio_state *state = get_asyncio_state(module);
  2857. if (register_eager_task(state, task) < 0) {
  2858. return NULL;
  2859. }
  2860. Py_RETURN_NONE;
  2861. }
  2862. /*[clinic input]
  2863. _asyncio._unregister_task
  2864. task: object
  2865. Unregister a task.
  2866. Returns None.
  2867. [clinic start generated code]*/
  2868. static PyObject *
  2869. _asyncio__unregister_task_impl(PyObject *module, PyObject *task)
  2870. /*[clinic end generated code: output=6e5585706d568a46 input=28fb98c3975f7bdc]*/
  2871. {
  2872. asyncio_state *state = get_asyncio_state(module);
  2873. if (unregister_task(state, task) < 0) {
  2874. return NULL;
  2875. }
  2876. Py_RETURN_NONE;
  2877. }
  2878. /*[clinic input]
  2879. _asyncio._unregister_eager_task
  2880. task: object
  2881. Unregister a task.
  2882. Returns None.
  2883. [clinic start generated code]*/
  2884. static PyObject *
  2885. _asyncio__unregister_eager_task_impl(PyObject *module, PyObject *task)
  2886. /*[clinic end generated code: output=a426922bd07f23d1 input=9d07401ef14ee048]*/
  2887. {
  2888. asyncio_state *state = get_asyncio_state(module);
  2889. if (unregister_eager_task(state, task) < 0) {
  2890. return NULL;
  2891. }
  2892. Py_RETURN_NONE;
  2893. }
  2894. /*[clinic input]
  2895. _asyncio._enter_task
  2896. loop: object
  2897. task: object
  2898. Enter into task execution or resume suspended task.
  2899. Task belongs to loop.
  2900. Returns None.
  2901. [clinic start generated code]*/
  2902. static PyObject *
  2903. _asyncio__enter_task_impl(PyObject *module, PyObject *loop, PyObject *task)
  2904. /*[clinic end generated code: output=a22611c858035b73 input=de1b06dca70d8737]*/
  2905. {
  2906. asyncio_state *state = get_asyncio_state(module);
  2907. if (enter_task(state, loop, task) < 0) {
  2908. return NULL;
  2909. }
  2910. Py_RETURN_NONE;
  2911. }
  2912. /*[clinic input]
  2913. _asyncio._leave_task
  2914. loop: object
  2915. task: object
  2916. Leave task execution or suspend a task.
  2917. Task belongs to loop.
  2918. Returns None.
  2919. [clinic start generated code]*/
  2920. static PyObject *
  2921. _asyncio__leave_task_impl(PyObject *module, PyObject *loop, PyObject *task)
  2922. /*[clinic end generated code: output=0ebf6db4b858fb41 input=51296a46313d1ad8]*/
  2923. {
  2924. asyncio_state *state = get_asyncio_state(module);
  2925. if (leave_task(state, loop, task) < 0) {
  2926. return NULL;
  2927. }
  2928. Py_RETURN_NONE;
  2929. }
  2930. /*[clinic input]
  2931. _asyncio._swap_current_task
  2932. loop: object
  2933. task: object
  2934. Temporarily swap in the supplied task and return the original one (or None).
  2935. This is intended for use during eager coroutine execution.
  2936. [clinic start generated code]*/
  2937. static PyObject *
  2938. _asyncio__swap_current_task_impl(PyObject *module, PyObject *loop,
  2939. PyObject *task)
  2940. /*[clinic end generated code: output=9f88de958df74c7e input=c9c72208d3d38b6c]*/
  2941. {
  2942. return swap_current_task(get_asyncio_state(module), loop, task);
  2943. }
  2944. /*[clinic input]
  2945. _asyncio.current_task
  2946. loop: object = None
  2947. Return a currently executed task.
  2948. [clinic start generated code]*/
  2949. static PyObject *
  2950. _asyncio_current_task_impl(PyObject *module, PyObject *loop)
  2951. /*[clinic end generated code: output=fe15ac331a7f981a input=58910f61a5627112]*/
  2952. {
  2953. PyObject *ret;
  2954. asyncio_state *state = get_asyncio_state(module);
  2955. if (loop == Py_None) {
  2956. loop = _asyncio_get_running_loop_impl(module);
  2957. if (loop == NULL) {
  2958. return NULL;
  2959. }
  2960. } else {
  2961. Py_INCREF(loop);
  2962. }
  2963. ret = PyDict_GetItemWithError(state->current_tasks, loop);
  2964. Py_DECREF(loop);
  2965. if (ret == NULL && PyErr_Occurred()) {
  2966. return NULL;
  2967. }
  2968. else if (ret == NULL) {
  2969. Py_RETURN_NONE;
  2970. }
  2971. Py_INCREF(ret);
  2972. return ret;
  2973. }
  2974. /*********************** Module **************************/
  2975. static void
  2976. module_free_freelists(asyncio_state *state)
  2977. {
  2978. PyObject *next;
  2979. PyObject *current;
  2980. next = (PyObject*) state->fi_freelist;
  2981. while (next != NULL) {
  2982. assert(state->fi_freelist_len > 0);
  2983. state->fi_freelist_len--;
  2984. current = next;
  2985. next = (PyObject*) ((futureiterobject*) current)->future;
  2986. PyObject_GC_Del(current);
  2987. }
  2988. assert(state->fi_freelist_len == 0);
  2989. state->fi_freelist = NULL;
  2990. }
  2991. static int
  2992. module_traverse(PyObject *mod, visitproc visit, void *arg)
  2993. {
  2994. asyncio_state *state = get_asyncio_state(mod);
  2995. Py_VISIT(state->FutureIterType);
  2996. Py_VISIT(state->TaskStepMethWrapper_Type);
  2997. Py_VISIT(state->FutureType);
  2998. Py_VISIT(state->TaskType);
  2999. Py_VISIT(state->asyncio_mod);
  3000. Py_VISIT(state->traceback_extract_stack);
  3001. Py_VISIT(state->asyncio_future_repr_func);
  3002. Py_VISIT(state->asyncio_get_event_loop_policy);
  3003. Py_VISIT(state->asyncio_iscoroutine_func);
  3004. Py_VISIT(state->asyncio_task_get_stack_func);
  3005. Py_VISIT(state->asyncio_task_print_stack_func);
  3006. Py_VISIT(state->asyncio_task_repr_func);
  3007. Py_VISIT(state->asyncio_InvalidStateError);
  3008. Py_VISIT(state->asyncio_CancelledError);
  3009. Py_VISIT(state->scheduled_tasks);
  3010. Py_VISIT(state->eager_tasks);
  3011. Py_VISIT(state->current_tasks);
  3012. Py_VISIT(state->iscoroutine_typecache);
  3013. Py_VISIT(state->context_kwname);
  3014. return 0;
  3015. }
  3016. static int
  3017. module_clear(PyObject *mod)
  3018. {
  3019. asyncio_state *state = get_asyncio_state(mod);
  3020. Py_CLEAR(state->FutureIterType);
  3021. Py_CLEAR(state->TaskStepMethWrapper_Type);
  3022. Py_CLEAR(state->FutureType);
  3023. Py_CLEAR(state->TaskType);
  3024. Py_CLEAR(state->asyncio_mod);
  3025. Py_CLEAR(state->traceback_extract_stack);
  3026. Py_CLEAR(state->asyncio_future_repr_func);
  3027. Py_CLEAR(state->asyncio_get_event_loop_policy);
  3028. Py_CLEAR(state->asyncio_iscoroutine_func);
  3029. Py_CLEAR(state->asyncio_task_get_stack_func);
  3030. Py_CLEAR(state->asyncio_task_print_stack_func);
  3031. Py_CLEAR(state->asyncio_task_repr_func);
  3032. Py_CLEAR(state->asyncio_InvalidStateError);
  3033. Py_CLEAR(state->asyncio_CancelledError);
  3034. Py_CLEAR(state->scheduled_tasks);
  3035. Py_CLEAR(state->eager_tasks);
  3036. Py_CLEAR(state->current_tasks);
  3037. Py_CLEAR(state->iscoroutine_typecache);
  3038. Py_CLEAR(state->context_kwname);
  3039. module_free_freelists(state);
  3040. return 0;
  3041. }
  3042. static void
  3043. module_free(void *mod)
  3044. {
  3045. (void)module_clear((PyObject *)mod);
  3046. }
  3047. static int
  3048. module_init(asyncio_state *state)
  3049. {
  3050. PyObject *module = NULL;
  3051. state->asyncio_mod = PyImport_ImportModule("asyncio");
  3052. if (state->asyncio_mod == NULL) {
  3053. goto fail;
  3054. }
  3055. state->current_tasks = PyDict_New();
  3056. if (state->current_tasks == NULL) {
  3057. goto fail;
  3058. }
  3059. state->iscoroutine_typecache = PySet_New(NULL);
  3060. if (state->iscoroutine_typecache == NULL) {
  3061. goto fail;
  3062. }
  3063. state->context_kwname = Py_BuildValue("(s)", "context");
  3064. if (state->context_kwname == NULL) {
  3065. goto fail;
  3066. }
  3067. #define WITH_MOD(NAME) \
  3068. Py_CLEAR(module); \
  3069. module = PyImport_ImportModule(NAME); \
  3070. if (module == NULL) { \
  3071. goto fail; \
  3072. }
  3073. #define GET_MOD_ATTR(VAR, NAME) \
  3074. VAR = PyObject_GetAttrString(module, NAME); \
  3075. if (VAR == NULL) { \
  3076. goto fail; \
  3077. }
  3078. WITH_MOD("asyncio.events")
  3079. GET_MOD_ATTR(state->asyncio_get_event_loop_policy, "get_event_loop_policy")
  3080. WITH_MOD("asyncio.base_futures")
  3081. GET_MOD_ATTR(state->asyncio_future_repr_func, "_future_repr")
  3082. WITH_MOD("asyncio.exceptions")
  3083. GET_MOD_ATTR(state->asyncio_InvalidStateError, "InvalidStateError")
  3084. GET_MOD_ATTR(state->asyncio_CancelledError, "CancelledError")
  3085. WITH_MOD("asyncio.base_tasks")
  3086. GET_MOD_ATTR(state->asyncio_task_repr_func, "_task_repr")
  3087. GET_MOD_ATTR(state->asyncio_task_get_stack_func, "_task_get_stack")
  3088. GET_MOD_ATTR(state->asyncio_task_print_stack_func, "_task_print_stack")
  3089. WITH_MOD("asyncio.coroutines")
  3090. GET_MOD_ATTR(state->asyncio_iscoroutine_func, "iscoroutine")
  3091. WITH_MOD("traceback")
  3092. GET_MOD_ATTR(state->traceback_extract_stack, "extract_stack")
  3093. PyObject *weak_set;
  3094. WITH_MOD("weakref")
  3095. GET_MOD_ATTR(weak_set, "WeakSet");
  3096. state->scheduled_tasks = PyObject_CallNoArgs(weak_set);
  3097. Py_CLEAR(weak_set);
  3098. if (state->scheduled_tasks == NULL) {
  3099. goto fail;
  3100. }
  3101. state->eager_tasks = PySet_New(NULL);
  3102. if (state->eager_tasks == NULL) {
  3103. goto fail;
  3104. }
  3105. Py_DECREF(module);
  3106. return 0;
  3107. fail:
  3108. Py_CLEAR(module);
  3109. return -1;
  3110. #undef WITH_MOD
  3111. #undef GET_MOD_ATTR
  3112. }
  3113. PyDoc_STRVAR(module_doc, "Accelerator module for asyncio");
  3114. static PyMethodDef asyncio_methods[] = {
  3115. _ASYNCIO_CURRENT_TASK_METHODDEF
  3116. _ASYNCIO_GET_EVENT_LOOP_METHODDEF
  3117. _ASYNCIO_GET_RUNNING_LOOP_METHODDEF
  3118. _ASYNCIO__GET_RUNNING_LOOP_METHODDEF
  3119. _ASYNCIO__SET_RUNNING_LOOP_METHODDEF
  3120. _ASYNCIO__REGISTER_TASK_METHODDEF
  3121. _ASYNCIO__REGISTER_EAGER_TASK_METHODDEF
  3122. _ASYNCIO__UNREGISTER_TASK_METHODDEF
  3123. _ASYNCIO__UNREGISTER_EAGER_TASK_METHODDEF
  3124. _ASYNCIO__ENTER_TASK_METHODDEF
  3125. _ASYNCIO__LEAVE_TASK_METHODDEF
  3126. _ASYNCIO__SWAP_CURRENT_TASK_METHODDEF
  3127. {NULL, NULL}
  3128. };
  3129. static int
  3130. module_exec(PyObject *mod)
  3131. {
  3132. asyncio_state *state = get_asyncio_state(mod);
  3133. #define CREATE_TYPE(m, tp, spec, base) \
  3134. do { \
  3135. tp = (PyTypeObject *)PyType_FromMetaclass(NULL, m, spec, \
  3136. (PyObject *)base); \
  3137. if (tp == NULL) { \
  3138. return -1; \
  3139. } \
  3140. } while (0)
  3141. CREATE_TYPE(mod, state->TaskStepMethWrapper_Type, &TaskStepMethWrapper_spec, NULL);
  3142. CREATE_TYPE(mod, state->FutureIterType, &FutureIter_spec, NULL);
  3143. CREATE_TYPE(mod, state->FutureType, &Future_spec, NULL);
  3144. CREATE_TYPE(mod, state->TaskType, &Task_spec, state->FutureType);
  3145. #undef CREATE_TYPE
  3146. if (PyModule_AddType(mod, state->FutureType) < 0) {
  3147. return -1;
  3148. }
  3149. if (PyModule_AddType(mod, state->TaskType) < 0) {
  3150. return -1;
  3151. }
  3152. // Must be done after types are added to avoid a circular dependency
  3153. if (module_init(state) < 0) {
  3154. return -1;
  3155. }
  3156. if (PyModule_AddObjectRef(mod, "_scheduled_tasks", state->scheduled_tasks) < 0) {
  3157. return -1;
  3158. }
  3159. if (PyModule_AddObjectRef(mod, "_eager_tasks", state->eager_tasks) < 0) {
  3160. return -1;
  3161. }
  3162. if (PyModule_AddObjectRef(mod, "_current_tasks", state->current_tasks) < 0) {
  3163. return -1;
  3164. }
  3165. return 0;
  3166. }
  3167. static struct PyModuleDef_Slot module_slots[] = {
  3168. {Py_mod_exec, module_exec},
  3169. {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
  3170. {0, NULL},
  3171. };
  3172. static struct PyModuleDef _asynciomodule = {
  3173. .m_base = PyModuleDef_HEAD_INIT,
  3174. .m_name = "_asyncio",
  3175. .m_doc = module_doc,
  3176. .m_size = sizeof(asyncio_state),
  3177. .m_methods = asyncio_methods,
  3178. .m_slots = module_slots,
  3179. .m_traverse = module_traverse,
  3180. .m_clear = module_clear,
  3181. .m_free = (freefunc)module_free,
  3182. };
  3183. PyMODINIT_FUNC
  3184. PyInit__asyncio(void)
  3185. {
  3186. return PyModuleDef_Init(&_asynciomodule);
  3187. }