forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_asynciomodule.c
2470 lines (2073 loc) · 61.5 KB
/
_asynciomodule.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Python.h"
#include "structmember.h"
/*[clinic input]
module _asyncio
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=8fd17862aa989c69]*/
/* identifiers used from some functions */
_Py_IDENTIFIER(add_done_callback);
_Py_IDENTIFIER(call_soon);
_Py_IDENTIFIER(cancel);
_Py_IDENTIFIER(send);
_Py_IDENTIFIER(throw);
_Py_IDENTIFIER(_step);
_Py_IDENTIFIER(_schedule_callbacks);
_Py_IDENTIFIER(_wakeup);
/* State of the _asyncio module */
static PyObject *all_tasks;
static PyObject *current_tasks;
static PyObject *traceback_extract_stack;
static PyObject *asyncio_get_event_loop;
static PyObject *asyncio_future_repr_info_func;
static PyObject *asyncio_task_repr_info_func;
static PyObject *asyncio_task_get_stack_func;
static PyObject *asyncio_task_print_stack_func;
static PyObject *asyncio_InvalidStateError;
static PyObject *asyncio_CancelledError;
static PyObject *inspect_isgenerator;
typedef enum {
STATE_PENDING,
STATE_CANCELLED,
STATE_FINISHED
} fut_state;
#define FutureObj_HEAD(prefix) \
PyObject_HEAD \
PyObject *prefix##_loop; \
PyObject *prefix##_callbacks; \
PyObject *prefix##_exception; \
PyObject *prefix##_result; \
PyObject *prefix##_source_tb; \
fut_state prefix##_state; \
int prefix##_log_tb; \
int prefix##_blocking; \
PyObject *dict; \
PyObject *prefix##_weakreflist;
typedef struct {
FutureObj_HEAD(fut)
} FutureObj;
typedef struct {
FutureObj_HEAD(task)
PyObject *task_fut_waiter;
PyObject *task_coro;
int task_must_cancel;
int task_log_destroy_pending;
} TaskObj;
typedef struct {
PyObject_HEAD
TaskObj *sw_task;
PyObject *sw_arg;
} TaskSendMethWrapper;
typedef struct {
PyObject_HEAD
TaskObj *ww_task;
} TaskWakeupMethWrapper;
#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 inline int future_call_schedule_callbacks(FutureObj *);
static int
future_schedule_callbacks(FutureObj *fut)
{
Py_ssize_t len;
PyObject* iters;
int i;
if (fut->fut_callbacks == NULL) {
PyErr_SetString(PyExc_RuntimeError, "NULL callbacks");
return -1;
}
len = PyList_GET_SIZE(fut->fut_callbacks);
if (len == 0) {
return 0;
}
iters = PyList_GetSlice(fut->fut_callbacks, 0, len);
if (iters == NULL) {
return -1;
}
if (PyList_SetSlice(fut->fut_callbacks, 0, len, NULL) < 0) {
Py_DECREF(iters);
return -1;
}
for (i = 0; i < len; i++) {
PyObject *handle = NULL;
PyObject *cb = PyList_GET_ITEM(iters, i);
handle = _PyObject_CallMethodIdObjArgs(fut->fut_loop, &PyId_call_soon,
cb, fut, NULL);
if (handle == NULL) {
Py_DECREF(iters);
return -1;
}
else {
Py_DECREF(handle);
}
}
Py_DECREF(iters);
return 0;
}
static int
future_init(FutureObj *fut, PyObject *loop)
{
PyObject *res = NULL;
_Py_IDENTIFIER(get_debug);
if (loop == NULL || loop == Py_None) {
loop = _PyObject_CallNoArg(asyncio_get_event_loop);
if (loop == NULL) {
return -1;
}
}
else {
Py_INCREF(loop);
}
Py_CLEAR(fut->fut_loop);
fut->fut_loop = loop;
res = _PyObject_CallMethodId(fut->fut_loop, &PyId_get_debug, NULL);
if (res == NULL) {
return -1;
}
if (PyObject_IsTrue(res)) {
Py_CLEAR(res);
fut->fut_source_tb = _PyObject_CallNoArg(traceback_extract_stack);
if (fut->fut_source_tb == NULL) {
return -1;
}
}
else {
Py_CLEAR(res);
}
fut->fut_callbacks = PyList_New(0);
if (fut->fut_callbacks == NULL) {
return -1;
}
return 0;
}
static PyObject *
future_set_result(FutureObj *fut, PyObject *res)
{
if (fut->fut_state != STATE_PENDING) {
PyErr_SetString(asyncio_InvalidStateError, "invalid state");
return NULL;
}
Py_INCREF(res);
fut->fut_result = res;
fut->fut_state = STATE_FINISHED;
if (future_call_schedule_callbacks(fut) == -1) {
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *
future_set_exception(FutureObj *fut, PyObject *exc)
{
PyObject *exc_val = NULL;
if (fut->fut_state != STATE_PENDING) {
PyErr_SetString(asyncio_InvalidStateError, "invalid state");
return NULL;
}
if (PyExceptionClass_Check(exc)) {
exc_val = _PyObject_CallNoArg(exc);
if (exc_val == NULL) {
return NULL;
}
}
else {
exc_val = exc;
Py_INCREF(exc_val);
}
if (!PyExceptionInstance_Check(exc_val)) {
Py_DECREF(exc_val);
PyErr_SetString(PyExc_TypeError, "invalid exception object");
return NULL;
}
if ((PyObject*)Py_TYPE(exc_val) == PyExc_StopIteration) {
Py_DECREF(exc_val);
PyErr_SetString(PyExc_TypeError,
"StopIteration interacts badly with generators "
"and cannot be raised into a Future");
return NULL;
}
fut->fut_exception = exc_val;
fut->fut_state = STATE_FINISHED;
if (future_call_schedule_callbacks(fut) == -1) {
return NULL;
}
fut->fut_log_tb = 1;
Py_RETURN_NONE;
}
static int
future_get_result(FutureObj *fut, PyObject **result)
{
PyObject *exc;
if (fut->fut_state == STATE_CANCELLED) {
exc = _PyObject_CallNoArg(asyncio_CancelledError);
if (exc == NULL) {
return -1;
}
*result = exc;
return 1;
}
if (fut->fut_state != STATE_FINISHED) {
PyObject *msg = PyUnicode_FromString("Result is not ready.");
if (msg == NULL) {
return -1;
}
exc = PyObject_CallFunctionObjArgs(asyncio_InvalidStateError, msg, NULL);
Py_DECREF(msg);
if (exc == NULL) {
return -1;
}
*result = exc;
return 1;
}
fut->fut_log_tb = 0;
if (fut->fut_exception != NULL) {
Py_INCREF(fut->fut_exception);
*result = fut->fut_exception;
return 1;
}
Py_INCREF(fut->fut_result);
*result = fut->fut_result;
return 0;
}
static PyObject *
future_add_done_callback(FutureObj *fut, PyObject *arg)
{
if (fut->fut_state != STATE_PENDING) {
PyObject *handle = _PyObject_CallMethodIdObjArgs(fut->fut_loop,
&PyId_call_soon,
arg, fut, NULL);
if (handle == NULL) {
return NULL;
}
else {
Py_DECREF(handle);
}
}
else {
int err = PyList_Append(fut->fut_callbacks, arg);
if (err != 0) {
return NULL;
}
}
Py_RETURN_NONE;
}
static PyObject *
future_cancel(FutureObj *fut)
{
if (fut->fut_state != STATE_PENDING) {
Py_RETURN_FALSE;
}
fut->fut_state = STATE_CANCELLED;
if (future_call_schedule_callbacks(fut) == -1) {
return NULL;
}
Py_RETURN_TRUE;
}
/*[clinic input]
_asyncio.Future.__init__
*
loop: 'O' = NULL
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=8e1681f23605be2d]*/
{
return future_init(self, loop);
}
static int
FutureObj_clear(FutureObj *fut)
{
Py_CLEAR(fut->fut_loop);
Py_CLEAR(fut->fut_callbacks);
Py_CLEAR(fut->fut_result);
Py_CLEAR(fut->fut_exception);
Py_CLEAR(fut->fut_source_tb);
Py_CLEAR(fut->dict);
return 0;
}
static int
FutureObj_traverse(FutureObj *fut, visitproc visit, void *arg)
{
Py_VISIT(fut->fut_loop);
Py_VISIT(fut->fut_callbacks);
Py_VISIT(fut->fut_result);
Py_VISIT(fut->fut_exception);
Py_VISIT(fut->fut_source_tb);
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]*/
{
PyObject *result;
int res = future_get_result(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
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)
/*[clinic end generated code: output=88b20d4f855e0710 input=733547a70c841c68]*/
{
if (self->fut_state == STATE_CANCELLED) {
PyErr_SetString(asyncio_CancelledError, "");
return NULL;
}
if (self->fut_state != STATE_FINISHED) {
PyErr_SetString(asyncio_InvalidStateError, "Result is not ready.");
return NULL;
}
if (self->fut_exception != NULL) {
self->fut_log_tb = 0;
Py_INCREF(self->fut_exception);
return self->fut_exception;
}
Py_RETURN_NONE;
}
/*[clinic input]
_asyncio.Future.set_result
res: 'O'
/
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(FutureObj *self, PyObject *res)
/*[clinic end generated code: output=a620abfc2796bfb6 input=8619565e0503357e]*/
{
return future_set_result(self, res);
}
/*[clinic input]
_asyncio.Future.set_exception
exception: 'O'
/
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(FutureObj *self, PyObject *exception)
/*[clinic end generated code: output=f1c1b0cd321be360 input=1377dbe15e6ea186]*/
{
return future_set_exception(self, exception);
}
/*[clinic input]
_asyncio.Future.add_done_callback
fn: 'O'
/
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(FutureObj *self, PyObject *fn)
/*[clinic end generated code: output=819e09629b2ec2b5 input=8cce187e32cec6a8]*/
{
return future_add_done_callback(self, fn);
}
/*[clinic input]
_asyncio.Future.remove_done_callback
fn: 'O'
/
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(FutureObj *self, PyObject *fn)
/*[clinic end generated code: output=5ab1fb52b24ef31f input=3fedb73e1409c31c]*/
{
PyObject *newlist;
Py_ssize_t len, i, j=0;
len = PyList_GET_SIZE(self->fut_callbacks);
if (len == 0) {
return PyLong_FromSsize_t(0);
}
newlist = PyList_New(len);
if (newlist == NULL) {
return NULL;
}
for (i = 0; i < PyList_GET_SIZE(self->fut_callbacks); i++) {
int ret;
PyObject *item = PyList_GET_ITEM(self->fut_callbacks, i);
if ((ret = PyObject_RichCompareBool(fn, item, Py_EQ)) < 0) {
goto fail;
}
if (ret == 0) {
Py_INCREF(item);
PyList_SET_ITEM(newlist, j, item);
j++;
}
}
if (PyList_SetSlice(newlist, j, len, NULL) < 0) {
goto fail;
}
if (PyList_SetSlice(self->fut_callbacks, 0, len, newlist) < 0) {
goto fail;
}
Py_DECREF(newlist);
return PyLong_FromSsize_t(len - j);
fail:
Py_DECREF(newlist);
return NULL;
}
/*[clinic input]
_asyncio.Future.cancel
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)
/*[clinic end generated code: output=e45b932ba8bd68a1 input=515709a127995109]*/
{
return future_cancel(self);
}
/*[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 (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 (self->fut_state == STATE_PENDING) {
Py_RETURN_FALSE;
}
else {
Py_RETURN_TRUE;
}
}
static PyObject *
FutureObj_get_blocking(FutureObj *fut)
{
if (fut->fut_blocking) {
Py_RETURN_TRUE;
}
else {
Py_RETURN_FALSE;
}
}
static int
FutureObj_set_blocking(FutureObj *fut, PyObject *val)
{
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)
{
if (fut->fut_log_tb) {
Py_RETURN_TRUE;
}
else {
Py_RETURN_FALSE;
}
}
static PyObject *
FutureObj_get_loop(FutureObj *fut)
{
if (fut->fut_loop == NULL) {
Py_RETURN_NONE;
}
Py_INCREF(fut->fut_loop);
return fut->fut_loop;
}
static PyObject *
FutureObj_get_callbacks(FutureObj *fut)
{
if (fut->fut_callbacks == NULL) {
Py_RETURN_NONE;
}
Py_INCREF(fut->fut_callbacks);
return fut->fut_callbacks;
}
static PyObject *
FutureObj_get_result(FutureObj *fut)
{
if (fut->fut_result == NULL) {
Py_RETURN_NONE;
}
Py_INCREF(fut->fut_result);
return fut->fut_result;
}
static PyObject *
FutureObj_get_exception(FutureObj *fut)
{
if (fut->fut_exception == NULL) {
Py_RETURN_NONE;
}
Py_INCREF(fut->fut_exception);
return fut->fut_exception;
}
static PyObject *
FutureObj_get_source_traceback(FutureObj *fut)
{
if (fut->fut_source_tb == NULL) {
Py_RETURN_NONE;
}
Py_INCREF(fut->fut_source_tb);
return fut->fut_source_tb;
}
static PyObject *
FutureObj_get_state(FutureObj *fut)
{
_Py_IDENTIFIER(PENDING);
_Py_IDENTIFIER(CANCELLED);
_Py_IDENTIFIER(FINISHED);
PyObject *ret = NULL;
switch (fut->fut_state) {
case STATE_PENDING:
ret = _PyUnicode_FromId(&PyId_PENDING);
break;
case STATE_CANCELLED:
ret = _PyUnicode_FromId(&PyId_CANCELLED);
break;
case STATE_FINISHED:
ret = _PyUnicode_FromId(&PyId_FINISHED);
break;
default:
assert (0);
}
Py_INCREF(ret);
return ret;
}
/*[clinic input]
_asyncio.Future._repr_info
[clinic start generated code]*/
static PyObject *
_asyncio_Future__repr_info_impl(FutureObj *self)
/*[clinic end generated code: output=fa69e901bd176cfb input=f21504d8e2ae1ca2]*/
{
return PyObject_CallFunctionObjArgs(
asyncio_future_repr_info_func, self, NULL);
}
/*[clinic input]
_asyncio.Future._schedule_callbacks
[clinic start generated code]*/
static PyObject *
_asyncio_Future__schedule_callbacks_impl(FutureObj *self)
/*[clinic end generated code: output=5e8958d89ea1c5dc input=4f5f295f263f4a88]*/
{
int ret = future_schedule_callbacks(self);
if (ret == -1) {
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *
FutureObj_repr(FutureObj *fut)
{
_Py_IDENTIFIER(_repr_info);
PyObject *_repr_info = _PyUnicode_FromId(&PyId__repr_info); // borrowed
if (_repr_info == NULL) {
return NULL;
}
PyObject *rinfo = PyObject_CallMethodObjArgs((PyObject*)fut, _repr_info,
NULL);
if (rinfo == NULL) {
return NULL;
}
PyObject *sp = PyUnicode_FromString(" ");
if (sp == NULL) {
Py_DECREF(rinfo);
return NULL;
}
PyObject *rinfo_s = PyUnicode_Join(sp, rinfo);
Py_DECREF(sp);
Py_DECREF(rinfo);
if (rinfo_s == NULL) {
return NULL;
}
PyObject *rstr = NULL;
PyObject *type_name = PyObject_GetAttrString((PyObject*)Py_TYPE(fut),
"__name__");
if (type_name != NULL) {
rstr = PyUnicode_FromFormat("<%S %S>", type_name, rinfo_s);
Py_DECREF(type_name);
}
Py_DECREF(rinfo_s);
return rstr;
}
static void
FutureObj_finalize(FutureObj *fut)
{
_Py_IDENTIFIER(call_exception_handler);
_Py_IDENTIFIER(message);
_Py_IDENTIFIER(exception);
_Py_IDENTIFIER(future);
_Py_IDENTIFIER(source_traceback);
if (!fut->fut_log_tb) {
return;
}
assert(fut->fut_exception != NULL);
fut->fut_log_tb = 0;;
PyObject *error_type, *error_value, *error_traceback;
/* Save the current exception, if any. */
PyErr_Fetch(&error_type, &error_value, &error_traceback);
PyObject *context = NULL;
PyObject *type_name = NULL;
PyObject *message = NULL;
PyObject *func = NULL;
PyObject *res = NULL;
context = PyDict_New();
if (context == NULL) {
goto finally;
}
type_name = PyObject_GetAttrString((PyObject*)Py_TYPE(fut), "__name__");
if (type_name == NULL) {
goto finally;
}
message = PyUnicode_FromFormat(
"%S exception was never retrieved", type_name);
if (message == NULL) {
goto finally;
}
if (_PyDict_SetItemId(context, &PyId_message, message) < 0 ||
_PyDict_SetItemId(context, &PyId_exception, fut->fut_exception) < 0 ||
_PyDict_SetItemId(context, &PyId_future, (PyObject*)fut) < 0) {
goto finally;
}
if (fut->fut_source_tb != NULL) {
if (_PyDict_SetItemId(context, &PyId_source_traceback,
fut->fut_source_tb) < 0) {
goto finally;
}
}
func = _PyObject_GetAttrId(fut->fut_loop, &PyId_call_exception_handler);
if (func != NULL) {
res = PyObject_CallFunctionObjArgs(func, context, NULL);
if (res == NULL) {
PyErr_WriteUnraisable(func);
}
}
finally:
Py_CLEAR(context);
Py_CLEAR(type_name);
Py_CLEAR(message);
Py_CLEAR(func);
Py_CLEAR(res);
/* Restore the saved exception. */
PyErr_Restore(error_type, error_value, error_traceback);
}
static PyAsyncMethods FutureType_as_async = {
(unaryfunc)future_new_iter, /* am_await */
0, /* am_aiter */
0 /* am_anext */
};
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__REPR_INFO_METHODDEF
_ASYNCIO_FUTURE__SCHEDULE_CALLBACKS_METHODDEF
{NULL, NULL} /* Sentinel */
};
#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, NULL, NULL}, \
{"_source_traceback", (getter)FutureObj_get_source_traceback, NULL, NULL},
static PyGetSetDef FutureType_getsetlist[] = {
FUTURE_COMMON_GETSETLIST
{NULL} /* Sentinel */
};
static void FutureObj_dealloc(PyObject *self);
static PyTypeObject FutureType = {
PyVarObject_HEAD_INIT(NULL, 0)
"_asyncio.Future",
sizeof(FutureObj), /* tp_basicsize */
.tp_dealloc = FutureObj_dealloc,
.tp_as_async = &FutureType_as_async,
.tp_repr = (reprfunc)FutureObj_repr,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_HAVE_FINALIZE,
.tp_doc = _asyncio_Future___init____doc__,
.tp_traverse = (traverseproc)FutureObj_traverse,
.tp_clear = (inquiry)FutureObj_clear,
.tp_weaklistoffset = offsetof(FutureObj, fut_weakreflist),
.tp_iter = (getiterfunc)future_new_iter,
.tp_methods = FutureType_methods,
.tp_getset = FutureType_getsetlist,
.tp_dictoffset = offsetof(FutureObj, dict),
.tp_init = (initproc)_asyncio_Future___init__,
.tp_new = PyType_GenericNew,
.tp_finalize = (destructor)FutureObj_finalize,
};
#define Future_CheckExact(obj) (Py_TYPE(obj) == &FutureType)
static inline int
future_call_schedule_callbacks(FutureObj *fut)
{
if (Future_CheckExact(fut)) {
return future_schedule_callbacks(fut);
}
else {
/* `fut` is a subclass of Future */
PyObject *ret = _PyObject_CallMethodId(
(PyObject*)fut, &PyId__schedule_callbacks, NULL);
if (ret == NULL) {
return -1;
}
Py_DECREF(ret);
return 0;
}
}
static void
FutureObj_dealloc(PyObject *self)
{
FutureObj *fut = (FutureObj *)self;
if (Future_CheckExact(fut)) {
/* When fut is subclass of Future, finalizer is called from
* subtype_dealloc.
*/
if (PyObject_CallFinalizerFromDealloc(self) < 0) {
// resurrected.
return;
}
}
if (fut->fut_weakreflist != NULL) {
PyObject_ClearWeakRefs(self);
}
(void)FutureObj_clear(fut);
Py_TYPE(fut)->tp_free(fut);
}
/*********************** Future Iterator **************************/
typedef struct {
PyObject_HEAD
FutureObj *future;
} futureiterobject;
static void
FutureIter_dealloc(futureiterobject *it)
{
PyObject_GC_UnTrack(it);
Py_XDECREF(it->future);
PyObject_GC_Del(it);
}
static PyObject *
FutureIter_iternext(futureiterobject *it)
{
PyObject *res;
FutureObj *fut = it->future;
if (fut == NULL) {
return NULL;
}
if (fut->fut_state == STATE_PENDING) {
if (!fut->fut_blocking) {
fut->fut_blocking = 1;
Py_INCREF(fut);
return (PyObject *)fut;
}
PyErr_Format(PyExc_AssertionError,
"yield from wasn't used with future");
return NULL;
}
res = _asyncio_Future_result_impl(fut);
if (res != NULL) {