1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868 |
- #ifndef Py_BUILD_CORE_BUILTIN
- # define Py_BUILD_CORE_MODULE 1
- #endif
- #include "Python.h"
- #include "pycore_pyerrors.h" // _PyErr_ClearExcState()
- #include "pycore_pystate.h" // _PyThreadState_GET()
- #include "pycore_runtime_init.h" // _Py_ID()
- #include "pycore_moduleobject.h" // _PyModule_GetState()
- #include "structmember.h" // PyMemberDef
- #include <stddef.h> // offsetof()
- /*[clinic input]
- module _asyncio
- [clinic start generated code]*/
- /*[clinic end generated code: output=da39a3ee5e6b4b0d input=8fd17862aa989c69]*/
- #define FI_FREELIST_MAXLEN 255
- typedef struct futureiterobject futureiterobject;
- /* State of the _asyncio module */
- typedef struct {
- PyTypeObject *FutureIterType;
- PyTypeObject *TaskStepMethWrapper_Type;
- PyTypeObject *FutureType;
- PyTypeObject *TaskType;
- PyObject *asyncio_mod;
- PyObject *context_kwname;
- /* Dictionary containing tasks that are currently active in
- all running event loops. {EventLoop: Task} */
- PyObject *current_tasks;
- /* WeakSet containing all tasks scheduled to run on event loops. */
- PyObject *scheduled_tasks;
- /* Set containing all eagerly executing tasks. */
- PyObject *eager_tasks;
- /* An isinstance type cache for the 'is_coroutine()' function. */
- PyObject *iscoroutine_typecache;
- /* Imports from asyncio.events. */
- PyObject *asyncio_get_event_loop_policy;
- /* Imports from asyncio.base_futures. */
- PyObject *asyncio_future_repr_func;
- /* Imports from asyncio.exceptions. */
- PyObject *asyncio_CancelledError;
- PyObject *asyncio_InvalidStateError;
- /* Imports from asyncio.base_tasks. */
- PyObject *asyncio_task_get_stack_func;
- PyObject *asyncio_task_print_stack_func;
- PyObject *asyncio_task_repr_func;
- /* Imports from asyncio.coroutines. */
- PyObject *asyncio_iscoroutine_func;
- /* Imports from traceback. */
- PyObject *traceback_extract_stack;
- PyObject *cached_running_loop; // Borrowed reference
- volatile uint64_t cached_running_loop_tsid;
- /* Counter for autogenerated Task names */
- uint64_t task_name_counter;
- futureiterobject *fi_freelist;
- Py_ssize_t fi_freelist_len;
- } asyncio_state;
- static inline asyncio_state *
- get_asyncio_state(PyObject *mod)
- {
- asyncio_state *state = _PyModule_GetState(mod);
- assert(state != NULL);
- return state;
- }
- static inline asyncio_state *
- get_asyncio_state_by_cls(PyTypeObject *cls)
- {
- asyncio_state *state = (asyncio_state *)_PyType_GetModuleState(cls);
- assert(state != NULL);
- return state;
- }
- static struct PyModuleDef _asynciomodule;
- static inline asyncio_state *
- get_asyncio_state_by_def(PyObject *self)
- {
- PyTypeObject *tp = Py_TYPE(self);
- PyObject *mod = PyType_GetModuleByDef(tp, &_asynciomodule);
- assert(mod != NULL);
- return get_asyncio_state(mod);
- }
- typedef enum {
- STATE_PENDING,
- STATE_CANCELLED,
- STATE_FINISHED
- } fut_state;
- #define FutureObj_HEAD(prefix) \
- PyObject_HEAD \
- PyObject *prefix##_loop; \
- PyObject *prefix##_callback0; \
- PyObject *prefix##_context0; \
- PyObject *prefix##_callbacks; \
- PyObject *prefix##_exception; \
- PyObject *prefix##_exception_tb; \
- PyObject *prefix##_result; \
- PyObject *prefix##_source_tb; \
- PyObject *prefix##_cancel_msg; \
- fut_state prefix##_state; \
- int prefix##_log_tb; \
- int prefix##_blocking; \
- PyObject *dict; \
- PyObject *prefix##_weakreflist; \
- PyObject *prefix##_cancelled_exc;
- typedef struct {
- FutureObj_HEAD(fut)
- } FutureObj;
- typedef struct {
- FutureObj_HEAD(task)
- PyObject *task_fut_waiter;
- PyObject *task_coro;
- PyObject *task_name;
- PyObject *task_context;
- int task_must_cancel;
- int task_log_destroy_pending;
- int task_num_cancels_requested;
- } TaskObj;
- typedef struct {
- PyObject_HEAD
- TaskObj *sw_task;
- PyObject *sw_arg;
- } TaskStepMethWrapper;
- #define Future_CheckExact(state, obj) Py_IS_TYPE(obj, state->FutureType)
- #define Task_CheckExact(state, obj) Py_IS_TYPE(obj, state->TaskType)
- #define Future_Check(state, obj) PyObject_TypeCheck(obj, state->FutureType)
- #define Task_Check(state, obj) PyObject_TypeCheck(obj, state->TaskType)
- #include "clinic/_asynciomodule.c.h"
- /*[clinic input]
- class _asyncio.Future "FutureObj *" "&Future_Type"
- [clinic start generated code]*/
- /*[clinic end generated code: output=da39a3ee5e6b4b0d input=00d3e4abca711e0f]*/
- /* Get FutureIter from Future */
- static PyObject * future_new_iter(PyObject *);
- static PyObject *
- task_step_handle_result_impl(asyncio_state *state, TaskObj *task, PyObject *result);
- static int
- _is_coroutine(asyncio_state *state, PyObject *coro)
- {
- /* 'coro' is not a native coroutine, call asyncio.iscoroutine()
- to check if it's another coroutine flavour.
- Do this check after 'future_init()'; in case we need to raise
- an error, __del__ needs a properly initialized object.
- */
- PyObject *res = PyObject_CallOneArg(state->asyncio_iscoroutine_func, coro);
- if (res == NULL) {
- return -1;
- }
- int is_res_true = PyObject_IsTrue(res);
- Py_DECREF(res);
- if (is_res_true <= 0) {
- return is_res_true;
- }
- if (PySet_GET_SIZE(state->iscoroutine_typecache) < 100) {
- /* Just in case we don't want to cache more than 100
- positive types. That shouldn't ever happen, unless
- someone stressing the system on purpose.
- */
- if (PySet_Add(state->iscoroutine_typecache, (PyObject*) Py_TYPE(coro))) {
- return -1;
- }
- }
- return 1;
- }
- static inline int
- is_coroutine(asyncio_state *state, PyObject *coro)
- {
- if (PyCoro_CheckExact(coro)) {
- return 1;
- }
- /* Check if `type(coro)` is in the cache.
- Caching makes is_coroutine() function almost as fast as
- PyCoro_CheckExact() for non-native coroutine-like objects
- (like coroutines compiled with Cython).
- asyncio.iscoroutine() has its own type caching mechanism.
- This cache allows us to avoid the cost of even calling
- a pure-Python function in 99.9% cases.
- */
- int has_it = PySet_Contains(
- state->iscoroutine_typecache, (PyObject*) Py_TYPE(coro));
- if (has_it == 0) {
- /* type(coro) is not in iscoroutine_typecache */
- return _is_coroutine(state, coro);
- }
- /* either an error has occurred or
- type(coro) is in iscoroutine_typecache
- */
- return has_it;
- }
- static PyObject *
- get_future_loop(asyncio_state *state, PyObject *fut)
- {
- /* Implementation of `asyncio.futures._get_loop` */
- PyObject *getloop;
- if (Future_CheckExact(state, fut) || Task_CheckExact(state, fut)) {
- PyObject *loop = ((FutureObj *)fut)->fut_loop;
- return Py_NewRef(loop);
- }
- if (_PyObject_LookupAttr(fut, &_Py_ID(get_loop), &getloop) < 0) {
- return NULL;
- }
- if (getloop != NULL) {
- PyObject *res = PyObject_CallNoArgs(getloop);
- Py_DECREF(getloop);
- return res;
- }
- return PyObject_GetAttr(fut, &_Py_ID(_loop));
- }
- static int
- get_running_loop(asyncio_state *state, PyObject **loop)
- {
- PyObject *rl;
- PyThreadState *ts = _PyThreadState_GET();
- uint64_t ts_id = PyThreadState_GetID(ts);
- if (state->cached_running_loop_tsid == ts_id &&
- state->cached_running_loop != NULL)
- {
- // Fast path, check the cache.
- rl = state->cached_running_loop;
- }
- else {
- PyObject *ts_dict = _PyThreadState_GetDict(ts); // borrowed
- if (ts_dict == NULL) {
- goto not_found;
- }
- rl = PyDict_GetItemWithError(
- ts_dict, &_Py_ID(__asyncio_running_event_loop__)); // borrowed
- if (rl == NULL) {
- if (PyErr_Occurred()) {
- goto error;
- }
- else {
- goto not_found;
- }
- }
- state->cached_running_loop = rl;
- state->cached_running_loop_tsid = ts_id;
- }
- if (rl == Py_None) {
- goto not_found;
- }
- *loop = Py_NewRef(rl);
- return 0;
- not_found:
- *loop = NULL;
- return 0;
- error:
- *loop = NULL;
- return -1;
- }
- static int
- set_running_loop(asyncio_state *state, PyObject *loop)
- {
- PyObject *ts_dict = NULL;
- PyThreadState *tstate = _PyThreadState_GET();
- if (tstate != NULL) {
- ts_dict = _PyThreadState_GetDict(tstate); // borrowed
- }
- if (ts_dict == NULL) {
- PyErr_SetString(
- PyExc_RuntimeError, "thread-local storage is not available");
- return -1;
- }
- if (PyDict_SetItem(
- ts_dict, &_Py_ID(__asyncio_running_event_loop__), loop) < 0)
- {
- return -1;
- }
- state->cached_running_loop = loop; // borrowed, kept alive by ts_dict
- state->cached_running_loop_tsid = PyThreadState_GetID(tstate);
- return 0;
- }
- static PyObject *
- get_event_loop(asyncio_state *state)
- {
- PyObject *loop;
- PyObject *policy;
- if (get_running_loop(state, &loop)) {
- return NULL;
- }
- if (loop != NULL) {
- return loop;
- }
- policy = PyObject_CallNoArgs(state->asyncio_get_event_loop_policy);
- if (policy == NULL) {
- return NULL;
- }
- loop = PyObject_CallMethodNoArgs(policy, &_Py_ID(get_event_loop));
- Py_DECREF(policy);
- return loop;
- }
- static int
- call_soon(asyncio_state *state, PyObject *loop, PyObject *func, PyObject *arg,
- PyObject *ctx)
- {
- PyObject *handle;
- if (ctx == NULL) {
- PyObject *stack[] = {loop, func, arg};
- size_t nargsf = 3 | PY_VECTORCALL_ARGUMENTS_OFFSET;
- handle = PyObject_VectorcallMethod(&_Py_ID(call_soon), stack, nargsf, NULL);
- }
- else {
- /* All refs in 'stack' are borrowed. */
- PyObject *stack[4];
- size_t nargs = 2;
- stack[0] = loop;
- stack[1] = func;
- if (arg != NULL) {
- stack[2] = arg;
- nargs++;
- }
- stack[nargs] = (PyObject *)ctx;
- size_t nargsf = nargs | PY_VECTORCALL_ARGUMENTS_OFFSET;
- handle = PyObject_VectorcallMethod(&_Py_ID(call_soon), stack, nargsf,
- state->context_kwname);
- }
- if (handle == NULL) {
- return -1;
- }
- Py_DECREF(handle);
- return 0;
- }
- static inline int
- future_is_alive(FutureObj *fut)
- {
- return fut->fut_loop != NULL;
- }
- static inline int
- future_ensure_alive(FutureObj *fut)
- {
- if (!future_is_alive(fut)) {
- PyErr_SetString(PyExc_RuntimeError,
- "Future object is not initialized.");
- return -1;
- }
- return 0;
- }
- #define ENSURE_FUTURE_ALIVE(state, fut) \
- do { \
- assert(Future_Check(state, fut) || Task_Check(state, fut)); \
- (void)state; \
- if (future_ensure_alive((FutureObj*)fut)) { \
- return NULL; \
- } \
- } while(0);
- static int
- future_schedule_callbacks(asyncio_state *state, FutureObj *fut)
- {
- if (fut->fut_callback0 != NULL) {
- /* There's a 1st callback */
- // Beware: An evil call_soon could alter fut_callback0 or fut_context0.
- // Since we are anyway clearing them after the call, whether call_soon
- // succeeds or not, the idea is to transfer ownership so that external
- // code is not able to alter them during the call.
- PyObject *fut_callback0 = fut->fut_callback0;
- fut->fut_callback0 = NULL;
- PyObject *fut_context0 = fut->fut_context0;
- fut->fut_context0 = NULL;
- int ret = call_soon(state, fut->fut_loop, fut_callback0,
- (PyObject *)fut, fut_context0);
- Py_CLEAR(fut_callback0);
- Py_CLEAR(fut_context0);
- if (ret) {
- /* If an error occurs in pure-Python implementation,
- all callbacks are cleared. */
- Py_CLEAR(fut->fut_callbacks);
- return ret;
- }
- /* we called the first callback, now try calling
- callbacks from the 'fut_callbacks' list. */
- }
- if (fut->fut_callbacks == NULL) {
- /* No more callbacks, return. */
- return 0;
- }
- // Beware: An evil call_soon could change fut->fut_callbacks.
- // The idea is to transfer the ownership of the callbacks list
- // so that external code is not able to mutate the list during
- // the iteration.
- PyObject *callbacks = fut->fut_callbacks;
- fut->fut_callbacks = NULL;
- Py_ssize_t n = PyList_GET_SIZE(callbacks);
- for (Py_ssize_t i = 0; i < n; i++) {
- assert(PyList_GET_SIZE(callbacks) == n);
- PyObject *cb_tup = PyList_GET_ITEM(callbacks, i);
- PyObject *cb = PyTuple_GET_ITEM(cb_tup, 0);
- PyObject *ctx = PyTuple_GET_ITEM(cb_tup, 1);
- if (call_soon(state, fut->fut_loop, cb, (PyObject *)fut, ctx)) {
- Py_DECREF(callbacks);
- return -1;
- }
- }
- Py_DECREF(callbacks);
- return 0;
- }
- static int
- future_init(FutureObj *fut, PyObject *loop)
- {
- PyObject *res;
- int is_true;
- // Same to FutureObj_clear() but not clearing fut->dict
- Py_CLEAR(fut->fut_loop);
- Py_CLEAR(fut->fut_callback0);
- Py_CLEAR(fut->fut_context0);
- Py_CLEAR(fut->fut_callbacks);
- Py_CLEAR(fut->fut_result);
- Py_CLEAR(fut->fut_exception);
- Py_CLEAR(fut->fut_exception_tb);
- Py_CLEAR(fut->fut_source_tb);
- Py_CLEAR(fut->fut_cancel_msg);
- Py_CLEAR(fut->fut_cancelled_exc);
- fut->fut_state = STATE_PENDING;
- fut->fut_log_tb = 0;
- fut->fut_blocking = 0;
- if (loop == Py_None) {
- asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut);
- loop = get_event_loop(state);
- if (loop == NULL) {
- return -1;
- }
- }
- else {
- Py_INCREF(loop);
- }
- fut->fut_loop = loop;
- res = PyObject_CallMethodNoArgs(fut->fut_loop, &_Py_ID(get_debug));
- if (res == NULL) {
- return -1;
- }
- is_true = PyObject_IsTrue(res);
- Py_DECREF(res);
- if (is_true < 0) {
- return -1;
- }
- if (is_true && !_Py_IsInterpreterFinalizing(PyInterpreterState_Get())) {
- /* Only try to capture the traceback if the interpreter is not being
- finalized. The original motivation to add a `_Py_IsFinalizing()`
- call was to prevent SIGSEGV when a Future is created in a __del__
- method, which is called during the interpreter shutdown and the
- traceback module is already unloaded.
- */
- asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut);
- fut->fut_source_tb = PyObject_CallNoArgs(state->traceback_extract_stack);
- if (fut->fut_source_tb == NULL) {
- return -1;
- }
- }
- return 0;
- }
- static PyObject *
- future_set_result(asyncio_state *state, FutureObj *fut, PyObject *res)
- {
- if (future_ensure_alive(fut)) {
- return NULL;
- }
- if (fut->fut_state != STATE_PENDING) {
- PyErr_SetString(state->asyncio_InvalidStateError, "invalid state");
- return NULL;
- }
- assert(!fut->fut_result);
- fut->fut_result = Py_NewRef(res);
- fut->fut_state = STATE_FINISHED;
- if (future_schedule_callbacks(state, fut) == -1) {
- return NULL;
- }
- Py_RETURN_NONE;
- }
- static PyObject *
- future_set_exception(asyncio_state *state, FutureObj *fut, PyObject *exc)
- {
- PyObject *exc_val = NULL;
- if (fut->fut_state != STATE_PENDING) {
- PyErr_SetString(state->asyncio_InvalidStateError, "invalid state");
- return NULL;
- }
- if (PyExceptionClass_Check(exc)) {
- exc_val = PyObject_CallNoArgs(exc);
- if (exc_val == NULL) {
- return NULL;
- }
- if (fut->fut_state != STATE_PENDING) {
- Py_DECREF(exc_val);
- PyErr_SetString(state->asyncio_InvalidStateError, "invalid state");
- return NULL;
- }
- }
- else {
- exc_val = Py_NewRef(exc);
- }
- if (!PyExceptionInstance_Check(exc_val)) {
- Py_DECREF(exc_val);
- PyErr_SetString(PyExc_TypeError, "invalid exception object");
- return NULL;
- }
- if (PyErr_GivenExceptionMatches(exc_val, PyExc_StopIteration)) {
- const char *msg = "StopIteration interacts badly with "
- "generators and cannot be raised into a "
- "Future";
- PyObject *message = PyUnicode_FromString(msg);
- if (message == NULL) {
- Py_DECREF(exc_val);
- return NULL;
- }
- PyObject *err = PyObject_CallOneArg(PyExc_RuntimeError, message);
- Py_DECREF(message);
- if (err == NULL) {
- Py_DECREF(exc_val);
- return NULL;
- }
- assert(PyExceptionInstance_Check(err));
- PyException_SetCause(err, Py_NewRef(exc_val));
- PyException_SetContext(err, Py_NewRef(exc_val));
- Py_DECREF(exc_val);
- exc_val = err;
- }
- assert(!fut->fut_exception);
- assert(!fut->fut_exception_tb);
- fut->fut_exception = exc_val;
- fut->fut_exception_tb = PyException_GetTraceback(exc_val);
- fut->fut_state = STATE_FINISHED;
- if (future_schedule_callbacks(state, fut) == -1) {
- return NULL;
- }
- fut->fut_log_tb = 1;
- Py_RETURN_NONE;
- }
- static PyObject *
- create_cancelled_error(asyncio_state *state, FutureObj *fut)
- {
- PyObject *exc;
- if (fut->fut_cancelled_exc != NULL) {
- /* transfer ownership */
- exc = fut->fut_cancelled_exc;
- fut->fut_cancelled_exc = NULL;
- return exc;
- }
- PyObject *msg = fut->fut_cancel_msg;
- if (msg == NULL || msg == Py_None) {
- exc = PyObject_CallNoArgs(state->asyncio_CancelledError);
- } else {
- exc = PyObject_CallOneArg(state->asyncio_CancelledError, msg);
- }
- return exc;
- }
- static void
- future_set_cancelled_error(asyncio_state *state, FutureObj *fut)
- {
- PyObject *exc = create_cancelled_error(state, fut);
- if (exc == NULL) {
- return;
- }
- PyErr_SetObject(state->asyncio_CancelledError, exc);
- Py_DECREF(exc);
- }
- static int
- future_get_result(asyncio_state *state, FutureObj *fut, PyObject **result)
- {
- if (fut->fut_state == STATE_CANCELLED) {
- future_set_cancelled_error(state, fut);
- return -1;
- }
- if (fut->fut_state != STATE_FINISHED) {
- PyErr_SetString(state->asyncio_InvalidStateError,
- "Result is not set.");
- return -1;
- }
- fut->fut_log_tb = 0;
- if (fut->fut_exception != NULL) {
- PyObject *tb = fut->fut_exception_tb;
- if (tb == NULL) {
- tb = Py_None;
- }
- if (PyException_SetTraceback(fut->fut_exception, tb) < 0) {
- return -1;
- }
- *result = Py_NewRef(fut->fut_exception);
- Py_CLEAR(fut->fut_exception_tb);
- return 1;
- }
- *result = Py_NewRef(fut->fut_result);
- return 0;
- }
- static PyObject *
- future_add_done_callback(asyncio_state *state, FutureObj *fut, PyObject *arg,
- PyObject *ctx)
- {
- if (!future_is_alive(fut)) {
- PyErr_SetString(PyExc_RuntimeError, "uninitialized Future object");
- return NULL;
- }
- if (fut->fut_state != STATE_PENDING) {
- /* The future is done/cancelled, so schedule the callback
- right away. */
- if (call_soon(state, fut->fut_loop, arg, (PyObject*) fut, ctx)) {
- return NULL;
- }
- }
- else {
- /* The future is pending, add a callback.
- Callbacks in the future object are stored as follows:
- callback0 -- a pointer to the first callback
- callbacks -- a list of 2nd, 3rd, ... callbacks
- Invariants:
- * callbacks != NULL:
- There are some callbacks in in the list. Just
- add the new callback to it.
- * callbacks == NULL and callback0 == NULL:
- This is the first callback. Set it to callback0.
- * callbacks == NULL and callback0 != NULL:
- This is a second callback. Initialize callbacks
- with a new list and add the new callback to it.
- */
- if (fut->fut_callbacks == NULL && fut->fut_callback0 == NULL) {
- fut->fut_callback0 = Py_NewRef(arg);
- fut->fut_context0 = Py_NewRef(ctx);
- }
- else {
- PyObject *tup = PyTuple_New(2);
- if (tup == NULL) {
- return NULL;
- }
- Py_INCREF(arg);
- PyTuple_SET_ITEM(tup, 0, arg);
- Py_INCREF(ctx);
- PyTuple_SET_ITEM(tup, 1, (PyObject *)ctx);
- if (fut->fut_callbacks != NULL) {
- int err = PyList_Append(fut->fut_callbacks, tup);
- if (err) {
- Py_DECREF(tup);
- return NULL;
- }
- Py_DECREF(tup);
- }
- else {
- fut->fut_callbacks = PyList_New(1);
- if (fut->fut_callbacks == NULL) {
- Py_DECREF(tup);
- return NULL;
- }
- PyList_SET_ITEM(fut->fut_callbacks, 0, tup); /* borrow */
- }
- }
- }
- Py_RETURN_NONE;
- }
- static PyObject *
- future_cancel(asyncio_state *state, FutureObj *fut, PyObject *msg)
- {
- fut->fut_log_tb = 0;
- if (fut->fut_state != STATE_PENDING) {
- Py_RETURN_FALSE;
- }
- fut->fut_state = STATE_CANCELLED;
- Py_XINCREF(msg);
- Py_XSETREF(fut->fut_cancel_msg, msg);
- if (future_schedule_callbacks(state, fut) == -1) {
- return NULL;
- }
- Py_RETURN_TRUE;
- }
- /*[clinic input]
- _asyncio.Future.__init__
- *
- loop: object = None
- This class is *almost* compatible with concurrent.futures.Future.
- Differences:
- - result() and exception() do not take a timeout argument and
- raise an exception when the future isn't done yet.
- - Callbacks registered with add_done_callback() are always called
- via the event loop's call_soon_threadsafe().
- - This class is not compatible with the wait() and as_completed()
- methods in the concurrent.futures package.
- [clinic start generated code]*/
- static int
- _asyncio_Future___init___impl(FutureObj *self, PyObject *loop)
- /*[clinic end generated code: output=9ed75799eaccb5d6 input=89af317082bc0bf8]*/
- {
- return future_init(self, loop);
- }
- static int
- FutureObj_clear(FutureObj *fut)
- {
- Py_CLEAR(fut->fut_loop);
- Py_CLEAR(fut->fut_callback0);
- Py_CLEAR(fut->fut_context0);
- Py_CLEAR(fut->fut_callbacks);
- Py_CLEAR(fut->fut_result);
- Py_CLEAR(fut->fut_exception);
- Py_CLEAR(fut->fut_exception_tb);
- Py_CLEAR(fut->fut_source_tb);
- Py_CLEAR(fut->fut_cancel_msg);
- Py_CLEAR(fut->fut_cancelled_exc);
- Py_CLEAR(fut->dict);
- return 0;
- }
- static int
- FutureObj_traverse(FutureObj *fut, visitproc visit, void *arg)
- {
- Py_VISIT(Py_TYPE(fut));
- Py_VISIT(fut->fut_loop);
- Py_VISIT(fut->fut_callback0);
- Py_VISIT(fut->fut_context0);
- Py_VISIT(fut->fut_callbacks);
- Py_VISIT(fut->fut_result);
- Py_VISIT(fut->fut_exception);
- Py_VISIT(fut->fut_exception_tb);
- Py_VISIT(fut->fut_source_tb);
- Py_VISIT(fut->fut_cancel_msg);
- Py_VISIT(fut->fut_cancelled_exc);
- Py_VISIT(fut->dict);
- return 0;
- }
- /*[clinic input]
- _asyncio.Future.result
- Return the result this future represents.
- If the future has been cancelled, raises CancelledError. If the
- future's result isn't yet available, raises InvalidStateError. If
- the future is done and has an exception set, this exception is raised.
- [clinic start generated code]*/
- static PyObject *
- _asyncio_Future_result_impl(FutureObj *self)
- /*[clinic end generated code: output=f35f940936a4b1e5 input=49ecf9cf5ec50dc5]*/
- {
- asyncio_state *state = get_asyncio_state_by_def((PyObject *)self);
- PyObject *result;
- if (!future_is_alive(self)) {
- PyErr_SetString(state->asyncio_InvalidStateError,
- "Future object is not initialized.");
- return NULL;
- }
- int res = future_get_result(state, self, &result);
- if (res == -1) {
- return NULL;
- }
- if (res == 0) {
- return result;
- }
- assert(res == 1);
- PyErr_SetObject(PyExceptionInstance_Class(result), result);
- Py_DECREF(result);
- return NULL;
- }
- /*[clinic input]
- _asyncio.Future.exception
- cls: defining_class
- /
- Return the exception that was set on this future.
- The exception (or None if no exception was set) is returned only if
- the future is done. If the future has been cancelled, raises
- CancelledError. If the future isn't done yet, raises
- InvalidStateError.
- [clinic start generated code]*/
- static PyObject *
- _asyncio_Future_exception_impl(FutureObj *self, PyTypeObject *cls)
- /*[clinic end generated code: output=ce75576b187c905b input=3faf15c22acdb60d]*/
- {
- if (!future_is_alive(self)) {
- asyncio_state *state = get_asyncio_state_by_cls(cls);
- PyErr_SetString(state->asyncio_InvalidStateError,
- "Future object is not initialized.");
- return NULL;
- }
- if (self->fut_state == STATE_CANCELLED) {
- asyncio_state *state = get_asyncio_state_by_cls(cls);
- future_set_cancelled_error(state, self);
- return NULL;
- }
- if (self->fut_state != STATE_FINISHED) {
- asyncio_state *state = get_asyncio_state_by_cls(cls);
- PyErr_SetString(state->asyncio_InvalidStateError,
- "Exception is not set.");
- return NULL;
- }
- if (self->fut_exception != NULL) {
- self->fut_log_tb = 0;
- return Py_NewRef(self->fut_exception);
- }
- Py_RETURN_NONE;
- }
- /*[clinic input]
- _asyncio.Future.set_result
- cls: defining_class
- result: object
- /
- Mark the future done and set its result.
- If the future is already done when this method is called, raises
- InvalidStateError.
- [clinic start generated code]*/
- static PyObject *
- _asyncio_Future_set_result_impl(FutureObj *self, PyTypeObject *cls,
- PyObject *result)
- /*[clinic end generated code: output=99afbbe78f99c32d input=d5a41c1e353acc2e]*/
- {
- asyncio_state *state = get_asyncio_state_by_cls(cls);
- ENSURE_FUTURE_ALIVE(state, self)
- return future_set_result(state, self, result);
- }
- /*[clinic input]
- _asyncio.Future.set_exception
- cls: defining_class
- exception: object
- /
- Mark the future done and set an exception.
- If the future is already done when this method is called, raises
- InvalidStateError.
- [clinic start generated code]*/
- static PyObject *
- _asyncio_Future_set_exception_impl(FutureObj *self, PyTypeObject *cls,
- PyObject *exception)
- /*[clinic end generated code: output=0a5e8b5a52f058d6 input=a245cd49d3df939b]*/
- {
- asyncio_state *state = get_asyncio_state_by_cls(cls);
- ENSURE_FUTURE_ALIVE(state, self)
- return future_set_exception(state, self, exception);
- }
- /*[clinic input]
- _asyncio.Future.add_done_callback
- cls: defining_class
- fn: object
- /
- *
- context: object = NULL
- Add a callback to be run when the future becomes done.
- The callback is called with a single argument - the future object. If
- the future is already done when this is called, the callback is
- scheduled with call_soon.
- [clinic start generated code]*/
- static PyObject *
- _asyncio_Future_add_done_callback_impl(FutureObj *self, PyTypeObject *cls,
- PyObject *fn, PyObject *context)
- /*[clinic end generated code: output=922e9a4cbd601167 input=599261c521458cc2]*/
- {
- asyncio_state *state = get_asyncio_state_by_cls(cls);
- if (context == NULL) {
- context = PyContext_CopyCurrent();
- if (context == NULL) {
- return NULL;
- }
- PyObject *res = future_add_done_callback(state, self, fn, context);
- Py_DECREF(context);
- return res;
- }
- return future_add_done_callback(state, self, fn, context);
- }
- /*[clinic input]
- _asyncio.Future.remove_done_callback
- cls: defining_class
- fn: object
- /
- Remove all instances of a callback from the "call when done" list.
- Returns the number of callbacks removed.
- [clinic start generated code]*/
- static PyObject *
- _asyncio_Future_remove_done_callback_impl(FutureObj *self, PyTypeObject *cls,
- PyObject *fn)
- /*[clinic end generated code: output=2da35ccabfe41b98 input=c7518709b86fc747]*/
- {
- PyObject *newlist;
- Py_ssize_t len, i, j=0;
- Py_ssize_t cleared_callback0 = 0;
- asyncio_state *state = get_asyncio_state_by_cls(cls);
- ENSURE_FUTURE_ALIVE(state, self)
- if (self->fut_callback0 != NULL) {
- // Beware: An evil PyObject_RichCompareBool could free fut_callback0
- // before a recursive call is made with that same arg. For details, see
- // https://github.com/python/cpython/pull/125967#discussion_r1816593340.
- PyObject *fut_callback0 = Py_NewRef(self->fut_callback0);
- int cmp = PyObject_RichCompareBool(fut_callback0, fn, Py_EQ);
- Py_DECREF(fut_callback0);
- if (cmp == -1) {
- return NULL;
- }
- if (cmp == 1) {
- /* callback0 == fn */
- Py_CLEAR(self->fut_callback0);
- Py_CLEAR(self->fut_context0);
- cleared_callback0 = 1;
- }
- }
- if (self->fut_callbacks == NULL) {
- return PyLong_FromSsize_t(cleared_callback0);
- }
- len = PyList_GET_SIZE(self->fut_callbacks);
- if (len == 0) {
- Py_CLEAR(self->fut_callbacks);
- return PyLong_FromSsize_t(cleared_callback0);
- }
- if (len == 1) {
- PyObject *cb_tup = PyList_GET_ITEM(self->fut_callbacks, 0);
- Py_INCREF(cb_tup);
- int cmp = PyObject_RichCompareBool(
- PyTuple_GET_ITEM(cb_tup, 0), fn, Py_EQ);
- Py_DECREF(cb_tup);
- if (cmp == -1) {
- return NULL;
- }
- if (cmp == 1) {
- /* callbacks[0] == fn */
- Py_CLEAR(self->fut_callbacks);
- return PyLong_FromSsize_t(1 + cleared_callback0);
- }
- /* callbacks[0] != fn and len(callbacks) == 1 */
- return PyLong_FromSsize_t(cleared_callback0);
- }
- newlist = PyList_New(len);
- if (newlist == NULL) {
- return NULL;
- }
- // Beware: PyObject_RichCompareBool below may change fut_callbacks.
- // See GH-97592.
- for (i = 0;
- self->fut_callbacks != NULL && i < PyList_GET_SIZE(self->fut_callbacks);
- i++) {
- int ret;
- PyObject *item = PyList_GET_ITEM(self->fut_callbacks, i);
- Py_INCREF(item);
- ret = PyObject_RichCompareBool(PyTuple_GET_ITEM(item, 0), fn, Py_EQ);
- if (ret == 0) {
- if (j < len) {
- PyList_SET_ITEM(newlist, j, item);
- j++;
- continue;
- }
- ret = PyList_Append(newlist, item);
- }
- Py_DECREF(item);
- if (ret < 0) {
- goto fail;
- }
- }
- // Note: fut_callbacks may have been cleared.
- if (j == 0 || self->fut_callbacks == NULL) {
- Py_CLEAR(self->fut_callbacks);
- Py_DECREF(newlist);
- return PyLong_FromSsize_t(len + cleared_callback0);
- }
- if (j < len) {
- Py_SET_SIZE(newlist, j);
- }
- j = PyList_GET_SIZE(newlist);
- len = PyList_GET_SIZE(self->fut_callbacks);
- if (j != len) {
- if (PyList_SetSlice(self->fut_callbacks, 0, len, newlist) < 0) {
- goto fail;
- }
- }
- Py_DECREF(newlist);
- return PyLong_FromSsize_t(len - j + cleared_callback0);
- fail:
- Py_DECREF(newlist);
- return NULL;
- }
- /*[clinic input]
- _asyncio.Future.cancel
- cls: defining_class
- /
- msg: object = None
- Cancel the future and schedule callbacks.
- If the future is already done or cancelled, return False. Otherwise,
- change the future's state to cancelled, schedule the callbacks and
- return True.
- [clinic start generated code]*/
- static PyObject *
- _asyncio_Future_cancel_impl(FutureObj *self, PyTypeObject *cls,
- PyObject *msg)
- /*[clinic end generated code: output=074956f35904b034 input=bba8f8b786941a94]*/
- {
- asyncio_state *state = get_asyncio_state_by_cls(cls);
- ENSURE_FUTURE_ALIVE(state, self)
- return future_cancel(state, self, msg);
- }
- /*[clinic input]
- _asyncio.Future.cancelled
- Return True if the future was cancelled.
- [clinic start generated code]*/
- static PyObject *
- _asyncio_Future_cancelled_impl(FutureObj *self)
- /*[clinic end generated code: output=145197ced586357d input=943ab8b7b7b17e45]*/
- {
- if (future_is_alive(self) && self->fut_state == STATE_CANCELLED) {
- Py_RETURN_TRUE;
- }
- else {
- Py_RETURN_FALSE;
- }
- }
- /*[clinic input]
- _asyncio.Future.done
- Return True if the future is done.
- Done means either that a result / exception are available, or that the
- future was cancelled.
- [clinic start generated code]*/
- static PyObject *
- _asyncio_Future_done_impl(FutureObj *self)
- /*[clinic end generated code: output=244c5ac351145096 input=28d7b23fdb65d2ac]*/
- {
- if (!future_is_alive(self) || self->fut_state == STATE_PENDING) {
- Py_RETURN_FALSE;
- }
- else {
- Py_RETURN_TRUE;
- }
- }
- /*[clinic input]
- _asyncio.Future.get_loop
- cls: defining_class
- /
- Return the event loop the Future is bound to.
- [clinic start generated code]*/
- static PyObject *
- _asyncio_Future_get_loop_impl(FutureObj *self, PyTypeObject *cls)
- /*[clinic end generated code: output=f50ea6c374d9ee97 input=163c2c498b45a1f0]*/
- {
- asyncio_state *state = get_asyncio_state_by_cls(cls);
- ENSURE_FUTURE_ALIVE(state, self)
- return Py_NewRef(self->fut_loop);
- }
- static PyObject *
- FutureObj_get_blocking(FutureObj *fut, void *Py_UNUSED(ignored))
- {
- if (future_is_alive(fut) && fut->fut_blocking) {
- Py_RETURN_TRUE;
- }
- else {
- Py_RETURN_FALSE;
- }
- }
- static int
- FutureObj_set_blocking(FutureObj *fut, PyObject *val, void *Py_UNUSED(ignored))
- {
- if (future_ensure_alive(fut)) {
- return -1;
- }
- if (val == NULL) {
- PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
- return -1;
- }
- int is_true = PyObject_IsTrue(val);
- if (is_true < 0) {
- return -1;
- }
- fut->fut_blocking = is_true;
- return 0;
- }
- static PyObject *
- FutureObj_get_log_traceback(FutureObj *fut, void *Py_UNUSED(ignored))
- {
- asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut);
- ENSURE_FUTURE_ALIVE(state, fut)
- if (fut->fut_log_tb) {
- Py_RETURN_TRUE;
- }
- else {
- Py_RETURN_FALSE;
- }
- }
- static int
- FutureObj_set_log_traceback(FutureObj *fut, PyObject *val, void *Py_UNUSED(ignored))
- {
- if (val == NULL) {
- PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
- return -1;
- }
- int is_true = PyObject_IsTrue(val);
- if (is_true < 0) {
- return -1;
- }
- if (is_true) {
- PyErr_SetString(PyExc_ValueError,
- "_log_traceback can only be set to False");
- return -1;
- }
- fut->fut_log_tb = is_true;
- return 0;
- }
- static PyObject *
- FutureObj_get_loop(FutureObj *fut, void *Py_UNUSED(ignored))
- {
- if (!future_is_alive(fut)) {
- Py_RETURN_NONE;
- }
- return Py_NewRef(fut->fut_loop);
- }
- static PyObject *
- FutureObj_get_callbacks(FutureObj *fut, void *Py_UNUSED(ignored))
- {
- asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut);
- ENSURE_FUTURE_ALIVE(state, fut)
- Py_ssize_t len = 0;
- if (fut->fut_callback0 != NULL) {
- len++;
- }
- if (fut->fut_callbacks != NULL) {
- len += PyList_GET_SIZE(fut->fut_callbacks);
- }
- if (len == 0) {
- Py_RETURN_NONE;
- }
- PyObject *callbacks = PyList_New(len);
- if (callbacks == NULL) {
- return NULL;
- }
- Py_ssize_t i = 0;
- if (fut->fut_callback0 != NULL) {
- PyObject *tup0 = PyTuple_New(2);
- if (tup0 == NULL) {
- Py_DECREF(callbacks);
- return NULL;
- }
- PyTuple_SET_ITEM(tup0, 0, Py_NewRef(fut->fut_callback0));
- assert(fut->fut_context0 != NULL);
- PyTuple_SET_ITEM(tup0, 1, Py_NewRef(fut->fut_context0));
- PyList_SET_ITEM(callbacks, i, tup0);
- i++;
- }
- if (fut->fut_callbacks != NULL) {
- for (Py_ssize_t j = 0; j < PyList_GET_SIZE(fut->fut_callbacks); j++) {
- PyObject *cb = PyList_GET_ITEM(fut->fut_callbacks, j);
- Py_INCREF(cb);
- PyList_SET_ITEM(callbacks, i, cb);
- i++;
- }
- }
- return callbacks;
- }
- static PyObject *
- FutureObj_get_result(FutureObj *fut, void *Py_UNUSED(ignored))
- {
- asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut);
- ENSURE_FUTURE_ALIVE(state, fut)
- if (fut->fut_result == NULL) {
- Py_RETURN_NONE;
- }
- return Py_NewRef(fut->fut_result);
- }
- static PyObject *
- FutureObj_get_exception(FutureObj *fut, void *Py_UNUSED(ignored))
- {
- asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut);
- ENSURE_FUTURE_ALIVE(state, fut)
- if (fut->fut_exception == NULL) {
- Py_RETURN_NONE;
- }
- return Py_NewRef(fut->fut_exception);
- }
- static PyObject *
- FutureObj_get_source_traceback(FutureObj *fut, void *Py_UNUSED(ignored))
- {
- if (!future_is_alive(fut) || fut->fut_source_tb == NULL) {
- Py_RETURN_NONE;
- }
- return Py_NewRef(fut->fut_source_tb);
- }
- static PyObject *
- FutureObj_get_cancel_message(FutureObj *fut, void *Py_UNUSED(ignored))
- {
- if (fut->fut_cancel_msg == NULL) {
- Py_RETURN_NONE;
- }
- return Py_NewRef(fut->fut_cancel_msg);
- }
- static int
- FutureObj_set_cancel_message(FutureObj *fut, PyObject *msg,
- void *Py_UNUSED(ignored))
- {
- if (msg == NULL) {
- PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
- return -1;
- }
- Py_INCREF(msg);
- Py_XSETREF(fut->fut_cancel_msg, msg);
- return 0;
- }
- static PyObject *
- FutureObj_get_state(FutureObj *fut, void *Py_UNUSED(ignored))
- {
- asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut);
- PyObject *ret = NULL;
- ENSURE_FUTURE_ALIVE(state, fut)
- switch (fut->fut_state) {
- case STATE_PENDING:
- ret = &_Py_ID(PENDING);
- break;
- case STATE_CANCELLED:
- ret = &_Py_ID(CANCELLED);
- break;
- case STATE_FINISHED:
- ret = &_Py_ID(FINISHED);
- break;
- default:
- assert (0);
- }
- return Py_XNewRef(ret);
- }
- static PyObject *
- FutureObj_repr(FutureObj *fut)
- {
- asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut);
- ENSURE_FUTURE_ALIVE(state, fut)
- return PyObject_CallOneArg(state->asyncio_future_repr_func, (PyObject *)fut);
- }
- /*[clinic input]
- _asyncio.Future._make_cancelled_error
- Create the CancelledError to raise if the Future is cancelled.
- This should only be called once when handling a cancellation since
- it erases the context exception value.
- [clinic start generated code]*/
- static PyObject *
- _asyncio_Future__make_cancelled_error_impl(FutureObj *self)
- /*[clinic end generated code: output=a5df276f6c1213de input=ac6effe4ba795ecc]*/
- {
- asyncio_state *state = get_asyncio_state_by_def((PyObject *)self);
- return create_cancelled_error(state, self);
- }
- static void
- FutureObj_finalize(FutureObj *fut)
- {
- PyObject *context;
- PyObject *message = NULL;
- PyObject *func;
- if (!fut->fut_log_tb) {
- return;
- }
- assert(fut->fut_exception != NULL);
- fut->fut_log_tb = 0;
- /* Save the current exception, if any. */
- PyObject *exc = PyErr_GetRaisedException();
- context = PyDict_New();
- if (context == NULL) {
- goto finally;
- }
- message = PyUnicode_FromFormat(
- "%s exception was never retrieved", _PyType_Name(Py_TYPE(fut)));
- if (message == NULL) {
- goto finally;
- }
- if (PyDict_SetItem(context, &_Py_ID(message), message) < 0 ||
- PyDict_SetItem(context, &_Py_ID(exception), fut->fut_exception) < 0 ||
- PyDict_SetItem(context, &_Py_ID(future), (PyObject*)fut) < 0) {
- goto finally;
- }
- if (fut->fut_source_tb != NULL) {
- if (PyDict_SetItem(context, &_Py_ID(source_traceback),
- fut->fut_source_tb) < 0) {
- goto finally;
- }
- }
- func = PyObject_GetAttr(fut->fut_loop, &_Py_ID(call_exception_handler));
- if (func != NULL) {
- PyObject *res = PyObject_CallOneArg(func, context);
- if (res == NULL) {
- PyErr_WriteUnraisable(func);
- }
- else {
- Py_DECREF(res);
- }
- Py_DECREF(func);
- }
- finally:
- Py_XDECREF(context);
- Py_XDECREF(message);
- /* Restore the saved exception. */
- PyErr_SetRaisedException(exc);
- }
- static PyMethodDef FutureType_methods[] = {
- _ASYNCIO_FUTURE_RESULT_METHODDEF
- _ASYNCIO_FUTURE_EXCEPTION_METHODDEF
- _ASYNCIO_FUTURE_SET_RESULT_METHODDEF
- _ASYNCIO_FUTURE_SET_EXCEPTION_METHODDEF
- _ASYNCIO_FUTURE_ADD_DONE_CALLBACK_METHODDEF
- _ASYNCIO_FUTURE_REMOVE_DONE_CALLBACK_METHODDEF
- _ASYNCIO_FUTURE_CANCEL_METHODDEF
- _ASYNCIO_FUTURE_CANCELLED_METHODDEF
- _ASYNCIO_FUTURE_DONE_METHODDEF
- _ASYNCIO_FUTURE_GET_LOOP_METHODDEF
- _ASYNCIO_FUTURE__MAKE_CANCELLED_ERROR_METHODDEF
- {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
- {NULL, NULL} /* Sentinel */
- };
- static PyMemberDef FutureType_members[] = {
- {"__weaklistoffset__", T_PYSSIZET, offsetof(FutureObj, fut_weakreflist), READONLY},
- {"__dictoffset__", T_PYSSIZET, offsetof(FutureObj, dict), READONLY},
- {NULL},
- };
- #define FUTURE_COMMON_GETSETLIST \
- {"_state", (getter)FutureObj_get_state, NULL, NULL}, \
- {"_asyncio_future_blocking", (getter)FutureObj_get_blocking, \
- (setter)FutureObj_set_blocking, NULL}, \
- {"_loop", (getter)FutureObj_get_loop, NULL, NULL}, \
- {"_callbacks", (getter)FutureObj_get_callbacks, NULL, NULL}, \
- {"_result", (getter)FutureObj_get_result, NULL, NULL}, \
- {"_exception", (getter)FutureObj_get_exception, NULL, NULL}, \
- {"_log_traceback", (getter)FutureObj_get_log_traceback, \
- (setter)FutureObj_set_log_traceback, NULL}, \
- {"_source_traceback", (getter)FutureObj_get_source_traceback, \
- NULL, NULL}, \
- {"_cancel_message", (getter)FutureObj_get_cancel_message, \
- (setter)FutureObj_set_cancel_message, NULL},
- static PyGetSetDef FutureType_getsetlist[] = {
- FUTURE_COMMON_GETSETLIST
- {NULL} /* Sentinel */
- };
- static void FutureObj_dealloc(PyObject *self);
- static PyType_Slot Future_slots[] = {
- {Py_tp_dealloc, FutureObj_dealloc},
- {Py_tp_repr, (reprfunc)FutureObj_repr},
- {Py_tp_doc, (void *)_asyncio_Future___init____doc__},
- {Py_tp_traverse, (traverseproc)FutureObj_traverse},
- {Py_tp_clear, (inquiry)FutureObj_clear},
- {Py_tp_iter, (getiterfunc)future_new_iter},
- {Py_tp_methods, FutureType_methods},
- {Py_tp_members, FutureType_members},
- {Py_tp_getset, FutureType_getsetlist},
- {Py_tp_init, (initproc)_asyncio_Future___init__},
- {Py_tp_new, PyType_GenericNew},
- {Py_tp_finalize, (destructor)FutureObj_finalize},
- // async slots
- {Py_am_await, (unaryfunc)future_new_iter},
- {0, NULL},
- };
- static PyType_Spec Future_spec = {
- .name = "_asyncio.Future",
- .basicsize = sizeof(FutureObj),
- .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE |
- Py_TPFLAGS_IMMUTABLETYPE),
- .slots = Future_slots,
- };
- static void
- FutureObj_dealloc(PyObject *self)
- {
- FutureObj *fut = (FutureObj *)self;
- if (PyObject_CallFinalizerFromDealloc(self) < 0) {
- // resurrected.
- return;
- }
- PyTypeObject *tp = Py_TYPE(fut);
- PyObject_GC_UnTrack(self);
- if (fut->fut_weakreflist != NULL) {
- PyObject_ClearWeakRefs(self);
- }
- (void)FutureObj_clear(fut);
- tp->tp_free(fut);
- Py_DECREF(tp);
- }
- /*********************** Future Iterator **************************/
- typedef struct futureiterobject {
- PyObject_HEAD
- FutureObj *future;
- } futureiterobject;
- static void
- FutureIter_dealloc(futureiterobject *it)
- {
- PyTypeObject *tp = Py_TYPE(it);
- // FutureIter is a heap type so any subclass must also be a heap type.
- assert(_PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE));
- PyObject *module = ((PyHeapTypeObject*)tp)->ht_module;
- asyncio_state *state = NULL;
- PyObject_GC_UnTrack(it);
- tp->tp_clear((PyObject *)it);
- // GH-115874: We can't use PyType_GetModuleByDef here as the type might have
- // already been cleared, which is also why we must check if ht_module != NULL.
- // Due to this restriction, subclasses that belong to a different module
- // will not be able to use the free list.
- if (module && _PyModule_GetDef(module) == &_asynciomodule) {
- state = get_asyncio_state(module);
- }
- if (state && state->fi_freelist_len < FI_FREELIST_MAXLEN) {
- state->fi_freelist_len++;
- it->future = (FutureObj*) state->fi_freelist;
- state->fi_freelist = it;
- }
- else {
- PyObject_GC_Del(it);
- Py_DECREF(tp);
- }
- }
- static PySendResult
- FutureIter_am_send(futureiterobject *it,
- PyObject *Py_UNUSED(arg),
- PyObject **result)
- {
- /* arg is unused, see the comment on FutureIter_send for clarification */
- PyObject *res;
- FutureObj *fut = it->future;
- *result = NULL;
- if (fut == NULL) {
- return PYGEN_ERROR;
- }
- if (fut->fut_state == STATE_PENDING) {
- if (!fut->fut_blocking) {
- fut->fut_blocking = 1;
- *result = Py_NewRef(fut);
- return PYGEN_NEXT;
- }
- PyErr_SetString(PyExc_RuntimeError,
- "await wasn't used with future");
- return PYGEN_ERROR;
- }
- it->future = NULL;
- res = _asyncio_Future_result_impl(fut);
- if (res != NULL) {
- Py_DECREF(fut);
- *result = res;
- return PYGEN_RETURN;
- }
- Py_DECREF(fut);
- return PYGEN_ERROR;
- }
- static PyObject *
- FutureIter_iternext(futureiterobject *it)
- {
- PyObject *result;
- switch (FutureIter_am_send(it, Py_None, &result)) {
- case PYGEN_RETURN:
- (void)_PyGen_SetStopIterationValue(result);
- Py_DECREF(result);
- return NULL;
- case PYGEN_NEXT:
- return result;
- case PYGEN_ERROR:
- return NULL;
- default:
- Py_UNREACHABLE();
- }
- }
- static PyObject *
- FutureIter_send(futureiterobject *self, PyObject *unused)
- {
- /* Future.__iter__ doesn't care about values that are pushed to the
- * generator, it just returns self.result().
- */
- return FutureIter_iternext(self);
- }
- static PyObject *
- FutureIter_throw(futureiterobject *self, PyObject *const *args, Py_ssize_t nargs)
- {
- PyObject *type, *val = NULL, *tb = NULL;
- if (!_PyArg_CheckPositional("throw", nargs, 1, 3)) {
- return NULL;
- }
- if (nargs > 1) {
- if (PyErr_WarnEx(PyExc_DeprecationWarning,
- "the (type, exc, tb) signature of throw() is deprecated, "
- "use the single-arg signature instead.",
- 1) < 0) {
- return NULL;
- }
- }
- type = args[0];
- if (nargs == 3) {
- val = args[1];
- tb = args[2];
- }
- else if (nargs == 2) {
- val = args[1];
- }
- if (val == Py_None) {
- val = NULL;
- }
- if (tb == Py_None ) {
- tb = NULL;
- } else if (tb != NULL && !PyTraceBack_Check(tb)) {
- PyErr_SetString(PyExc_TypeError, "throw() third argument must be a traceback");
- return NULL;
- }
- Py_INCREF(type);
- Py_XINCREF(val);
- Py_XINCREF(tb);
- if (PyExceptionClass_Check(type)) {
- PyErr_NormalizeException(&type, &val, &tb);
- /* No need to call PyException_SetTraceback since we'll be calling
- PyErr_Restore for `type`, `val`, and `tb`. */
- } else if (PyExceptionInstance_Check(type)) {
- if (val) {
- PyErr_SetString(PyExc_TypeError,
- "instance exception may not have a separate value");
- goto fail;
- }
- val = type;
- type = PyExceptionInstance_Class(type);
- Py_INCREF(type);
- if (tb == NULL)
- tb = PyException_GetTraceback(val);
- } else {
- PyErr_SetString(PyExc_TypeError,
- "exceptions must be classes deriving BaseException or "
- "instances of such a class");
- goto fail;
- }
- Py_CLEAR(self->future);
- PyErr_Restore(type, val, tb);
- return NULL;
- fail:
- Py_DECREF(type);
- Py_XDECREF(val);
- Py_XDECREF(tb);
- return NULL;
- }
- static int
- FutureIter_clear(futureiterobject *it)
- {
- Py_CLEAR(it->future);
- return 0;
- }
- static PyObject *
- FutureIter_close(futureiterobject *self, PyObject *arg)
- {
- (void)FutureIter_clear(self);
- Py_RETURN_NONE;
- }
- static int
- FutureIter_traverse(futureiterobject *it, visitproc visit, void *arg)
- {
- Py_VISIT(Py_TYPE(it));
- Py_VISIT(it->future);
- return 0;
- }
- static PyMethodDef FutureIter_methods[] = {
- {"send", (PyCFunction)FutureIter_send, METH_O, NULL},
- {"throw", _PyCFunction_CAST(FutureIter_throw), METH_FASTCALL, NULL},
- {"close", (PyCFunction)FutureIter_close, METH_NOARGS, NULL},
- {NULL, NULL} /* Sentinel */
- };
- static PyType_Slot FutureIter_slots[] = {
- {Py_tp_dealloc, (destructor)FutureIter_dealloc},
- {Py_tp_getattro, PyObject_GenericGetAttr},
- {Py_tp_traverse, (traverseproc)FutureIter_traverse},
- {Py_tp_clear, FutureIter_clear},
- {Py_tp_iter, PyObject_SelfIter},
- {Py_tp_iternext, (iternextfunc)FutureIter_iternext},
- {Py_tp_methods, FutureIter_methods},
- // async methods
- {Py_am_send, (sendfunc)FutureIter_am_send},
- {0, NULL},
- };
- static PyType_Spec FutureIter_spec = {
- .name = "_asyncio.FutureIter",
- .basicsize = sizeof(futureiterobject),
- .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_IMMUTABLETYPE),
- .slots = FutureIter_slots,
- };
- static PyObject *
- future_new_iter(PyObject *fut)
- {
- futureiterobject *it;
- asyncio_state *state = get_asyncio_state_by_def((PyObject *)fut);
- ENSURE_FUTURE_ALIVE(state, fut)
- if (state->fi_freelist_len) {
- state->fi_freelist_len--;
- it = state->fi_freelist;
- state->fi_freelist = (futureiterobject*) it->future;
- it->future = NULL;
- _Py_NewReference((PyObject*) it);
- }
- else {
- it = PyObject_GC_New(futureiterobject, state->FutureIterType);
- if (it == NULL) {
- return NULL;
- }
- }
- it->future = (FutureObj*)Py_NewRef(fut);
- PyObject_GC_Track(it);
- return (PyObject*)it;
- }
- /*********************** Task **************************/
- /*[clinic input]
- class _asyncio.Task "TaskObj *" "&Task_Type"
- [clinic start generated code]*/
- /*[clinic end generated code: output=da39a3ee5e6b4b0d input=719dcef0fcc03b37]*/
- static int task_call_step_soon(asyncio_state *state, TaskObj *, PyObject *);
- static PyObject * task_wakeup(TaskObj *, PyObject *);
- static PyObject * task_step(asyncio_state *, TaskObj *, PyObject *);
- static int task_eager_start(asyncio_state *state, TaskObj *task);
- /* ----- Task._step wrapper */
- static int
- TaskStepMethWrapper_clear(TaskStepMethWrapper *o)
- {
- Py_CLEAR(o->sw_task);
- Py_CLEAR(o->sw_arg);
- return 0;
- }
- static void
- TaskStepMethWrapper_dealloc(TaskStepMethWrapper *o)
- {
- PyTypeObject *tp = Py_TYPE(o);
- PyObject_GC_UnTrack(o);
- (void)TaskStepMethWrapper_clear(o);
- Py_TYPE(o)->tp_free(o);
- Py_DECREF(tp);
- }
- static PyObject *
- TaskStepMethWrapper_call(TaskStepMethWrapper *o,
- PyObject *args, PyObject *kwds)
- {
- if (kwds != NULL && PyDict_GET_SIZE(kwds) != 0) {
- PyErr_SetString(PyExc_TypeError, "function takes no keyword arguments");
- return NULL;
- }
- if (args != NULL && PyTuple_GET_SIZE(args) != 0) {
- PyErr_SetString(PyExc_TypeError, "function takes no positional arguments");
- return NULL;
- }
- asyncio_state *state = get_asyncio_state_by_def((PyObject *)o);
- return task_step(state, o->sw_task, o->sw_arg);
- }
- static int
- TaskStepMethWrapper_traverse(TaskStepMethWrapper *o,
- visitproc visit, void *arg)
- {
- Py_VISIT(Py_TYPE(o));
- Py_VISIT(o->sw_task);
- Py_VISIT(o->sw_arg);
- return 0;
- }
- static PyObject *
- TaskStepMethWrapper_get___self__(TaskStepMethWrapper *o, void *Py_UNUSED(ignored))
- {
- if (o->sw_task) {
- return Py_NewRef(o->sw_task);
- }
- Py_RETURN_NONE;
- }
- static PyGetSetDef TaskStepMethWrapper_getsetlist[] = {
- {"__self__", (getter)TaskStepMethWrapper_get___self__, NULL, NULL},
- {NULL} /* Sentinel */
- };
- static PyType_Slot TaskStepMethWrapper_slots[] = {
- {Py_tp_getset, TaskStepMethWrapper_getsetlist},
- {Py_tp_dealloc, (destructor)TaskStepMethWrapper_dealloc},
- {Py_tp_call, (ternaryfunc)TaskStepMethWrapper_call},
- {Py_tp_getattro, PyObject_GenericGetAttr},
- {Py_tp_traverse, (traverseproc)TaskStepMethWrapper_traverse},
- {Py_tp_clear, (inquiry)TaskStepMethWrapper_clear},
- {0, NULL},
- };
- static PyType_Spec TaskStepMethWrapper_spec = {
- .name = "_asyncio.TaskStepMethWrapper",
- .basicsize = sizeof(TaskStepMethWrapper),
- .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_IMMUTABLETYPE),
- .slots = TaskStepMethWrapper_slots,
- };
- static PyObject *
- TaskStepMethWrapper_new(TaskObj *task, PyObject *arg)
- {
- asyncio_state *state = get_asyncio_state_by_def((PyObject *)task);
- TaskStepMethWrapper *o;
- o = PyObject_GC_New(TaskStepMethWrapper, state->TaskStepMethWrapper_Type);
- if (o == NULL) {
- return NULL;
- }
- o->sw_task = (TaskObj*)Py_NewRef(task);
- o->sw_arg = Py_XNewRef(arg);
- PyObject_GC_Track(o);
- return (PyObject*) o;
- }
- /* ----- Task._wakeup implementation */
- static PyMethodDef TaskWakeupDef = {
- "task_wakeup",
- (PyCFunction)task_wakeup,
- METH_O,
- NULL
- };
- /* ----- Task introspection helpers */
- static int
- register_task(asyncio_state *state, PyObject *task)
- {
- PyObject *res = PyObject_CallMethodOneArg(state->scheduled_tasks,
- &_Py_ID(add), task);
- if (res == NULL) {
- return -1;
- }
- Py_DECREF(res);
- return 0;
- }
- static int
- register_eager_task(asyncio_state *state, PyObject *task)
- {
- return PySet_Add(state->eager_tasks, task);
- }
- static int
- unregister_task(asyncio_state *state, PyObject *task)
- {
- PyObject *res = PyObject_CallMethodOneArg(state->scheduled_tasks,
- &_Py_ID(discard), task);
- if (res == NULL) {
- return -1;
- }
- Py_DECREF(res);
- return 0;
- }
- static int
- unregister_eager_task(asyncio_state *state, PyObject *task)
- {
- return PySet_Discard(state->eager_tasks, task);
- }
- static int
- enter_task(asyncio_state *state, PyObject *loop, PyObject *task)
- {
- PyObject *item;
- Py_hash_t hash;
- hash = PyObject_Hash(loop);
- if (hash == -1) {
- return -1;
- }
- item = _PyDict_GetItem_KnownHash(state->current_tasks, loop, hash);
- if (item != NULL) {
- Py_INCREF(item);
- PyErr_Format(
- PyExc_RuntimeError,
- "Cannot enter into task %R while another " \
- "task %R is being executed.",
- task, item, NULL);
- Py_DECREF(item);
- return -1;
- }
- if (PyErr_Occurred()) {
- return -1;
- }
- return _PyDict_SetItem_KnownHash(state->current_tasks, loop, task, hash);
- }
- static int
- leave_task(asyncio_state *state, PyObject *loop, PyObject *task)
- /*[clinic end generated code: output=0ebf6db4b858fb41 input=51296a46313d1ad8]*/
- {
- PyObject *item;
- Py_hash_t hash;
- hash = PyObject_Hash(loop);
- if (hash == -1) {
- return -1;
- }
- item = _PyDict_GetItem_KnownHash(state->current_tasks, loop, hash);
- if (item != task) {
- if (item == NULL) {
- /* Not entered, replace with None */
- item = Py_None;
- }
- PyErr_Format(
- PyExc_RuntimeError,
- "Leaving task %R does not match the current task %R.",
- task, item, NULL);
- return -1;
- }
- return _PyDict_DelItem_KnownHash(state->current_tasks, loop, hash);
- }
- static PyObject *
- swap_current_task(asyncio_state *state, PyObject *loop, PyObject *task)
- {
- PyObject *prev_task;
- Py_hash_t hash;
- hash = PyObject_Hash(loop);
- if (hash == -1) {
- return NULL;
- }
- prev_task = _PyDict_GetItem_KnownHash(state->current_tasks, loop, hash);
- if (prev_task == NULL) {
- if (PyErr_Occurred()) {
- return NULL;
- }
- prev_task = Py_None;
- }
- Py_INCREF(prev_task);
- if (task == Py_None) {
- if (_PyDict_DelItem_KnownHash(state->current_tasks, loop, hash) == -1) {
- goto error;
- }
- } else {
- if (_PyDict_SetItem_KnownHash(state->current_tasks, loop, task, hash) == -1) {
- goto error;
- }
- }
- return prev_task;
- error:
- Py_DECREF(prev_task);
- return NULL;
- }
- /* ----- Task */
- /*[clinic input]
- _asyncio.Task.__init__
- coro: object
- *
- loop: object = None
- name: object = None
- context: object = None
- eager_start: bool = False
- A coroutine wrapped in a Future.
- [clinic start generated code]*/
- static int
- _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop,
- PyObject *name, PyObject *context,
- int eager_start)
- /*[clinic end generated code: output=7aced2d27836f1a1 input=18e3f113a51b829d]*/
- {
- if (future_init((FutureObj*)self, loop)) {
- return -1;
- }
- asyncio_state *state = get_asyncio_state_by_def((PyObject *)self);
- int is_coro = is_coroutine(state, coro);
- if (is_coro == -1) {
- return -1;
- }
- if (is_coro == 0) {
- self->task_log_destroy_pending = 0;
- PyErr_Format(PyExc_TypeError,
- "a coroutine was expected, got %R",
- coro, NULL);
- return -1;
- }
- if (context == Py_None) {
- Py_XSETREF(self->task_context, PyContext_CopyCurrent());
- if (self->task_context == NULL) {
- return -1;
- }
- } else {
- Py_XSETREF(self->task_context, Py_NewRef(context));
- }
- Py_CLEAR(self->task_fut_waiter);
- self->task_must_cancel = 0;
- self->task_log_destroy_pending = 1;
- self->task_num_cancels_requested = 0;
- Py_INCREF(coro);
- Py_XSETREF(self->task_coro, coro);
- if (name == Py_None) {
- // optimization: defer task name formatting
- // store the task counter as PyLong in the name
- // for deferred formatting in get_name
- name = PyLong_FromUnsignedLongLong(++state->task_name_counter);
- } else if (!PyUnicode_CheckExact(name)) {
- name = PyObject_Str(name);
- } else {
- Py_INCREF(name);
- }
- Py_XSETREF(self->task_name, name);
- if (self->task_name == NULL) {
- return -1;
- }
- if (eager_start) {
- PyObject *res = PyObject_CallMethodNoArgs(loop, &_Py_ID(is_running));
- if (res == NULL) {
- return -1;
- }
- int is_loop_running = Py_IsTrue(res);
- Py_DECREF(res);
- if (is_loop_running) {
- if (task_eager_start(state, self)) {
- return -1;
- }
- return 0;
- }
- }
- if (task_call_step_soon(state, self, NULL)) {
- return -1;
- }
- return register_task(state, (PyObject*)self);
- }
- static int
- TaskObj_clear(TaskObj *task)
- {
- (void)FutureObj_clear((FutureObj*) task);
- Py_CLEAR(task->task_context);
- Py_CLEAR(task->task_coro);
- Py_CLEAR(task->task_name);
- Py_CLEAR(task->task_fut_waiter);
- return 0;
- }
- static int
- TaskObj_traverse(TaskObj *task, visitproc visit, void *arg)
- {
- Py_VISIT(Py_TYPE(task));
- Py_VISIT(task->task_context);
- Py_VISIT(task->task_coro);
- Py_VISIT(task->task_name);
- Py_VISIT(task->task_fut_waiter);
- FutureObj *fut = (FutureObj *)task;
- Py_VISIT(fut->fut_loop);
- Py_VISIT(fut->fut_callback0);
- Py_VISIT(fut->fut_context0);
- Py_VISIT(fut->fut_callbacks);
- Py_VISIT(fut->fut_result);
- Py_VISIT(fut->fut_exception);
- Py_VISIT(fut->fut_exception_tb);
- Py_VISIT(fut->fut_source_tb);
- Py_VISIT(fut->fut_cancel_msg);
- Py_VISIT(fut->fut_cancelled_exc);
- Py_VISIT(fut->dict);
- return 0;
- }
- static PyObject *
- TaskObj_get_log_destroy_pending(TaskObj *task, void *Py_UNUSED(ignored))
- {
- if (task->task_log_destroy_pending) {
- Py_RETURN_TRUE;
- }
- else {
- Py_RETURN_FALSE;
- }
- }
- static int
- TaskObj_set_log_destroy_pending(TaskObj *task, PyObject *val, void *Py_UNUSED(ignored))
- {
- if (val == NULL) {
- PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
- return -1;
- }
- int is_true = PyObject_IsTrue(val);
- if (is_true < 0) {
- return -1;
- }
- task->task_log_destroy_pending = is_true;
- return 0;
- }
- static PyObject *
- TaskObj_get_must_cancel(TaskObj *task, void *Py_UNUSED(ignored))
- {
- if (task->task_must_cancel) {
- Py_RETURN_TRUE;
- }
- else {
- Py_RETURN_FALSE;
- }
- }
- static PyObject *
- TaskObj_get_coro(TaskObj *task, void *Py_UNUSED(ignored))
- {
- if (task->task_coro) {
- return Py_NewRef(task->task_coro);
- }
- Py_RETURN_NONE;
- }
- static PyObject *
- TaskObj_get_fut_waiter(TaskObj *task, void *Py_UNUSED(ignored))
- {
- if (task->task_fut_waiter) {
- return Py_NewRef(task->task_fut_waiter);
- }
- Py_RETURN_NONE;
- }
- static PyObject *
- TaskObj_repr(TaskObj *task)
- {
- asyncio_state *state = get_asyncio_state_by_def((PyObject *)task);
- return PyObject_CallOneArg(state->asyncio_task_repr_func,
- (PyObject *)task);
- }
- /*[clinic input]
- _asyncio.Task._make_cancelled_error
- Create the CancelledError to raise if the Task is cancelled.
- This should only be called once when handling a cancellation since
- it erases the context exception value.
- [clinic start generated code]*/
- static PyObject *
- _asyncio_Task__make_cancelled_error_impl(TaskObj *self)
- /*[clinic end generated code: output=55a819e8b4276fab input=52c0e32de8e2f840]*/
- {
- FutureObj *fut = (FutureObj*)self;
- return _asyncio_Future__make_cancelled_error_impl(fut);
- }
- /*[clinic input]
- _asyncio.Task.cancel
- msg: object = None
- Request that this task cancel itself.
- This arranges for a CancelledError to be thrown into the
- wrapped coroutine on the next cycle through the event loop.
- The coroutine then has a chance to clean up or even deny
- the request using try/except/finally.
- Unlike Future.cancel, this does not guarantee that the
- task will be cancelled: the exception might be caught and
- acted upon, delaying cancellation of the task or preventing
- cancellation completely. The task may also return a value or
- raise a different exception.
- Immediately after this method is called, Task.cancelled() will
- not return True (unless the task was already cancelled). A
- task will be marked as cancelled when the wrapped coroutine
- terminates with a CancelledError exception (even if cancel()
- was not called).
- This also increases the task's count of cancellation requests.
- [clinic start generated code]*/
- static PyObject *
- _asyncio_Task_cancel_impl(TaskObj *self, PyObject *msg)
- /*[clinic end generated code: output=c66b60d41c74f9f1 input=7bb51bf25974c783]*/
- {
- self->task_log_tb = 0;
- if (self->task_state != STATE_PENDING) {
- Py_RETURN_FALSE;
- }
- self->task_num_cancels_requested += 1;
- // These three lines are controversial. See discussion starting at
- // https://github.com/python/cpython/pull/31394#issuecomment-1053545331
- // and corresponding code in tasks.py.
- // if (self->task_num_cancels_requested > 1) {
- // Py_RETURN_FALSE;
- // }
- if (self->task_fut_waiter) {
- PyObject *res;
- int is_true;
- res = PyObject_CallMethodOneArg(self->task_fut_waiter,
- &_Py_ID(cancel), msg);
- if (res == NULL) {
- return NULL;
- }
- is_true = PyObject_IsTrue(res);
- Py_DECREF(res);
- if (is_true < 0) {
- return NULL;
- }
- if (is_true) {
- Py_RETURN_TRUE;
- }
- }
- self->task_must_cancel = 1;
- Py_XINCREF(msg);
- Py_XSETREF(self->task_cancel_msg, msg);
- Py_RETURN_TRUE;
- }
- /*[clinic input]
- _asyncio.Task.cancelling
- Return the count of the task's cancellation requests.
- This count is incremented when .cancel() is called
- and may be decremented using .uncancel().
- [clinic start generated code]*/
- static PyObject *
- _asyncio_Task_cancelling_impl(TaskObj *self)
- /*[clinic end generated code: output=803b3af96f917d7e input=b625224d310cbb17]*/
- /*[clinic end generated code]*/
- {
- return PyLong_FromLong(self->task_num_cancels_requested);
- }
- /*[clinic input]
- _asyncio.Task.uncancel
- Decrement the task's count of cancellation requests.
- This should be used by tasks that catch CancelledError
- and wish to continue indefinitely until they are cancelled again.
- Returns the remaining number of cancellation requests.
- [clinic start generated code]*/
- static PyObject *
- _asyncio_Task_uncancel_impl(TaskObj *self)
- /*[clinic end generated code: output=58184d236a817d3c input=68f81a4b90b46be2]*/
- /*[clinic end generated code]*/
- {
- if (self->task_num_cancels_requested > 0) {
- self->task_num_cancels_requested -= 1;
- }
- return PyLong_FromLong(self->task_num_cancels_requested);
- }
- /*[clinic input]
- _asyncio.Task.get_stack
- cls: defining_class
- /
- *
- limit: object = None
- Return the list of stack frames for this task's coroutine.
- If the coroutine is not done, this returns the stack where it is
- suspended. If the coroutine has completed successfully or was
- cancelled, this returns an empty list. If the coroutine was
- terminated by an exception, this returns the list of traceback
- frames.
- The frames are always ordered from oldest to newest.
- The optional limit gives the maximum number of frames to
- return; by default all available frames are returned. Its
- meaning differs depending on whether a stack or a traceback is
- returned: the newest frames of a stack are returned, but the
- oldest frames of a traceback are returned. (This matches the
- behavior of the traceback module.)
- For reasons beyond our control, only one stack frame is
- returned for a suspended coroutine.
- [clinic start generated code]*/
- static PyObject *
- _asyncio_Task_get_stack_impl(TaskObj *self, PyTypeObject *cls,
- PyObject *limit)
- /*[clinic end generated code: output=6774dfc10d3857fa input=8e01c9b2618ae953]*/
- {
- asyncio_state *state = get_asyncio_state_by_cls(cls);
- PyObject *stack[] = {(PyObject *)self, limit};
- return PyObject_Vectorcall(state->asyncio_task_get_stack_func,
- stack, 2, NULL);
- }
- /*[clinic input]
- _asyncio.Task.print_stack
- cls: defining_class
- /
- *
- limit: object = None
- file: object = None
- Print the stack or traceback for this task's coroutine.
- This produces output similar to that of the traceback module,
- for the frames retrieved by get_stack(). The limit argument
- is passed to get_stack(). The file argument is an I/O stream
- to which the output is written; by default output is written
- to sys.stderr.
- [clinic start generated code]*/
- static PyObject *
- _asyncio_Task_print_stack_impl(TaskObj *self, PyTypeObject *cls,
- PyObject *limit, PyObject *file)
- /*[clinic end generated code: output=b38affe9289ec826 input=150b35ba2d3a7dee]*/
- {
- asyncio_state *state = get_asyncio_state_by_cls(cls);
- PyObject *stack[] = {(PyObject *)self, limit, file};
- return PyObject_Vectorcall(state->asyncio_task_print_stack_func,
- stack, 3, NULL);
- }
- /*[clinic input]
- _asyncio.Task.set_result
- result: object
- /
- [clinic start generated code]*/
- static PyObject *
- _asyncio_Task_set_result(TaskObj *self, PyObject *result)
- /*[clinic end generated code: output=1dcae308bfcba318 input=9d1a00c07be41bab]*/
- {
- PyErr_SetString(PyExc_RuntimeError,
- "Task does not support set_result operation");
- return NULL;
- }
- /*[clinic input]
- _asyncio.Task.set_exception
- exception: object
- /
- [clinic start generated code]*/
- static PyObject *
- _asyncio_Task_set_exception(TaskObj *self, PyObject *exception)
- /*[clinic end generated code: output=bc377fc28067303d input=9a8f65c83dcf893a]*/
- {
- PyErr_SetString(PyExc_RuntimeError,
- "Task does not support set_exception operation");
- return NULL;
- }
- /*[clinic input]
- _asyncio.Task.get_coro
- [clinic start generated code]*/
- static PyObject *
- _asyncio_Task_get_coro_impl(TaskObj *self)
- /*[clinic end generated code: output=bcac27c8cc6c8073 input=d2e8606c42a7b403]*/
- {
- if (self->task_coro) {
- return Py_NewRef(self->task_coro);
- }
- Py_RETURN_NONE;
- }
- /*[clinic input]
- _asyncio.Task.get_context
- [clinic start generated code]*/
- static PyObject *
- _asyncio_Task_get_context_impl(TaskObj *self)
- /*[clinic end generated code: output=6996f53d3dc01aef input=87c0b209b8fceeeb]*/
- {
- return Py_NewRef(self->task_context);
- }
- /*[clinic input]
- _asyncio.Task.get_name
- [clinic start generated code]*/
- static PyObject *
- _asyncio_Task_get_name_impl(TaskObj *self)
- /*[clinic end generated code: output=0ecf1570c3b37a8f input=a4a6595d12f4f0f8]*/
- {
- if (self->task_name) {
- if (PyLong_CheckExact(self->task_name)) {
- PyObject *name = PyUnicode_FromFormat("Task-%S", self->task_name);
- if (name == NULL) {
- return NULL;
- }
- Py_SETREF(self->task_name, name);
- }
- return Py_NewRef(self->task_name);
- }
- Py_RETURN_NONE;
- }
- /*[clinic input]
- _asyncio.Task.set_name
- value: object
- /
- [clinic start generated code]*/
- static PyObject *
- _asyncio_Task_set_name(TaskObj *self, PyObject *value)
- /*[clinic end generated code: output=138a8d51e32057d6 input=a8359b6e65f8fd31]*/
- {
- if (!PyUnicode_CheckExact(value)) {
- value = PyObject_Str(value);
- if (value == NULL) {
- return NULL;
- }
- } else {
- Py_INCREF(value);
- }
- Py_XSETREF(self->task_name, value);
- Py_RETURN_NONE;
- }
- static void
- TaskObj_finalize(TaskObj *task)
- {
- PyObject *context;
- PyObject *message = NULL;
- PyObject *func;
- if (task->task_state != STATE_PENDING || !task->task_log_destroy_pending) {
- goto done;
- }
- /* Save the current exception, if any. */
- PyObject *exc = PyErr_GetRaisedException();
- context = PyDict_New();
- if (context == NULL) {
- goto finally;
- }
- message = PyUnicode_FromString("Task was destroyed but it is pending!");
- if (message == NULL) {
- goto finally;
- }
- if (PyDict_SetItem(context, &_Py_ID(message), message) < 0 ||
- PyDict_SetItem(context, &_Py_ID(task), (PyObject*)task) < 0)
- {
- goto finally;
- }
- if (task->task_source_tb != NULL) {
- if (PyDict_SetItem(context, &_Py_ID(source_traceback),
- task->task_source_tb) < 0)
- {
- goto finally;
- }
- }
- func = PyObject_GetAttr(task->task_loop, &_Py_ID(call_exception_handler));
- if (func != NULL) {
- PyObject *res = PyObject_CallOneArg(func, context);
- if (res == NULL) {
- PyErr_WriteUnraisable(func);
- }
- else {
- Py_DECREF(res);
- }
- Py_DECREF(func);
- }
- finally:
- Py_XDECREF(context);
- Py_XDECREF(message);
- /* Restore the saved exception. */
- PyErr_SetRaisedException(exc);
- done:
- FutureObj_finalize((FutureObj*)task);
- }
- static void TaskObj_dealloc(PyObject *); /* Needs Task_CheckExact */
- static PyMethodDef TaskType_methods[] = {
- _ASYNCIO_FUTURE_RESULT_METHODDEF
- _ASYNCIO_FUTURE_EXCEPTION_METHODDEF
- _ASYNCIO_FUTURE_ADD_DONE_CALLBACK_METHODDEF
- _ASYNCIO_FUTURE_REMOVE_DONE_CALLBACK_METHODDEF
- _ASYNCIO_FUTURE_CANCELLED_METHODDEF
- _ASYNCIO_FUTURE_DONE_METHODDEF
- _ASYNCIO_TASK_SET_RESULT_METHODDEF
- _ASYNCIO_TASK_SET_EXCEPTION_METHODDEF
- _ASYNCIO_TASK_CANCEL_METHODDEF
- _ASYNCIO_TASK_CANCELLING_METHODDEF
- _ASYNCIO_TASK_UNCANCEL_METHODDEF
- _ASYNCIO_TASK_GET_STACK_METHODDEF
- _ASYNCIO_TASK_PRINT_STACK_METHODDEF
- _ASYNCIO_TASK__MAKE_CANCELLED_ERROR_METHODDEF
- _ASYNCIO_TASK_GET_NAME_METHODDEF
- _ASYNCIO_TASK_SET_NAME_METHODDEF
- _ASYNCIO_TASK_GET_CORO_METHODDEF
- _ASYNCIO_TASK_GET_CONTEXT_METHODDEF
- {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
- {NULL, NULL} /* Sentinel */
- };
- static PyMemberDef TaskType_members[] = {
- {"__weaklistoffset__", T_PYSSIZET, offsetof(TaskObj, task_weakreflist), READONLY},
- {"__dictoffset__", T_PYSSIZET, offsetof(TaskObj, dict), READONLY},
- {NULL},
- };
- static PyGetSetDef TaskType_getsetlist[] = {
- FUTURE_COMMON_GETSETLIST
- {"_log_destroy_pending", (getter)TaskObj_get_log_destroy_pending,
- (setter)TaskObj_set_log_destroy_pending, NULL},
- {"_must_cancel", (getter)TaskObj_get_must_cancel, NULL, NULL},
- {"_coro", (getter)TaskObj_get_coro, NULL, NULL},
- {"_fut_waiter", (getter)TaskObj_get_fut_waiter, NULL, NULL},
- {NULL} /* Sentinel */
- };
- static PyType_Slot Task_slots[] = {
- {Py_tp_dealloc, TaskObj_dealloc},
- {Py_tp_repr, (reprfunc)TaskObj_repr},
- {Py_tp_doc, (void *)_asyncio_Task___init____doc__},
- {Py_tp_traverse, (traverseproc)TaskObj_traverse},
- {Py_tp_clear, (inquiry)TaskObj_clear},
- {Py_tp_iter, (getiterfunc)future_new_iter},
- {Py_tp_methods, TaskType_methods},
- {Py_tp_members, TaskType_members},
- {Py_tp_getset, TaskType_getsetlist},
- {Py_tp_init, (initproc)_asyncio_Task___init__},
- {Py_tp_new, PyType_GenericNew},
- {Py_tp_finalize, (destructor)TaskObj_finalize},
- // async slots
- {Py_am_await, (unaryfunc)future_new_iter},
- {0, NULL},
- };
- static PyType_Spec Task_spec = {
- .name = "_asyncio.Task",
- .basicsize = sizeof(TaskObj),
- .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE |
- Py_TPFLAGS_IMMUTABLETYPE),
- .slots = Task_slots,
- };
- static void
- TaskObj_dealloc(PyObject *self)
- {
- TaskObj *task = (TaskObj *)self;
- if (PyObject_CallFinalizerFromDealloc(self) < 0) {
- // resurrected.
- return;
- }
- PyTypeObject *tp = Py_TYPE(task);
- PyObject_GC_UnTrack(self);
- if (task->task_weakreflist != NULL) {
- PyObject_ClearWeakRefs(self);
- }
- (void)TaskObj_clear(task);
- tp->tp_free(task);
- Py_DECREF(tp);
- }
- static int
- task_call_step_soon(asyncio_state *state, TaskObj *task, PyObject *arg)
- {
- PyObject *cb = TaskStepMethWrapper_new(task, arg);
- if (cb == NULL) {
- return -1;
- }
- // Beware: An evil call_soon could alter task_context.
- // See: https://github.com/python/cpython/issues/126080.
- PyObject *task_context = Py_NewRef(task->task_context);
- int ret = call_soon(state, task->task_loop, cb, NULL, task_context);
- Py_DECREF(task_context);
- Py_DECREF(cb);
- return ret;
- }
- static PyObject *
- task_set_error_soon(asyncio_state *state, TaskObj *task, PyObject *et,
- const char *format, ...)
- {
- PyObject* msg;
- va_list vargs;
- va_start(vargs, format);
- msg = PyUnicode_FromFormatV(format, vargs);
- va_end(vargs);
- if (msg == NULL) {
- return NULL;
- }
- PyObject *e = PyObject_CallOneArg(et, msg);
- Py_DECREF(msg);
- if (e == NULL) {
- return NULL;
- }
- if (task_call_step_soon(state, task, e) == -1) {
- Py_DECREF(e);
- return NULL;
- }
- Py_DECREF(e);
- Py_RETURN_NONE;
- }
- static inline int
- gen_status_from_result(PyObject **result)
- {
- if (*result != NULL) {
- return PYGEN_NEXT;
- }
- if (_PyGen_FetchStopIterationValue(result) == 0) {
- return PYGEN_RETURN;
- }
- assert(PyErr_Occurred());
- return PYGEN_ERROR;
- }
- static PyObject *
- task_step_impl(asyncio_state *state, TaskObj *task, PyObject *exc)
- {
- int res;
- int clear_exc = 0;
- PyObject *result = NULL;
- PyObject *coro;
- PyObject *o;
- if (task->task_state != STATE_PENDING) {
- PyErr_Format(state->asyncio_InvalidStateError,
- "_step(): already done: %R %R",
- task,
- exc ? exc : Py_None);
- goto fail;
- }
- if (task->task_must_cancel) {
- assert(exc != Py_None);
- if (exc) {
- /* Check if exc is a CancelledError */
- res = PyObject_IsInstance(exc, state->asyncio_CancelledError);
- if (res == -1) {
- /* An error occurred, abort */
- goto fail;
- }
- if (res == 0) {
- /* exc is not CancelledError; reset it to NULL */
- exc = NULL;
- }
- }
- if (!exc) {
- /* exc was not a CancelledError */
- exc = create_cancelled_error(state, (FutureObj*)task);
- if (!exc) {
- goto fail;
- }
- clear_exc = 1;
- }
- task->task_must_cancel = 0;
- }
- Py_CLEAR(task->task_fut_waiter);
- coro = task->task_coro;
- if (coro == NULL) {
- PyErr_SetString(PyExc_RuntimeError, "uninitialized Task object");
- if (clear_exc) {
- /* We created 'exc' during this call */
- Py_DECREF(exc);
- }
- return NULL;
- }
- int gen_status = PYGEN_ERROR;
- if (exc == NULL) {
- gen_status = PyIter_Send(coro, Py_None, &result);
- }
- else {
- result = PyObject_CallMethodOneArg(coro, &_Py_ID(throw), exc);
- gen_status = gen_status_from_result(&result);
- if (clear_exc) {
- /* We created 'exc' during this call */
- Py_DECREF(exc);
- }
- }
- if (gen_status == PYGEN_RETURN || gen_status == PYGEN_ERROR) {
- if (result != NULL) {
- /* The error is StopIteration and that means that
- the underlying coroutine has resolved */
- PyObject *tmp;
- if (task->task_must_cancel) {
- // Task is cancelled right before coro stops.
- task->task_must_cancel = 0;
- tmp = future_cancel(state, (FutureObj*)task,
- task->task_cancel_msg);
- }
- else {
- tmp = future_set_result(state, (FutureObj*)task, result);
- }
- Py_DECREF(result);
- if (tmp == NULL) {
- return NULL;
- }
- Py_DECREF(tmp);
- Py_RETURN_NONE;
- }
- if (PyErr_ExceptionMatches(state->asyncio_CancelledError)) {
- /* CancelledError */
- PyObject *exc = PyErr_GetRaisedException();
- assert(exc);
- FutureObj *fut = (FutureObj*)task;
- /* transfer ownership */
- fut->fut_cancelled_exc = exc;
- return future_cancel(state, fut, NULL);
- }
- /* Some other exception; pop it and call Task.set_exception() */
- PyObject *exc = PyErr_GetRaisedException();
- assert(exc);
- o = future_set_exception(state, (FutureObj*)task, exc);
- if (!o) {
- /* An exception in Task.set_exception() */
- Py_DECREF(exc);
- goto fail;
- }
- assert(o == Py_None);
- Py_DECREF(o);
- if (PyErr_GivenExceptionMatches(exc, PyExc_KeyboardInterrupt) ||
- PyErr_GivenExceptionMatches(exc, PyExc_SystemExit))
- {
- /* We've got a KeyboardInterrupt or a SystemError; re-raise it */
- PyErr_SetRaisedException(exc);
- goto fail;
- }
- Py_DECREF(exc);
- Py_RETURN_NONE;
- }
- PyObject *ret = task_step_handle_result_impl(state, task, result);
- return ret;
- fail:
- return NULL;
- }
- static PyObject *
- task_step_handle_result_impl(asyncio_state *state, TaskObj *task, PyObject *result)
- {
- int res;
- PyObject *o;
- if (result == (PyObject*)task) {
- /* We have a task that wants to await on itself */
- goto self_await;
- }
- /* Check if `result` is FutureObj or TaskObj (and not a subclass) */
- if (Future_CheckExact(state, result) || Task_CheckExact(state, result)) {
- PyObject *wrapper;
- PyObject *tmp;
- FutureObj *fut = (FutureObj*)result;
- /* Check if `result` future is attached to a different loop */
- if (fut->fut_loop != task->task_loop) {
- goto different_loop;
- }
- if (!fut->fut_blocking) {
- goto yield_insteadof_yf;
- }
- fut->fut_blocking = 0;
- /* result.add_done_callback(task._wakeup) */
- wrapper = PyCFunction_New(&TaskWakeupDef, (PyObject *)task);
- if (wrapper == NULL) {
- goto fail;
- }
- tmp = future_add_done_callback(state,
- (FutureObj*)result, wrapper, task->task_context);
- Py_DECREF(wrapper);
- if (tmp == NULL) {
- goto fail;
- }
- Py_DECREF(tmp);
- /* task._fut_waiter = result */
- task->task_fut_waiter = result; /* no incref is necessary */
- if (task->task_must_cancel) {
- PyObject *r;
- int is_true;
- // Beware: An evil `__getattribute__` could
- // prematurely delete task->task_cancel_msg before the
- // task is cancelled, thereby causing a UAF crash.
- //
- // See https://github.com/python/cpython/issues/126138
- PyObject *task_cancel_msg = Py_NewRef(task->task_cancel_msg);
- r = PyObject_CallMethodOneArg(result, &_Py_ID(cancel),
- task_cancel_msg);
- Py_DECREF(task_cancel_msg);
- if (r == NULL) {
- return NULL;
- }
- is_true = PyObject_IsTrue(r);
- Py_DECREF(r);
- if (is_true < 0) {
- return NULL;
- }
- else if (is_true) {
- task->task_must_cancel = 0;
- }
- }
- Py_RETURN_NONE;
- }
- /* Check if `result` is None */
- if (result == Py_None) {
- /* Bare yield relinquishes control for one event loop iteration. */
- if (task_call_step_soon(state, task, NULL)) {
- goto fail;
- }
- return result;
- }
- /* Check if `result` is a Future-compatible object */
- if (_PyObject_LookupAttr(result, &_Py_ID(_asyncio_future_blocking), &o) < 0) {
- goto fail;
- }
- if (o != NULL && o != Py_None) {
- /* `result` is a Future-compatible object */
- PyObject *wrapper;
- PyObject *tmp;
- int blocking = PyObject_IsTrue(o);
- Py_DECREF(o);
- if (blocking < 0) {
- goto fail;
- }
- /* Check if `result` future is attached to a different loop */
- PyObject *oloop = get_future_loop(state, result);
- if (oloop == NULL) {
- goto fail;
- }
- if (oloop != task->task_loop) {
- Py_DECREF(oloop);
- goto different_loop;
- }
- Py_DECREF(oloop);
- if (!blocking) {
- goto yield_insteadof_yf;
- }
- /* result._asyncio_future_blocking = False */
- if (PyObject_SetAttr(
- result, &_Py_ID(_asyncio_future_blocking), Py_False) == -1) {
- goto fail;
- }
- wrapper = PyCFunction_New(&TaskWakeupDef, (PyObject *)task);
- if (wrapper == NULL) {
- goto fail;
- }
- /* result.add_done_callback(task._wakeup) */
- PyObject *add_cb = PyObject_GetAttr(
- result, &_Py_ID(add_done_callback));
- if (add_cb == NULL) {
- Py_DECREF(wrapper);
- goto fail;
- }
- PyObject *stack[2];
- stack[0] = wrapper;
- stack[1] = (PyObject *)task->task_context;
- EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, add_cb);
- tmp = PyObject_Vectorcall(add_cb, stack, 1, state->context_kwname);
- Py_DECREF(add_cb);
- Py_DECREF(wrapper);
- if (tmp == NULL) {
- goto fail;
- }
- Py_DECREF(tmp);
- /* task._fut_waiter = result */
- task->task_fut_waiter = result; /* no incref is necessary */
- if (task->task_must_cancel) {
- PyObject *r;
- int is_true;
- // Beware: An evil `__getattribute__` could
- // prematurely delete task->task_cancel_msg before the
- // task is cancelled, thereby causing a UAF crash.
- //
- // See https://github.com/python/cpython/issues/126138
- PyObject *task_cancel_msg = Py_NewRef(task->task_cancel_msg);
- r = PyObject_CallMethodOneArg(result, &_Py_ID(cancel),
- task_cancel_msg);
- Py_DECREF(task_cancel_msg);
- if (r == NULL) {
- return NULL;
- }
- is_true = PyObject_IsTrue(r);
- Py_DECREF(r);
- if (is_true < 0) {
- return NULL;
- }
- else if (is_true) {
- task->task_must_cancel = 0;
- }
- }
- Py_RETURN_NONE;
- }
- Py_XDECREF(o);
- /* Check if `result` is a generator */
- res = PyObject_IsInstance(result, (PyObject*)&PyGen_Type);
- if (res < 0) {
- goto fail;
- }
- if (res) {
- /* `result` is a generator */
- o = task_set_error_soon(
- state, task, PyExc_RuntimeError,
- "yield was used instead of yield from for "
- "generator in task %R with %R", task, result);
- Py_DECREF(result);
- return o;
- }
- /* The `result` is none of the above */
- o = task_set_error_soon(
- state, task, PyExc_RuntimeError, "Task got bad yield: %R", result);
- Py_DECREF(result);
- return o;
- self_await:
- o = task_set_error_soon(
- state, task, PyExc_RuntimeError,
- "Task cannot await on itself: %R", task);
- Py_DECREF(result);
- return o;
- yield_insteadof_yf:
- o = task_set_error_soon(
- state, task, PyExc_RuntimeError,
- "yield was used instead of yield from "
- "in task %R with %R",
- task, result);
- Py_DECREF(result);
- return o;
- different_loop:
- o = task_set_error_soon(
- state, task, PyExc_RuntimeError,
- "Task %R got Future %R attached to a different loop",
- task, result);
- Py_DECREF(result);
- return o;
- fail:
- Py_XDECREF(result);
- return NULL;
- }
- static PyObject *
- task_step(asyncio_state *state, TaskObj *task, PyObject *exc)
- {
- PyObject *res;
- if (enter_task(state, task->task_loop, (PyObject*)task) < 0) {
- return NULL;
- }
- res = task_step_impl(state, task, exc);
- if (res == NULL) {
- PyObject *exc = PyErr_GetRaisedException();
- leave_task(state, task->task_loop, (PyObject*)task);
- _PyErr_ChainExceptions1(exc);
- return NULL;
- }
- else {
- if (leave_task(state, task->task_loop, (PyObject*)task) < 0) {
- Py_DECREF(res);
- return NULL;
- }
- else {
- return res;
- }
- }
- }
- static int
- task_eager_start(asyncio_state *state, TaskObj *task)
- {
- assert(task != NULL);
- PyObject *prevtask = swap_current_task(state, task->task_loop, (PyObject *)task);
- if (prevtask == NULL) {
- return -1;
- }
- if (register_eager_task(state, (PyObject *)task) == -1) {
- Py_DECREF(prevtask);
- return -1;
- }
- if (PyContext_Enter(task->task_context) == -1) {
- Py_DECREF(prevtask);
- return -1;
- }
- int retval = 0;
- PyObject *stepres = task_step_impl(state, task, NULL);
- if (stepres == NULL) {
- PyObject *exc = PyErr_GetRaisedException();
- _PyErr_ChainExceptions1(exc);
- retval = -1;
- } else {
- Py_DECREF(stepres);
- }
- PyObject *curtask = swap_current_task(state, task->task_loop, prevtask);
- Py_DECREF(prevtask);
- if (curtask == NULL) {
- retval = -1;
- } else {
- assert(curtask == (PyObject *)task);
- Py_DECREF(curtask);
- }
- if (unregister_eager_task(state, (PyObject *)task) == -1) {
- retval = -1;
- }
- if (PyContext_Exit(task->task_context) == -1) {
- retval = -1;
- }
- if (task->task_state == STATE_PENDING) {
- if (register_task(state, (PyObject *)task) == -1) {
- retval = -1;
- }
- } else {
- // This seems to really help performance on pyperformance benchmarks
- Py_CLEAR(task->task_coro);
- }
- return retval;
- }
- static PyObject *
- task_wakeup(TaskObj *task, PyObject *o)
- {
- PyObject *result;
- assert(o);
- asyncio_state *state = get_asyncio_state_by_def((PyObject *)task);
- if (Future_CheckExact(state, o) || Task_CheckExact(state, o)) {
- PyObject *fut_result = NULL;
- int res = future_get_result(state, (FutureObj*)o, &fut_result);
- switch(res) {
- case -1:
- assert(fut_result == NULL);
- break; /* exception raised */
- case 0:
- Py_DECREF(fut_result);
- return task_step(state, task, NULL);
- default:
- assert(res == 1);
- result = task_step(state, task, fut_result);
- Py_DECREF(fut_result);
- return result;
- }
- }
- else {
- PyObject *fut_result = PyObject_CallMethod(o, "result", NULL);
- if (fut_result != NULL) {
- Py_DECREF(fut_result);
- return task_step(state, task, NULL);
- }
- /* exception raised */
- }
- PyObject *exc = PyErr_GetRaisedException();
- assert(exc);
- result = task_step(state, task, exc);
- Py_DECREF(exc);
- return result;
- }
- /*********************** Functions **************************/
- /*[clinic input]
- _asyncio._get_running_loop
- Return the running event loop or None.
- This is a low-level function intended to be used by event loops.
- This function is thread-specific.
- [clinic start generated code]*/
- static PyObject *
- _asyncio__get_running_loop_impl(PyObject *module)
- /*[clinic end generated code: output=b4390af721411a0a input=0a21627e25a4bd43]*/
- {
- PyObject *loop;
- asyncio_state *state = get_asyncio_state(module);
- if (get_running_loop(state, &loop)) {
- return NULL;
- }
- if (loop == NULL) {
- /* There's no currently running event loop */
- Py_RETURN_NONE;
- }
- return loop;
- }
- /*[clinic input]
- _asyncio._set_running_loop
- loop: 'O'
- /
- Set the running event loop.
- This is a low-level function intended to be used by event loops.
- This function is thread-specific.
- [clinic start generated code]*/
- static PyObject *
- _asyncio__set_running_loop(PyObject *module, PyObject *loop)
- /*[clinic end generated code: output=ae56bf7a28ca189a input=4c9720233d606604]*/
- {
- asyncio_state *state = get_asyncio_state(module);
- if (set_running_loop(state, loop)) {
- return NULL;
- }
- Py_RETURN_NONE;
- }
- /*[clinic input]
- _asyncio.get_event_loop
- Return an asyncio event loop.
- When called from a coroutine or a callback (e.g. scheduled with
- call_soon or similar API), this function will always return the
- running event loop.
- If there is no running event loop set, the function will return
- the result of `get_event_loop_policy().get_event_loop()` call.
- [clinic start generated code]*/
- static PyObject *
- _asyncio_get_event_loop_impl(PyObject *module)
- /*[clinic end generated code: output=2a2d8b2f824c648b input=9364bf2916c8655d]*/
- {
- asyncio_state *state = get_asyncio_state(module);
- return get_event_loop(state);
- }
- /*[clinic input]
- _asyncio.get_running_loop
- Return the running event loop. Raise a RuntimeError if there is none.
- This function is thread-specific.
- [clinic start generated code]*/
- static PyObject *
- _asyncio_get_running_loop_impl(PyObject *module)
- /*[clinic end generated code: output=c247b5f9e529530e input=2a3bf02ba39f173d]*/
- {
- PyObject *loop;
- asyncio_state *state = get_asyncio_state(module);
- if (get_running_loop(state, &loop)) {
- return NULL;
- }
- if (loop == NULL) {
- /* There's no currently running event loop */
- PyErr_SetString(
- PyExc_RuntimeError, "no running event loop");
- }
- return loop;
- }
- /*[clinic input]
- _asyncio._register_task
- task: object
- Register a new task in asyncio as executed by loop.
- Returns None.
- [clinic start generated code]*/
- static PyObject *
- _asyncio__register_task_impl(PyObject *module, PyObject *task)
- /*[clinic end generated code: output=8672dadd69a7d4e2 input=21075aaea14dfbad]*/
- {
- asyncio_state *state = get_asyncio_state(module);
- if (register_task(state, task) < 0) {
- return NULL;
- }
- Py_RETURN_NONE;
- }
- /*[clinic input]
- _asyncio._register_eager_task
- task: object
- Register a new task in asyncio as executed by loop.
- Returns None.
- [clinic start generated code]*/
- static PyObject *
- _asyncio__register_eager_task_impl(PyObject *module, PyObject *task)
- /*[clinic end generated code: output=dfe1d45367c73f1a input=237f684683398c51]*/
- {
- asyncio_state *state = get_asyncio_state(module);
- if (register_eager_task(state, task) < 0) {
- return NULL;
- }
- Py_RETURN_NONE;
- }
- /*[clinic input]
- _asyncio._unregister_task
- task: object
- Unregister a task.
- Returns None.
- [clinic start generated code]*/
- static PyObject *
- _asyncio__unregister_task_impl(PyObject *module, PyObject *task)
- /*[clinic end generated code: output=6e5585706d568a46 input=28fb98c3975f7bdc]*/
- {
- asyncio_state *state = get_asyncio_state(module);
- if (unregister_task(state, task) < 0) {
- return NULL;
- }
- Py_RETURN_NONE;
- }
- /*[clinic input]
- _asyncio._unregister_eager_task
- task: object
- Unregister a task.
- Returns None.
- [clinic start generated code]*/
- static PyObject *
- _asyncio__unregister_eager_task_impl(PyObject *module, PyObject *task)
- /*[clinic end generated code: output=a426922bd07f23d1 input=9d07401ef14ee048]*/
- {
- asyncio_state *state = get_asyncio_state(module);
- if (unregister_eager_task(state, task) < 0) {
- return NULL;
- }
- Py_RETURN_NONE;
- }
- /*[clinic input]
- _asyncio._enter_task
- loop: object
- task: object
- Enter into task execution or resume suspended task.
- Task belongs to loop.
- Returns None.
- [clinic start generated code]*/
- static PyObject *
- _asyncio__enter_task_impl(PyObject *module, PyObject *loop, PyObject *task)
- /*[clinic end generated code: output=a22611c858035b73 input=de1b06dca70d8737]*/
- {
- asyncio_state *state = get_asyncio_state(module);
- if (enter_task(state, loop, task) < 0) {
- return NULL;
- }
- Py_RETURN_NONE;
- }
- /*[clinic input]
- _asyncio._leave_task
- loop: object
- task: object
- Leave task execution or suspend a task.
- Task belongs to loop.
- Returns None.
- [clinic start generated code]*/
- static PyObject *
- _asyncio__leave_task_impl(PyObject *module, PyObject *loop, PyObject *task)
- /*[clinic end generated code: output=0ebf6db4b858fb41 input=51296a46313d1ad8]*/
- {
- asyncio_state *state = get_asyncio_state(module);
- if (leave_task(state, loop, task) < 0) {
- return NULL;
- }
- Py_RETURN_NONE;
- }
- /*[clinic input]
- _asyncio._swap_current_task
- loop: object
- task: object
- Temporarily swap in the supplied task and return the original one (or None).
- This is intended for use during eager coroutine execution.
- [clinic start generated code]*/
- static PyObject *
- _asyncio__swap_current_task_impl(PyObject *module, PyObject *loop,
- PyObject *task)
- /*[clinic end generated code: output=9f88de958df74c7e input=c9c72208d3d38b6c]*/
- {
- return swap_current_task(get_asyncio_state(module), loop, task);
- }
- /*[clinic input]
- _asyncio.current_task
- loop: object = None
- Return a currently executed task.
- [clinic start generated code]*/
- static PyObject *
- _asyncio_current_task_impl(PyObject *module, PyObject *loop)
- /*[clinic end generated code: output=fe15ac331a7f981a input=58910f61a5627112]*/
- {
- PyObject *ret;
- asyncio_state *state = get_asyncio_state(module);
- if (loop == Py_None) {
- loop = _asyncio_get_running_loop_impl(module);
- if (loop == NULL) {
- return NULL;
- }
- } else {
- Py_INCREF(loop);
- }
- ret = PyDict_GetItemWithError(state->current_tasks, loop);
- Py_DECREF(loop);
- if (ret == NULL && PyErr_Occurred()) {
- return NULL;
- }
- else if (ret == NULL) {
- Py_RETURN_NONE;
- }
- Py_INCREF(ret);
- return ret;
- }
- /*********************** Module **************************/
- static void
- module_free_freelists(asyncio_state *state)
- {
- PyObject *next;
- PyObject *current;
- next = (PyObject*) state->fi_freelist;
- while (next != NULL) {
- assert(state->fi_freelist_len > 0);
- state->fi_freelist_len--;
- current = next;
- next = (PyObject*) ((futureiterobject*) current)->future;
- PyObject_GC_Del(current);
- }
- assert(state->fi_freelist_len == 0);
- state->fi_freelist = NULL;
- }
- static int
- module_traverse(PyObject *mod, visitproc visit, void *arg)
- {
- asyncio_state *state = get_asyncio_state(mod);
- Py_VISIT(state->FutureIterType);
- Py_VISIT(state->TaskStepMethWrapper_Type);
- Py_VISIT(state->FutureType);
- Py_VISIT(state->TaskType);
- Py_VISIT(state->asyncio_mod);
- Py_VISIT(state->traceback_extract_stack);
- Py_VISIT(state->asyncio_future_repr_func);
- Py_VISIT(state->asyncio_get_event_loop_policy);
- Py_VISIT(state->asyncio_iscoroutine_func);
- Py_VISIT(state->asyncio_task_get_stack_func);
- Py_VISIT(state->asyncio_task_print_stack_func);
- Py_VISIT(state->asyncio_task_repr_func);
- Py_VISIT(state->asyncio_InvalidStateError);
- Py_VISIT(state->asyncio_CancelledError);
- Py_VISIT(state->scheduled_tasks);
- Py_VISIT(state->eager_tasks);
- Py_VISIT(state->current_tasks);
- Py_VISIT(state->iscoroutine_typecache);
- Py_VISIT(state->context_kwname);
- return 0;
- }
- static int
- module_clear(PyObject *mod)
- {
- asyncio_state *state = get_asyncio_state(mod);
- Py_CLEAR(state->FutureIterType);
- Py_CLEAR(state->TaskStepMethWrapper_Type);
- Py_CLEAR(state->FutureType);
- Py_CLEAR(state->TaskType);
- Py_CLEAR(state->asyncio_mod);
- Py_CLEAR(state->traceback_extract_stack);
- Py_CLEAR(state->asyncio_future_repr_func);
- Py_CLEAR(state->asyncio_get_event_loop_policy);
- Py_CLEAR(state->asyncio_iscoroutine_func);
- Py_CLEAR(state->asyncio_task_get_stack_func);
- Py_CLEAR(state->asyncio_task_print_stack_func);
- Py_CLEAR(state->asyncio_task_repr_func);
- Py_CLEAR(state->asyncio_InvalidStateError);
- Py_CLEAR(state->asyncio_CancelledError);
- Py_CLEAR(state->scheduled_tasks);
- Py_CLEAR(state->eager_tasks);
- Py_CLEAR(state->current_tasks);
- Py_CLEAR(state->iscoroutine_typecache);
- Py_CLEAR(state->context_kwname);
- module_free_freelists(state);
- return 0;
- }
- static void
- module_free(void *mod)
- {
- (void)module_clear((PyObject *)mod);
- }
- static int
- module_init(asyncio_state *state)
- {
- PyObject *module = NULL;
- state->asyncio_mod = PyImport_ImportModule("asyncio");
- if (state->asyncio_mod == NULL) {
- goto fail;
- }
- state->current_tasks = PyDict_New();
- if (state->current_tasks == NULL) {
- goto fail;
- }
- state->iscoroutine_typecache = PySet_New(NULL);
- if (state->iscoroutine_typecache == NULL) {
- goto fail;
- }
- state->context_kwname = Py_BuildValue("(s)", "context");
- if (state->context_kwname == NULL) {
- goto fail;
- }
- #define WITH_MOD(NAME) \
- Py_CLEAR(module); \
- module = PyImport_ImportModule(NAME); \
- if (module == NULL) { \
- goto fail; \
- }
- #define GET_MOD_ATTR(VAR, NAME) \
- VAR = PyObject_GetAttrString(module, NAME); \
- if (VAR == NULL) { \
- goto fail; \
- }
- WITH_MOD("asyncio.events")
- GET_MOD_ATTR(state->asyncio_get_event_loop_policy, "get_event_loop_policy")
- WITH_MOD("asyncio.base_futures")
- GET_MOD_ATTR(state->asyncio_future_repr_func, "_future_repr")
- WITH_MOD("asyncio.exceptions")
- GET_MOD_ATTR(state->asyncio_InvalidStateError, "InvalidStateError")
- GET_MOD_ATTR(state->asyncio_CancelledError, "CancelledError")
- WITH_MOD("asyncio.base_tasks")
- GET_MOD_ATTR(state->asyncio_task_repr_func, "_task_repr")
- GET_MOD_ATTR(state->asyncio_task_get_stack_func, "_task_get_stack")
- GET_MOD_ATTR(state->asyncio_task_print_stack_func, "_task_print_stack")
- WITH_MOD("asyncio.coroutines")
- GET_MOD_ATTR(state->asyncio_iscoroutine_func, "iscoroutine")
- WITH_MOD("traceback")
- GET_MOD_ATTR(state->traceback_extract_stack, "extract_stack")
- PyObject *weak_set;
- WITH_MOD("weakref")
- GET_MOD_ATTR(weak_set, "WeakSet");
- state->scheduled_tasks = PyObject_CallNoArgs(weak_set);
- Py_CLEAR(weak_set);
- if (state->scheduled_tasks == NULL) {
- goto fail;
- }
- state->eager_tasks = PySet_New(NULL);
- if (state->eager_tasks == NULL) {
- goto fail;
- }
- Py_DECREF(module);
- return 0;
- fail:
- Py_CLEAR(module);
- return -1;
- #undef WITH_MOD
- #undef GET_MOD_ATTR
- }
- PyDoc_STRVAR(module_doc, "Accelerator module for asyncio");
- static PyMethodDef asyncio_methods[] = {
- _ASYNCIO_CURRENT_TASK_METHODDEF
- _ASYNCIO_GET_EVENT_LOOP_METHODDEF
- _ASYNCIO_GET_RUNNING_LOOP_METHODDEF
- _ASYNCIO__GET_RUNNING_LOOP_METHODDEF
- _ASYNCIO__SET_RUNNING_LOOP_METHODDEF
- _ASYNCIO__REGISTER_TASK_METHODDEF
- _ASYNCIO__REGISTER_EAGER_TASK_METHODDEF
- _ASYNCIO__UNREGISTER_TASK_METHODDEF
- _ASYNCIO__UNREGISTER_EAGER_TASK_METHODDEF
- _ASYNCIO__ENTER_TASK_METHODDEF
- _ASYNCIO__LEAVE_TASK_METHODDEF
- _ASYNCIO__SWAP_CURRENT_TASK_METHODDEF
- {NULL, NULL}
- };
- static int
- module_exec(PyObject *mod)
- {
- asyncio_state *state = get_asyncio_state(mod);
- #define CREATE_TYPE(m, tp, spec, base) \
- do { \
- tp = (PyTypeObject *)PyType_FromMetaclass(NULL, m, spec, \
- (PyObject *)base); \
- if (tp == NULL) { \
- return -1; \
- } \
- } while (0)
- CREATE_TYPE(mod, state->TaskStepMethWrapper_Type, &TaskStepMethWrapper_spec, NULL);
- CREATE_TYPE(mod, state->FutureIterType, &FutureIter_spec, NULL);
- CREATE_TYPE(mod, state->FutureType, &Future_spec, NULL);
- CREATE_TYPE(mod, state->TaskType, &Task_spec, state->FutureType);
- #undef CREATE_TYPE
- if (PyModule_AddType(mod, state->FutureType) < 0) {
- return -1;
- }
- if (PyModule_AddType(mod, state->TaskType) < 0) {
- return -1;
- }
- // Must be done after types are added to avoid a circular dependency
- if (module_init(state) < 0) {
- return -1;
- }
- if (PyModule_AddObjectRef(mod, "_scheduled_tasks", state->scheduled_tasks) < 0) {
- return -1;
- }
- if (PyModule_AddObjectRef(mod, "_eager_tasks", state->eager_tasks) < 0) {
- return -1;
- }
- if (PyModule_AddObjectRef(mod, "_current_tasks", state->current_tasks) < 0) {
- return -1;
- }
- return 0;
- }
- static struct PyModuleDef_Slot module_slots[] = {
- {Py_mod_exec, module_exec},
- {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
- {0, NULL},
- };
- static struct PyModuleDef _asynciomodule = {
- .m_base = PyModuleDef_HEAD_INIT,
- .m_name = "_asyncio",
- .m_doc = module_doc,
- .m_size = sizeof(asyncio_state),
- .m_methods = asyncio_methods,
- .m_slots = module_slots,
- .m_traverse = module_traverse,
- .m_clear = module_clear,
- .m_free = (freefunc)module_free,
- };
- PyMODINIT_FUNC
- PyInit__asyncio(void)
- {
- return PyModuleDef_Init(&_asynciomodule);
- }
|