Debian Codegraph @

Lib/test/test_asyncio/test_futures.py

raw
1 """Tests for futures.py."""
2
3 import concurrent.futures
4 import gc
5 import re
6 import sys
7 import threading
8 import traceback
9 import unittest
10 from unittest import mock
11 from types import GenericAlias
12 import asyncio
13 from asyncio import futures
14 import warnings
15 from test.test_asyncio import utils as test_utils
16 from test import support
17
18
19 def tearDownModule():
20 asyncio.events._set_event_loop_policy(None)
21
22
23 def _fakefunc(f):
24 return f
25
26
27 def first_cb():
28 pass
29
30
31 def last_cb():
32 pass
33
34
35 class ReachableCode(Exception):
36 """Exception to raise to indicate that some code was reached.
37
38 Use this exception if using mocks is not a good alternative.
39 """
40
41
42 class SimpleEvilEventLoop(asyncio.base_events.BaseEventLoop):
43 """Base class for UAF and other evil stuff requiring an evil event loop."""
44
45 def get_debug(self): # to suppress tracebacks
46 return False
47
48 def __del__(self):
49 # Automatically close the evil event loop to avoid warnings.
50 if not self.is_closed() and not self.is_running():
51 self.close()
52
53
54 class DuckFuture:
55 # Class that does not inherit from Future but aims to be duck-type
56 # compatible with it.
57
58 _asyncio_future_blocking = False
59 __cancelled = False
60 __result = None
61 __exception = None
62
63 def cancel(self):
64 if self.done():
65 return False
66 self.__cancelled = True
67 return True
68
69 def cancelled(self):
70 return self.__cancelled
71
72 def done(self):
73 return (self.__cancelled
74 or self.__result is not None
75 or self.__exception is not None)
76
77 def result(self):
78 self.assertFalse(self.cancelled())
79 if self.__exception is not None:
80 raise self.__exception
81 return self.__result
82
83 def exception(self):
84 self.assertFalse(self.cancelled())
85 return self.__exception
86
87 def set_result(self, result):
88 self.assertFalse(self.done())
89 self.assertIsNotNone(result)
90 self.__result = result
91
92 def set_exception(self, exception):
93 self.assertFalse(self.done())
94 self.assertIsNotNone(exception)
95 self.__exception = exception
96
97 def __iter__(self):
98 if not self.done():
99 self._asyncio_future_blocking = True
100 yield self
101 self.assertTrue(self.done())
102 return self.result()
103
104
105 class DuckTests(test_utils.TestCase):
106
107 def setUp(self):
108 super().setUp()
109 self.loop = self.new_test_loop()
110 self.addCleanup(self.loop.close)
111
112 def test_wrap_future(self):
113 f = DuckFuture()
114 g = asyncio.wrap_future(f)
115 self.assertIs(g, f)
116
117 def test_ensure_future(self):
118 f = DuckFuture()
119 g = asyncio.ensure_future(f)
120 self.assertIs(g, f)
121
122
123 class BaseFutureTests:
124
125 def _new_future(self, *args, **kwargs):
126 return self.cls(*args, **kwargs)
127
128 def setUp(self):
129 super().setUp()
130 self.loop = self.new_test_loop()
131 self.addCleanup(self.loop.close)
132
133 def test_generic_alias(self):
134 future = self.cls[str]
135 self.assertEqual(future.__args__, (str,))
136 self.assertIsInstance(future, GenericAlias)
137
138 def test_isfuture(self):
139 class MyFuture:
140 _asyncio_future_blocking = None
141
142 def __init__(self):
143 self._asyncio_future_blocking = False
144
145 self.assertFalse(asyncio.isfuture(MyFuture))
146 self.assertTrue(asyncio.isfuture(MyFuture()))
147 self.assertFalse(asyncio.isfuture(1))
148
149 # As `isinstance(Mock(), Future)` returns `False`
150 self.assertFalse(asyncio.isfuture(mock.Mock()))
151
152 f = self._new_future(loop=self.loop)
153 self.assertTrue(asyncio.isfuture(f))
154 self.assertFalse(asyncio.isfuture(type(f)))
155
156 # As `isinstance(Mock(Future), Future)` returns `True`
157 self.assertTrue(asyncio.isfuture(mock.Mock(type(f))))
158
159 f.cancel()
160
161 def test_initial_state(self):
162 f = self._new_future(loop=self.loop)
163 self.assertFalse(f.cancelled())
164 self.assertFalse(f.done())
165 f.cancel()
166 self.assertTrue(f.cancelled())
167
168 def test_constructor_without_loop(self):
169 with self.assertRaisesRegex(RuntimeError, 'no current event loop'):
170 self._new_future()
171
172 def test_constructor_use_running_loop(self):
173 async def test():
174 return self._new_future()
175 f = self.loop.run_until_complete(test())
176 self.assertIs(f._loop, self.loop)
177 self.assertIs(f.get_loop(), self.loop)
178
179 def test_constructor_use_global_loop(self):
180 # Deprecated in 3.10, undeprecated in 3.12
181 asyncio.set_event_loop(self.loop)
182 self.addCleanup(asyncio.set_event_loop, None)
183 f = self._new_future()
184 self.assertIs(f._loop, self.loop)
185 self.assertIs(f.get_loop(), self.loop)
186
187 def test_constructor_positional(self):
188 # Make sure Future doesn't accept a positional argument
189 self.assertRaises(TypeError, self._new_future, 42)
190
191 def test_uninitialized(self):
192 # Test that C Future doesn't crash when Future.__init__()
193 # call was skipped.
194
195 fut = self.cls.__new__(self.cls, loop=self.loop)
196 self.assertRaises(asyncio.InvalidStateError, fut.result)
197
198 fut = self.cls.__new__(self.cls, loop=self.loop)
199 self.assertRaises(asyncio.InvalidStateError, fut.exception)
200
201 fut = self.cls.__new__(self.cls, loop=self.loop)
202 with self.assertRaises((RuntimeError, AttributeError)):
203 fut.set_result(None)
204
205 fut = self.cls.__new__(self.cls, loop=self.loop)
206 with self.assertRaises((RuntimeError, AttributeError)):
207 fut.set_exception(Exception)
208
209 fut = self.cls.__new__(self.cls, loop=self.loop)
210 with self.assertRaises((RuntimeError, AttributeError)):
211 fut.cancel()
212
213 fut = self.cls.__new__(self.cls, loop=self.loop)
214 with self.assertRaises((RuntimeError, AttributeError)):
215 fut.add_done_callback(lambda f: None)
216
217 fut = self.cls.__new__(self.cls, loop=self.loop)
218 with self.assertRaises((RuntimeError, AttributeError)):
219 fut.remove_done_callback(lambda f: None)
220
221 fut = self.cls.__new__(self.cls, loop=self.loop)
222 try:
223 repr(fut)
224 except (RuntimeError, AttributeError):
225 pass
226
227 fut = self.cls.__new__(self.cls, loop=self.loop)
228 try:
229 fut.__await__()
230 except RuntimeError:
231 pass
232
233 fut = self.cls.__new__(self.cls, loop=self.loop)
234 try:
235 iter(fut)
236 except RuntimeError:
237 pass
238
239 fut = self.cls.__new__(self.cls, loop=self.loop)
240 self.assertFalse(fut.cancelled())
241 self.assertFalse(fut.done())
242
243 def test_future_cancel_message_getter(self):
244 f = self._new_future(loop=self.loop)
245 self.assertHasAttr(f, '_cancel_message')
246 self.assertEqual(f._cancel_message, None)
247
248 f.cancel('my message')
249 with self.assertRaises(asyncio.CancelledError):
250 self.loop.run_until_complete(f)
251 self.assertEqual(f._cancel_message, 'my message')
252
253 def test_future_cancel_message_setter(self):
254 f = self._new_future(loop=self.loop)
255 f.cancel('my message')
256 f._cancel_message = 'my new message'
257 self.assertEqual(f._cancel_message, 'my new message')
258
259 # Also check that the value is used for cancel().
260 with self.assertRaises(asyncio.CancelledError):
261 self.loop.run_until_complete(f)
262 self.assertEqual(f._cancel_message, 'my new message')
263
264 def test_cancel(self):
265 f = self._new_future(loop=self.loop)
266 self.assertTrue(f.cancel())
267 self.assertTrue(f.cancelled())
268 self.assertTrue(f.done())
269 self.assertRaises(asyncio.CancelledError, f.result)
270 self.assertRaises(asyncio.CancelledError, f.exception)
271 self.assertRaises(asyncio.InvalidStateError, f.set_result, None)
272 self.assertRaises(asyncio.InvalidStateError, f.set_exception, None)
273 self.assertFalse(f.cancel())
274
275 def test_result(self):
276 f = self._new_future(loop=self.loop)
277 self.assertRaises(asyncio.InvalidStateError, f.result)
278
279 f.set_result(42)
280 self.assertFalse(f.cancelled())
281 self.assertTrue(f.done())
282 self.assertEqual(f.result(), 42)
283 self.assertEqual(f.exception(), None)
284 self.assertRaises(asyncio.InvalidStateError, f.set_result, None)
285 self.assertRaises(asyncio.InvalidStateError, f.set_exception, None)
286 self.assertFalse(f.cancel())
287
288 def test_exception(self):
289 exc = RuntimeError()
290 f = self._new_future(loop=self.loop)
291 self.assertRaises(asyncio.InvalidStateError, f.exception)
292
293 f.set_exception(exc)
294 self.assertFalse(f.cancelled())
295 self.assertTrue(f.done())
296 self.assertRaises(RuntimeError, f.result)
297 self.assertEqual(f.exception(), exc)
298 self.assertRaises(asyncio.InvalidStateError, f.set_result, None)
299 self.assertRaises(asyncio.InvalidStateError, f.set_exception, None)
300 self.assertFalse(f.cancel())
301
302 def test_stop_iteration_exception(self, stop_iteration_class=StopIteration):
303 exc = stop_iteration_class()
304 f = self._new_future(loop=self.loop)
305 f.set_exception(exc)
306 self.assertFalse(f.cancelled())
307 self.assertTrue(f.done())
308 self.assertRaises(RuntimeError, f.result)
309 exc = f.exception()
310 cause = exc.__cause__
311 self.assertIsInstance(exc, RuntimeError)
312 self.assertRegex(str(exc), 'StopIteration .* cannot be raised')
313 self.assertIsInstance(cause, stop_iteration_class)
314
315 def test_stop_iteration_subclass_exception(self):
316 class MyStopIteration(StopIteration):
317 pass
318
319 self.test_stop_iteration_exception(MyStopIteration)
320
321 def test_exception_class(self):
322 f = self._new_future(loop=self.loop)
323 f.set_exception(RuntimeError)
324 self.assertIsInstance(f.exception(), RuntimeError)
325
326 def test_yield_from_twice(self):
327 f = self._new_future(loop=self.loop)
328
329 def fixture():
330 yield 'A'
331 x = yield from f
332 yield 'B', x
333 y = yield from f
334 yield 'C', y
335
336 g = fixture()
337 self.assertEqual(next(g), 'A') # yield 'A'.
338 self.assertEqual(next(g), f) # First yield from f.
339 f.set_result(42)
340 self.assertEqual(next(g), ('B', 42)) # yield 'B', x.
341 # The second "yield from f" does not yield f.
342 self.assertEqual(next(g), ('C', 42)) # yield 'C', y.
343
344 def test_future_repr(self):
345 self.loop.set_debug(True)
346 f_pending_debug = self._new_future(loop=self.loop)
347 frame = f_pending_debug._source_traceback[-1]
348 self.assertEqual(
349 repr(f_pending_debug),
350 f'<{self.cls.__name__} pending created at {frame[0]}:{frame[1]}>')
351 f_pending_debug.cancel()
352
353 self.loop.set_debug(False)
354 f_pending = self._new_future(loop=self.loop)
355 self.assertEqual(repr(f_pending), f'<{self.cls.__name__} pending>')
356 f_pending.cancel()
357
358 f_cancelled = self._new_future(loop=self.loop)
359 f_cancelled.cancel()
360 self.assertEqual(repr(f_cancelled), f'<{self.cls.__name__} cancelled>')
361
362 f_result = self._new_future(loop=self.loop)
363 f_result.set_result(4)
364 self.assertEqual(
365 repr(f_result), f'<{self.cls.__name__} finished result=4>')
366 self.assertEqual(f_result.result(), 4)
367
368 exc = RuntimeError()
369 f_exception = self._new_future(loop=self.loop)
370 f_exception.set_exception(exc)
371 self.assertEqual(
372 repr(f_exception),
373 f'<{self.cls.__name__} finished exception=RuntimeError()>')
374 self.assertIs(f_exception.exception(), exc)
375
376 def func_repr(func):
377 filename, lineno = test_utils.get_function_source(func)
378 text = '%s() at %s:%s' % (func.__qualname__, filename, lineno)
379 return re.escape(text)
380
381 f_one_callbacks = self._new_future(loop=self.loop)
382 f_one_callbacks.add_done_callback(_fakefunc)
383 fake_repr = func_repr(_fakefunc)
384 self.assertRegex(
385 repr(f_one_callbacks),
386 r'<' + self.cls.__name__ + r' pending cb=\[%s\]>' % fake_repr)
387 f_one_callbacks.cancel()
388 self.assertEqual(repr(f_one_callbacks),
389 f'<{self.cls.__name__} cancelled>')
390
391 f_two_callbacks = self._new_future(loop=self.loop)
392 f_two_callbacks.add_done_callback(first_cb)
393 f_two_callbacks.add_done_callback(last_cb)
394 first_repr = func_repr(first_cb)
395 last_repr = func_repr(last_cb)
396 self.assertRegex(repr(f_two_callbacks),
397 r'<' + self.cls.__name__ + r' pending cb=\[%s, %s\]>'
398 % (first_repr, last_repr))
399
400 f_many_callbacks = self._new_future(loop=self.loop)
401 f_many_callbacks.add_done_callback(first_cb)
402 for i in range(8):
403 f_many_callbacks.add_done_callback(_fakefunc)
404 f_many_callbacks.add_done_callback(last_cb)
405 cb_regex = r'%s, <8 more>, %s' % (first_repr, last_repr)
406 self.assertRegex(
407 repr(f_many_callbacks),
408 r'<' + self.cls.__name__ + r' pending cb=\[%s\]>' % cb_regex)
409 f_many_callbacks.cancel()
410 self.assertEqual(repr(f_many_callbacks),
411 f'<{self.cls.__name__} cancelled>')
412
413 def test_copy_state(self):
414 from asyncio.futures import _copy_future_state
415
416 f = self._new_future(loop=self.loop)
417 f.set_result(10)
418
419 newf = self._new_future(loop=self.loop)
420 _copy_future_state(f, newf)
421 self.assertTrue(newf.done())
422 self.assertEqual(newf.result(), 10)
423
424 f_exception = self._new_future(loop=self.loop)
425 f_exception.set_exception(RuntimeError())
426
427 newf_exception = self._new_future(loop=self.loop)
428 _copy_future_state(f_exception, newf_exception)
429 self.assertTrue(newf_exception.done())
430 self.assertRaises(RuntimeError, newf_exception.result)
431
432 f_cancelled = self._new_future(loop=self.loop)
433 f_cancelled.cancel()
434
435 newf_cancelled = self._new_future(loop=self.loop)
436 _copy_future_state(f_cancelled, newf_cancelled)
437 self.assertTrue(newf_cancelled.cancelled())
438
439 try:
440 raise concurrent.futures.InvalidStateError
441 except BaseException as e:
442 f_exc = e
443
444 f_conexc = self._new_future(loop=self.loop)
445 f_conexc.set_exception(f_exc)
446
447 newf_conexc = self._new_future(loop=self.loop)
448 _copy_future_state(f_conexc, newf_conexc)
449 self.assertTrue(newf_conexc.done())
450 try:
451 newf_conexc.result()
452 except BaseException as e:
453 newf_exc = e # assertRaises context manager drops the traceback
454 newf_tb = ''.join(traceback.format_tb(newf_exc.__traceback__))
455 self.assertEqual(newf_tb.count('raise concurrent.futures.InvalidStateError'), 1)
456
457 def test_iter(self):
458 fut = self._new_future(loop=self.loop)
459
460 def coro():
461 yield from fut
462
463 def test():
464 arg1, arg2 = coro()
465
466 with self.assertRaisesRegex(RuntimeError, "await wasn't used"):
467 test()
468 fut.cancel()
469
470 def test_log_traceback(self):
471 fut = self._new_future(loop=self.loop)
472 with self.assertRaisesRegex(ValueError, 'can only be set to False'):
473 fut._log_traceback = True
474
475 @mock.patch('asyncio.base_events.logger')
476 def test_tb_logger_abandoned(self, m_log):
477 fut = self._new_future(loop=self.loop)
478 del fut
479 self.assertFalse(m_log.error.called)
480
481 @mock.patch('asyncio.base_events.logger')
482 def test_tb_logger_not_called_after_cancel(self, m_log):
483 fut = self._new_future(loop=self.loop)
484 fut.set_exception(Exception())
485 fut.cancel()
486 del fut
487 self.assertFalse(m_log.error.called)
488
489 @mock.patch('asyncio.base_events.logger')
490 def test_tb_logger_result_unretrieved(self, m_log):
491 fut = self._new_future(loop=self.loop)
492 fut.set_result(42)
493 del fut
494 self.assertFalse(m_log.error.called)
495
496 @mock.patch('asyncio.base_events.logger')
497 def test_tb_logger_result_retrieved(self, m_log):
498 fut = self._new_future(loop=self.loop)
499 fut.set_result(42)
500 fut.result()
501 del fut
502 self.assertFalse(m_log.error.called)
503
504 @mock.patch('asyncio.base_events.logger')
505 def test_tb_logger_exception_unretrieved(self, m_log):
506 fut = self._new_future(loop=self.loop)
507 fut.set_exception(RuntimeError('boom'))
508 del fut
509 test_utils.run_briefly(self.loop)
510 support.gc_collect()
511 self.assertTrue(m_log.error.called)
512
513 @mock.patch('asyncio.base_events.logger')
514 def test_tb_logger_exception_retrieved(self, m_log):
515 fut = self._new_future(loop=self.loop)
516 fut.set_exception(RuntimeError('boom'))
517 fut.exception()
518 del fut
519 self.assertFalse(m_log.error.called)
520
521 @mock.patch('asyncio.base_events.logger')
522 def test_tb_logger_exception_result_retrieved(self, m_log):
523 fut = self._new_future(loop=self.loop)
524 fut.set_exception(RuntimeError('boom'))
525 self.assertRaises(RuntimeError, fut.result)
526 del fut
527 self.assertFalse(m_log.error.called)
528
529 def test_wrap_future(self):
530
531 def run(arg):
532 return (arg, threading.get_ident())
533 ex = concurrent.futures.ThreadPoolExecutor(1)
534 f1 = ex.submit(run, 'oi')
535 f2 = asyncio.wrap_future(f1, loop=self.loop)
536 res, ident = self.loop.run_until_complete(f2)
537 self.assertTrue(asyncio.isfuture(f2))
538 self.assertEqual(res, 'oi')
539 self.assertNotEqual(ident, threading.get_ident())
540 ex.shutdown(wait=True)
541
542 def test_wrap_future_future(self):
543 f1 = self._new_future(loop=self.loop)
544 f2 = asyncio.wrap_future(f1)
545 self.assertIs(f1, f2)
546
547 def test_wrap_future_without_loop(self):
548 def run(arg):
549 return (arg, threading.get_ident())
550 ex = concurrent.futures.ThreadPoolExecutor(1)
551 f1 = ex.submit(run, 'oi')
552 with self.assertRaisesRegex(RuntimeError, 'no current event loop'):
553 asyncio.wrap_future(f1)
554 ex.shutdown(wait=True)
555
556 def test_wrap_future_use_running_loop(self):
557 def run(arg):
558 return (arg, threading.get_ident())
559 ex = concurrent.futures.ThreadPoolExecutor(1)
560 f1 = ex.submit(run, 'oi')
561 async def test():
562 return asyncio.wrap_future(f1)
563 f2 = self.loop.run_until_complete(test())
564 self.assertIs(self.loop, f2._loop)
565 ex.shutdown(wait=True)
566
567 def test_wrap_future_use_global_loop(self):
568 # Deprecated in 3.10, undeprecated in 3.12
569 asyncio.set_event_loop(self.loop)
570 self.addCleanup(asyncio.set_event_loop, None)
571 def run(arg):
572 return (arg, threading.get_ident())
573 ex = concurrent.futures.ThreadPoolExecutor(1)
574 f1 = ex.submit(run, 'oi')
575 f2 = asyncio.wrap_future(f1)
576 self.assertIs(self.loop, f2._loop)
577 ex.shutdown(wait=True)
578
579 def test_wrap_future_cancel(self):
580 f1 = concurrent.futures.Future()
581 f2 = asyncio.wrap_future(f1, loop=self.loop)
582 f2.cancel()
583 test_utils.run_briefly(self.loop)
584 self.assertTrue(f1.cancelled())
585 self.assertTrue(f2.cancelled())
586
587 def test_wrap_future_cancel2(self):
588 f1 = concurrent.futures.Future()
589 f2 = asyncio.wrap_future(f1, loop=self.loop)
590 f1.set_result(42)
591 f2.cancel()
592 test_utils.run_briefly(self.loop)
593 self.assertFalse(f1.cancelled())
594 self.assertEqual(f1.result(), 42)
595 self.assertTrue(f2.cancelled())
596
597 def test_future_source_traceback(self):
598 self.loop.set_debug(True)
599
600 future = self._new_future(loop=self.loop)
601 lineno = sys._getframe().f_lineno - 1
602 self.assertIsInstance(future._source_traceback, list)
603 self.assertEqual(future._source_traceback[-2][:3],
604 (__file__,
605 lineno,
606 'test_future_source_traceback'))
607
608 @mock.patch('asyncio.base_events.logger')
609 def check_future_exception_never_retrieved(self, debug, m_log):
610 self.loop.set_debug(debug)
611
612 def memory_error():
613 try:
614 raise MemoryError()
615 except BaseException as exc:
616 return exc
617 exc = memory_error()
618
619 future = self._new_future(loop=self.loop)
620 future.set_exception(exc)
621 future = None
622 test_utils.run_briefly(self.loop)
623 support.gc_collect()
624
625 regex = f'^{self.cls.__name__} exception was never retrieved\n'
626 exc_info = (type(exc), exc, exc.__traceback__)
627 m_log.error.assert_called_once_with(mock.ANY, exc_info=exc_info)
628
629 message = m_log.error.call_args[0][0]
630 self.assertRegex(message, re.compile(regex, re.DOTALL))
631
632 def test_future_exception_never_retrieved(self):
633 self.check_future_exception_never_retrieved(False)
634
635 def test_future_exception_never_retrieved_debug(self):
636 self.check_future_exception_never_retrieved(True)
637
638 def test_set_result_unless_cancelled(self):
639 fut = self._new_future(loop=self.loop)
640 fut.cancel()
641 futures._set_result_unless_cancelled(fut, 2)
642 self.assertTrue(fut.cancelled())
643
644 def test_future_stop_iteration_args(self):
645 fut = self._new_future(loop=self.loop)
646 fut.set_result((1, 2))
647 fi = fut.__iter__()
648 result = None
649 try:
650 fi.send(None)
651 except StopIteration as ex:
652 result = ex.args[0]
653 else:
654 self.fail('StopIteration was expected')
655 self.assertEqual(result, (1, 2))
656
657 def test_future_iter_throw(self):
658 fut = self._new_future(loop=self.loop)
659 fi = iter(fut)
660 with self.assertWarns(DeprecationWarning):
661 self.assertRaises(Exception, fi.throw, Exception, Exception("zebra"), None)
662 with warnings.catch_warnings():
663 warnings.filterwarnings("ignore", category=DeprecationWarning)
664 self.assertRaises(TypeError, fi.throw,
665 Exception, Exception("elephant"), 32)
666 self.assertRaises(TypeError, fi.throw,
667 Exception("elephant"), Exception("elephant"))
668 # https://github.com/python/cpython/issues/101326
669 self.assertRaises(ValueError, fi.throw, ValueError, None, None)
670 self.assertRaises(TypeError, fi.throw, list)
671
672 def test_future_del_collect(self):
673 class Evil:
674 def __del__(self):
675 gc.collect()
676
677 for i in range(100):
678 fut = self._new_future(loop=self.loop)
679 fut.set_result(Evil())
680
681 def test_future_cancelled_result_refcycles(self):
682 f = self._new_future(loop=self.loop)
683 f.cancel()
684 exc = None
685 try:
686 f.result()
687 except asyncio.CancelledError as e:
688 exc = e
689 self.assertIsNotNone(exc)
690 self.assertListEqual(gc.get_referrers(exc), [])
691
692 def test_future_cancelled_exception_refcycles(self):
693 f = self._new_future(loop=self.loop)
694 f.cancel()
695 exc = None
696 try:
697 f.exception()
698 except asyncio.CancelledError as e:
699 exc = e
700 self.assertIsNotNone(exc)
701 self.assertListEqual(gc.get_referrers(exc), [])
702
703
704 @unittest.skipUnless(hasattr(futures, '_CFuture'),
705 'requires the C _asyncio module')
706 class CFutureTests(BaseFutureTests, test_utils.TestCase):
707 try:
708 cls = futures._CFuture
709 except AttributeError:
710 cls = None
711
712 def test_future_del_segfault(self):
713 fut = self._new_future(loop=self.loop)
714 with self.assertRaises(AttributeError):
715 del fut._asyncio_future_blocking
716 with self.assertRaises(AttributeError):
717 del fut._log_traceback
718
719 def test_callbacks_copy(self):
720 # See https://github.com/python/cpython/issues/125789
721 # In C implementation, the `_callbacks` attribute
722 # always returns a new list to avoid mutations of internal state
723
724 fut = self._new_future(loop=self.loop)
725 f1 = lambda _: 1
726 f2 = lambda _: 2
727 fut.add_done_callback(f1)
728 fut.add_done_callback(f2)
729 callbacks = fut._callbacks
730 self.assertIsNot(callbacks, fut._callbacks)
731 fut.remove_done_callback(f1)
732 callbacks = fut._callbacks
733 self.assertIsNot(callbacks, fut._callbacks)
734 fut.remove_done_callback(f2)
735 self.assertIsNone(fut._callbacks)
736
737
738 @unittest.skipUnless(hasattr(futures, '_CFuture'),
739 'requires the C _asyncio module')
740 class CSubFutureTests(BaseFutureTests, test_utils.TestCase):
741 try:
742 class CSubFuture(futures._CFuture):
743 pass
744
745 cls = CSubFuture
746 except AttributeError:
747 cls = None
748
749
750 class PyFutureTests(BaseFutureTests, test_utils.TestCase):
751 cls = futures._PyFuture
752
753
754 class BaseFutureDoneCallbackTests():
755
756 def setUp(self):
757 super().setUp()
758 self.loop = self.new_test_loop()
759
760 def run_briefly(self):
761 test_utils.run_briefly(self.loop)
762
763 def _make_callback(self, bag, thing):
764 # Create a callback function that appends thing to bag.
765 def bag_appender(future):
766 bag.append(thing)
767 return bag_appender
768
769 def _new_future(self):
770 raise NotImplementedError
771
772 def test_callbacks_remove_first_callback(self):
773 bag = []
774 f = self._new_future()
775
776 cb1 = self._make_callback(bag, 42)
777 cb2 = self._make_callback(bag, 17)
778 cb3 = self._make_callback(bag, 100)
779
780 f.add_done_callback(cb1)
781 f.add_done_callback(cb2)
782 f.add_done_callback(cb3)
783
784 f.remove_done_callback(cb1)
785 f.remove_done_callback(cb1)
786
787 self.assertEqual(bag, [])
788 f.set_result('foo')
789
790 self.run_briefly()
791
792 self.assertEqual(bag, [17, 100])
793 self.assertEqual(f.result(), 'foo')
794
795 def test_callbacks_remove_first_and_second_callback(self):
796 bag = []
797 f = self._new_future()
798
799 cb1 = self._make_callback(bag, 42)
800 cb2 = self._make_callback(bag, 17)
801 cb3 = self._make_callback(bag, 100)
802
803 f.add_done_callback(cb1)
804 f.add_done_callback(cb2)
805 f.add_done_callback(cb3)
806
807 f.remove_done_callback(cb1)
808 f.remove_done_callback(cb2)
809 f.remove_done_callback(cb1)
810
811 self.assertEqual(bag, [])
812 f.set_result('foo')
813
814 self.run_briefly()
815
816 self.assertEqual(bag, [100])
817 self.assertEqual(f.result(), 'foo')
818
819 def test_callbacks_remove_third_callback(self):
820 bag = []
821 f = self._new_future()
822
823 cb1 = self._make_callback(bag, 42)
824 cb2 = self._make_callback(bag, 17)
825 cb3 = self._make_callback(bag, 100)
826
827 f.add_done_callback(cb1)
828 f.add_done_callback(cb2)
829 f.add_done_callback(cb3)
830
831 f.remove_done_callback(cb3)
832 f.remove_done_callback(cb3)
833
834 self.assertEqual(bag, [])
835 f.set_result('foo')
836
837 self.run_briefly()
838
839 self.assertEqual(bag, [42, 17])
840 self.assertEqual(f.result(), 'foo')
841
842 def test_callbacks_invoked_on_set_result(self):
843 bag = []
844 f = self._new_future()
845 f.add_done_callback(self._make_callback(bag, 42))
846 f.add_done_callback(self._make_callback(bag, 17))
847
848 self.assertEqual(bag, [])
849 f.set_result('foo')
850
851 self.run_briefly()
852
853 self.assertEqual(bag, [42, 17])
854 self.assertEqual(f.result(), 'foo')
855
856 def test_callbacks_invoked_on_set_exception(self):
857 bag = []
858 f = self._new_future()
859 f.add_done_callback(self._make_callback(bag, 100))
860
861 self.assertEqual(bag, [])
862 exc = RuntimeError()
863 f.set_exception(exc)
864
865 self.run_briefly()
866
867 self.assertEqual(bag, [100])
868 self.assertEqual(f.exception(), exc)
869
870 def test_remove_done_callback(self):
871 bag = []
872 f = self._new_future()
873 cb1 = self._make_callback(bag, 1)
874 cb2 = self._make_callback(bag, 2)
875 cb3 = self._make_callback(bag, 3)
876
877 # Add one cb1 and one cb2.
878 f.add_done_callback(cb1)
879 f.add_done_callback(cb2)
880
881 # One instance of cb2 removed. Now there's only one cb1.
882 self.assertEqual(f.remove_done_callback(cb2), 1)
883
884 # Never had any cb3 in there.
885 self.assertEqual(f.remove_done_callback(cb3), 0)
886
887 # After this there will be 6 instances of cb1 and one of cb2.
888 f.add_done_callback(cb2)
889 for i in range(5):
890 f.add_done_callback(cb1)
891
892 # Remove all instances of cb1. One cb2 remains.
893 self.assertEqual(f.remove_done_callback(cb1), 6)
894
895 self.assertEqual(bag, [])
896 f.set_result('foo')
897
898 self.run_briefly()
899
900 self.assertEqual(bag, [2])
901 self.assertEqual(f.result(), 'foo')
902
903 def test_remove_done_callbacks_list_mutation(self):
904 # see http://bugs.python.org/issue28963 for details
905
906 fut = self._new_future()
907 fut.add_done_callback(str)
908
909 for _ in range(63):
910 fut.add_done_callback(id)
911
912 class evil:
913 def __eq__(self, other):
914 fut.remove_done_callback(id)
915 return False
916
917 fut.remove_done_callback(evil())
918
919 def test_remove_done_callbacks_list_clear(self):
920 # see https://github.com/python/cpython/issues/97592 for details
921
922 fut = self._new_future()
923 fut.add_done_callback(str)
924
925 for _ in range(63):
926 fut.add_done_callback(id)
927
928 class evil:
929 def __eq__(self, other):
930 fut.remove_done_callback(other)
931
932 fut.remove_done_callback(evil())
933
934 def test_schedule_callbacks_list_mutation_1(self):
935 # see http://bugs.python.org/issue28963 for details
936
937 def mut(f):
938 f.remove_done_callback(str)
939
940 fut = self._new_future()
941 fut.add_done_callback(mut)
942 fut.add_done_callback(str)
943 fut.add_done_callback(str)
944 fut.set_result(1)
945 test_utils.run_briefly(self.loop)
946
947 def test_schedule_callbacks_list_mutation_2(self):
948 # see http://bugs.python.org/issue30828 for details
949
950 fut = self._new_future()
951 fut.add_done_callback(str)
952
953 for _ in range(63):
954 fut.add_done_callback(id)
955
956 max_extra_cbs = 100
957 extra_cbs = 0
958
959 class evil:
960 def __eq__(self, other):
961 nonlocal extra_cbs
962 extra_cbs += 1
963 if extra_cbs < max_extra_cbs:
964 fut.add_done_callback(id)
965 return False
966
967 fut.remove_done_callback(evil())
968
969 def test_evil_call_soon_list_mutation(self):
970 # see: https://github.com/python/cpython/issues/125969
971 called_on_fut_callback0 = False
972
973 pad = lambda: ...
974
975 def evil_call_soon(*args, **kwargs):
976 nonlocal called_on_fut_callback0
977 if called_on_fut_callback0:
978 # Called when handling fut->fut_callbacks[0]
979 # and mutates the length fut->fut_callbacks.
980 fut.remove_done_callback(int)
981 fut.remove_done_callback(pad)
982 else:
983 called_on_fut_callback0 = True
984
985 fake_event_loop = SimpleEvilEventLoop()
986 fake_event_loop.call_soon = evil_call_soon
987
988 with mock.patch.object(self, 'loop', fake_event_loop):
989 fut = self._new_future()
990 self.assertIs(fut.get_loop(), fake_event_loop)
991
992 fut.add_done_callback(str) # sets fut->fut_callback0
993 fut.add_done_callback(int) # sets fut->fut_callbacks[0]
994 fut.add_done_callback(pad) # sets fut->fut_callbacks[1]
995 fut.add_done_callback(pad) # sets fut->fut_callbacks[2]
996 fut.set_result("boom")
997
998 # When there are no more callbacks, the Python implementation
999 # returns an empty list but the C implementation returns None.
1000 self.assertIn(fut._callbacks, (None, []))
1001
1002 def test_use_after_free_on_fut_callback_0_with_evil__eq__(self):
1003 # Special thanks to Nico-Posada for the original PoC.
1004 # See https://github.com/python/cpython/issues/125966.
1005
1006 fut = self._new_future()
1007
1008 class cb_pad:
1009 def __eq__(self, other):
1010 return True
1011
1012 class evil(cb_pad):
1013 def __eq__(self, other):
1014 fut.remove_done_callback(None)
1015 return NotImplemented
1016
1017 fut.add_done_callback(cb_pad())
1018 fut.remove_done_callback(evil())
1019
1020 def test_use_after_free_on_fut_callback_0_with_evil__getattribute__(self):
1021 # see: https://github.com/python/cpython/issues/125984
1022
1023 class EvilEventLoop(SimpleEvilEventLoop):
1024 def call_soon(self, *args, **kwargs):
1025 super().call_soon(*args, **kwargs)
1026 raise ReachableCode
1027
1028 def __getattribute__(self, name):
1029 nonlocal fut_callback_0
1030 if name == 'call_soon':
1031 fut.remove_done_callback(fut_callback_0)
1032 del fut_callback_0
1033 return object.__getattribute__(self, name)
1034
1035 evil_loop = EvilEventLoop()
1036 with mock.patch.object(self, 'loop', evil_loop):
1037 fut = self._new_future()
1038 self.assertIs(fut.get_loop(), evil_loop)
1039
1040 fut_callback_0 = lambda: ...
1041 fut.add_done_callback(fut_callback_0)
1042 self.assertRaises(ReachableCode, fut.set_result, "boom")
1043
1044 def test_use_after_free_on_fut_context_0_with_evil__getattribute__(self):
1045 # see: https://github.com/python/cpython/issues/125984
1046
1047 class EvilEventLoop(SimpleEvilEventLoop):
1048 def call_soon(self, *args, **kwargs):
1049 super().call_soon(*args, **kwargs)
1050 raise ReachableCode
1051
1052 def __getattribute__(self, name):
1053 if name == 'call_soon':
1054 # resets the future's event loop
1055 fut.__init__(loop=SimpleEvilEventLoop())
1056 return object.__getattribute__(self, name)
1057
1058 evil_loop = EvilEventLoop()
1059 with mock.patch.object(self, 'loop', evil_loop):
1060 fut = self._new_future()
1061 self.assertIs(fut.get_loop(), evil_loop)
1062
1063 fut_callback_0 = mock.Mock()
1064 fut_context_0 = mock.Mock()
1065 fut.add_done_callback(fut_callback_0, context=fut_context_0)
1066 del fut_context_0
1067 del fut_callback_0
1068 self.assertRaises(ReachableCode, fut.set_result, "boom")
1069
1070
1071 @unittest.skipUnless(hasattr(futures, '_CFuture'),
1072 'requires the C _asyncio module')
1073 class CFutureDoneCallbackTests(BaseFutureDoneCallbackTests,
1074 test_utils.TestCase):
1075
1076 def _new_future(self):
1077 return futures._CFuture(loop=self.loop)
1078
1079
1080 @unittest.skipUnless(hasattr(futures, '_CFuture'),
1081 'requires the C _asyncio module')
1082 class CSubFutureDoneCallbackTests(BaseFutureDoneCallbackTests,
1083 test_utils.TestCase):
1084
1085 def _new_future(self):
1086 class CSubFuture(futures._CFuture):
1087 pass
1088 return CSubFuture(loop=self.loop)
1089
1090
1091 class PyFutureDoneCallbackTests(BaseFutureDoneCallbackTests,
1092 test_utils.TestCase):
1093
1094 def _new_future(self):
1095 return futures._PyFuture(loop=self.loop)
1096
1097
1098 class BaseFutureInheritanceTests:
1099
1100 def _get_future_cls(self):
1101 raise NotImplementedError
1102
1103 def setUp(self):
1104 super().setUp()
1105 self.loop = self.new_test_loop()
1106 self.addCleanup(self.loop.close)
1107
1108 def test_inherit_without_calling_super_init(self):
1109 # See https://bugs.python.org/issue38785 for the context
1110 cls = self._get_future_cls()
1111
1112 class MyFut(cls):
1113 def __init__(self, *args, **kwargs):
1114 # don't call super().__init__()
1115 pass
1116
1117 fut = MyFut(loop=self.loop)
1118 with self.assertRaisesRegex(
1119 RuntimeError,
1120 "Future object is not initialized."
1121 ):
1122 fut.get_loop()
1123
1124
1125 class PyFutureInheritanceTests(BaseFutureInheritanceTests,
1126 test_utils.TestCase):
1127 def _get_future_cls(self):
1128 return futures._PyFuture
1129
1130
1131 @unittest.skipUnless(hasattr(futures, '_CFuture'),
1132 'requires the C _asyncio module')
1133 class CFutureInheritanceTests(BaseFutureInheritanceTests,
1134 test_utils.TestCase):
1135 def _get_future_cls(self):
1136 return futures._CFuture
1137
1138
1139 if __name__ == '__main__':
1140 unittest.main()