_asynciomodule.c 103 KB

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