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.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.assertTrue(hasattr(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_future_iter_get_referents_segfault(self):
720 # See https://github.com/python/cpython/issues/122695
721 import _asyncio
722 it = iter(self._new_future(loop=self.loop))
723 del it
724 evil = gc.get_referents(_asyncio)
725 gc.collect()
726
727 def test_callbacks_copy(self):
728 # See https://github.com/python/cpython/issues/125789
729 # In C implementation, the `_callbacks` attribute
730 # always returns a new list to avoid mutations of internal state
731
732 fut = self._new_future(loop=self.loop)
733 f1 = lambda _: 1
734 f2 = lambda _: 2
735 fut.add_done_callback(f1)
736 fut.add_done_callback(f2)
737 callbacks = fut._callbacks
738 self.assertIsNot(callbacks, fut._callbacks)
739 fut.remove_done_callback(f1)
740 callbacks = fut._callbacks
741 self.assertIsNot(callbacks, fut._callbacks)
742 fut.remove_done_callback(f2)
743 self.assertIsNone(fut._callbacks)
744
745
746 @unittest.skipUnless(hasattr(futures, '_CFuture'),
747 'requires the C _asyncio module')
748 class CSubFutureTests(BaseFutureTests, test_utils.TestCase):
749 try:
750 class CSubFuture(futures._CFuture):
751 pass
752
753 cls = CSubFuture
754 except AttributeError:
755 cls = None
756
757
758 class PyFutureTests(BaseFutureTests, test_utils.TestCase):
759 cls = futures._PyFuture
760
761
762 class BaseFutureDoneCallbackTests():
763
764 def setUp(self):
765 super().setUp()
766 self.loop = self.new_test_loop()
767
768 def run_briefly(self):
769 test_utils.run_briefly(self.loop)
770
771 def _make_callback(self, bag, thing):
772 # Create a callback function that appends thing to bag.
773 def bag_appender(future):
774 bag.append(thing)
775 return bag_appender
776
777 def _new_future(self):
778 raise NotImplementedError
779
780 def test_callbacks_remove_first_callback(self):
781 bag = []
782 f = self._new_future()
783
784 cb1 = self._make_callback(bag, 42)
785 cb2 = self._make_callback(bag, 17)
786 cb3 = self._make_callback(bag, 100)
787
788 f.add_done_callback(cb1)
789 f.add_done_callback(cb2)
790 f.add_done_callback(cb3)
791
792 f.remove_done_callback(cb1)
793 f.remove_done_callback(cb1)
794
795 self.assertEqual(bag, [])
796 f.set_result('foo')
797
798 self.run_briefly()
799
800 self.assertEqual(bag, [17, 100])
801 self.assertEqual(f.result(), 'foo')
802
803 def test_callbacks_remove_first_and_second_callback(self):
804 bag = []
805 f = self._new_future()
806
807 cb1 = self._make_callback(bag, 42)
808 cb2 = self._make_callback(bag, 17)
809 cb3 = self._make_callback(bag, 100)
810
811 f.add_done_callback(cb1)
812 f.add_done_callback(cb2)
813 f.add_done_callback(cb3)
814
815 f.remove_done_callback(cb1)
816 f.remove_done_callback(cb2)
817 f.remove_done_callback(cb1)
818
819 self.assertEqual(bag, [])
820 f.set_result('foo')
821
822 self.run_briefly()
823
824 self.assertEqual(bag, [100])
825 self.assertEqual(f.result(), 'foo')
826
827 def test_callbacks_remove_third_callback(self):
828 bag = []
829 f = self._new_future()
830
831 cb1 = self._make_callback(bag, 42)
832 cb2 = self._make_callback(bag, 17)
833 cb3 = self._make_callback(bag, 100)
834
835 f.add_done_callback(cb1)
836 f.add_done_callback(cb2)
837 f.add_done_callback(cb3)
838
839 f.remove_done_callback(cb3)
840 f.remove_done_callback(cb3)
841
842 self.assertEqual(bag, [])
843 f.set_result('foo')
844
845 self.run_briefly()
846
847 self.assertEqual(bag, [42, 17])
848 self.assertEqual(f.result(), 'foo')
849
850 def test_callbacks_invoked_on_set_result(self):
851 bag = []
852 f = self._new_future()
853 f.add_done_callback(self._make_callback(bag, 42))
854 f.add_done_callback(self._make_callback(bag, 17))
855
856 self.assertEqual(bag, [])
857 f.set_result('foo')
858
859 self.run_briefly()
860
861 self.assertEqual(bag, [42, 17])
862 self.assertEqual(f.result(), 'foo')
863
864 def test_callbacks_invoked_on_set_exception(self):
865 bag = []
866 f = self._new_future()
867 f.add_done_callback(self._make_callback(bag, 100))
868
869 self.assertEqual(bag, [])
870 exc = RuntimeError()
871 f.set_exception(exc)
872
873 self.run_briefly()
874
875 self.assertEqual(bag, [100])
876 self.assertEqual(f.exception(), exc)
877
878 def test_remove_done_callback(self):
879 bag = []
880 f = self._new_future()
881 cb1 = self._make_callback(bag, 1)
882 cb2 = self._make_callback(bag, 2)
883 cb3 = self._make_callback(bag, 3)
884
885 # Add one cb1 and one cb2.
886 f.add_done_callback(cb1)
887 f.add_done_callback(cb2)
888
889 # One instance of cb2 removed. Now there's only one cb1.
890 self.assertEqual(f.remove_done_callback(cb2), 1)
891
892 # Never had any cb3 in there.
893 self.assertEqual(f.remove_done_callback(cb3), 0)
894
895 # After this there will be 6 instances of cb1 and one of cb2.
896 f.add_done_callback(cb2)
897 for i in range(5):
898 f.add_done_callback(cb1)
899
900 # Remove all instances of cb1. One cb2 remains.
901 self.assertEqual(f.remove_done_callback(cb1), 6)
902
903 self.assertEqual(bag, [])
904 f.set_result('foo')
905
906 self.run_briefly()
907
908 self.assertEqual(bag, [2])
909 self.assertEqual(f.result(), 'foo')
910
911 def test_remove_done_callbacks_list_mutation(self):
912 # see http://bugs.python.org/issue28963 for details
913
914 fut = self._new_future()
915 fut.add_done_callback(str)
916
917 for _ in range(63):
918 fut.add_done_callback(id)
919
920 class evil:
921 def __eq__(self, other):
922 fut.remove_done_callback(id)
923 return False
924
925 fut.remove_done_callback(evil())
926
927 def test_remove_done_callbacks_list_clear(self):
928 # see https://github.com/python/cpython/issues/97592 for details
929
930 fut = self._new_future()
931 fut.add_done_callback(str)
932
933 for _ in range(63):
934 fut.add_done_callback(id)
935
936 class evil:
937 def __eq__(self, other):
938 fut.remove_done_callback(other)
939
940 fut.remove_done_callback(evil())
941
942 def test_schedule_callbacks_list_mutation_1(self):
943 # see http://bugs.python.org/issue28963 for details
944
945 def mut(f):
946 f.remove_done_callback(str)
947
948 fut = self._new_future()
949 fut.add_done_callback(mut)
950 fut.add_done_callback(str)
951 fut.add_done_callback(str)
952 fut.set_result(1)
953 test_utils.run_briefly(self.loop)
954
955 def test_schedule_callbacks_list_mutation_2(self):
956 # see http://bugs.python.org/issue30828 for details
957
958 fut = self._new_future()
959 fut.add_done_callback(str)
960
961 for _ in range(63):
962 fut.add_done_callback(id)
963
964 max_extra_cbs = 100
965 extra_cbs = 0
966
967 class evil:
968 def __eq__(self, other):
969 nonlocal extra_cbs
970 extra_cbs += 1
971 if extra_cbs < max_extra_cbs:
972 fut.add_done_callback(id)
973 return False
974
975 fut.remove_done_callback(evil())
976
977 def test_evil_call_soon_list_mutation(self):
978 # see: https://github.com/python/cpython/issues/125969
979 called_on_fut_callback0 = False
980
981 pad = lambda: ...
982
983 def evil_call_soon(*args, **kwargs):
984 nonlocal called_on_fut_callback0
985 if called_on_fut_callback0:
986 # Called when handling fut->fut_callbacks[0]
987 # and mutates the length fut->fut_callbacks.
988 fut.remove_done_callback(int)
989 fut.remove_done_callback(pad)
990 else:
991 called_on_fut_callback0 = True
992
993 fake_event_loop = SimpleEvilEventLoop()
994 fake_event_loop.call_soon = evil_call_soon
995
996 with mock.patch.object(self, 'loop', fake_event_loop):
997 fut = self._new_future()
998 self.assertIs(fut.get_loop(), fake_event_loop)
999
1000 fut.add_done_callback(str) # sets fut->fut_callback0
1001 fut.add_done_callback(int) # sets fut->fut_callbacks[0]
1002 fut.add_done_callback(pad) # sets fut->fut_callbacks[1]
1003 fut.add_done_callback(pad) # sets fut->fut_callbacks[2]
1004 fut.set_result("boom")
1005
1006 # When there are no more callbacks, the Python implementation
1007 # returns an empty list but the C implementation returns None.
1008 self.assertIn(fut._callbacks, (None, []))
1009
1010 def test_use_after_free_on_fut_callback_0_with_evil__eq__(self):
1011 # Special thanks to Nico-Posada for the original PoC.
1012 # See https://github.com/python/cpython/issues/125966.
1013
1014 fut = self._new_future()
1015
1016 class cb_pad:
1017 def __eq__(self, other):
1018 return True
1019
1020 class evil(cb_pad):
1021 def __eq__(self, other):
1022 fut.remove_done_callback(None)
1023 return NotImplemented
1024
1025 fut.add_done_callback(cb_pad())
1026 fut.remove_done_callback(evil())
1027
1028 def test_use_after_free_on_fut_callback_0_with_evil__getattribute__(self):
1029 # see: https://github.com/python/cpython/issues/125984
1030
1031 class EvilEventLoop(SimpleEvilEventLoop):
1032 def call_soon(self, *args, **kwargs):
1033 super().call_soon(*args, **kwargs)
1034 raise ReachableCode
1035
1036 def __getattribute__(self, name):
1037 nonlocal fut_callback_0
1038 if name == 'call_soon':
1039 fut.remove_done_callback(fut_callback_0)
1040 del fut_callback_0
1041 return object.__getattribute__(self, name)
1042
1043 evil_loop = EvilEventLoop()
1044 with mock.patch.object(self, 'loop', evil_loop):
1045 fut = self._new_future()
1046 self.assertIs(fut.get_loop(), evil_loop)
1047
1048 fut_callback_0 = lambda: ...
1049 fut.add_done_callback(fut_callback_0)
1050 self.assertRaises(ReachableCode, fut.set_result, "boom")
1051
1052 def test_use_after_free_on_fut_context_0_with_evil__getattribute__(self):
1053 # see: https://github.com/python/cpython/issues/125984
1054
1055 class EvilEventLoop(SimpleEvilEventLoop):
1056 def call_soon(self, *args, **kwargs):
1057 super().call_soon(*args, **kwargs)
1058 raise ReachableCode
1059
1060 def __getattribute__(self, name):
1061 if name == 'call_soon':
1062 # resets the future's event loop
1063 fut.__init__(loop=SimpleEvilEventLoop())
1064 return object.__getattribute__(self, name)
1065
1066 evil_loop = EvilEventLoop()
1067 with mock.patch.object(self, 'loop', evil_loop):
1068 fut = self._new_future()
1069 self.assertIs(fut.get_loop(), evil_loop)
1070
1071 fut_callback_0 = mock.Mock()
1072 fut_context_0 = mock.Mock()
1073 fut.add_done_callback(fut_callback_0, context=fut_context_0)
1074 del fut_context_0
1075 del fut_callback_0
1076 self.assertRaises(ReachableCode, fut.set_result, "boom")
1077
1078
1079 @unittest.skipUnless(hasattr(futures, '_CFuture'),
1080 'requires the C _asyncio module')
1081 class CFutureDoneCallbackTests(BaseFutureDoneCallbackTests,
1082 test_utils.TestCase):
1083
1084 def _new_future(self):
1085 return futures._CFuture(loop=self.loop)
1086
1087
1088 @unittest.skipUnless(hasattr(futures, '_CFuture'),
1089 'requires the C _asyncio module')
1090 class CSubFutureDoneCallbackTests(BaseFutureDoneCallbackTests,
1091 test_utils.TestCase):
1092
1093 def _new_future(self):
1094 class CSubFuture(futures._CFuture):
1095 pass
1096 return CSubFuture(loop=self.loop)
1097
1098
1099 class PyFutureDoneCallbackTests(BaseFutureDoneCallbackTests,
1100 test_utils.TestCase):
1101
1102 def _new_future(self):
1103 return futures._PyFuture(loop=self.loop)
1104
1105
1106 class BaseFutureInheritanceTests:
1107
1108 def _get_future_cls(self):
1109 raise NotImplementedError
1110
1111 def setUp(self):
1112 super().setUp()
1113 self.loop = self.new_test_loop()
1114 self.addCleanup(self.loop.close)
1115
1116 def test_inherit_without_calling_super_init(self):
1117 # See https://bugs.python.org/issue38785 for the context
1118 cls = self._get_future_cls()
1119
1120 class MyFut(cls):
1121 def __init__(self, *args, **kwargs):
1122 # don't call super().__init__()
1123 pass
1124
1125 fut = MyFut(loop=self.loop)
1126 with self.assertRaisesRegex(
1127 RuntimeError,
1128 "Future object is not initialized."
1129 ):
1130 fut.get_loop()
1131
1132
1133 class PyFutureInheritanceTests(BaseFutureInheritanceTests,
1134 test_utils.TestCase):
1135 def _get_future_cls(self):
1136 return futures._PyFuture
1137
1138
1139 @unittest.skipUnless(hasattr(futures, '_CFuture'),
1140 'requires the C _asyncio module')
1141 class CFutureInheritanceTests(BaseFutureInheritanceTests,
1142 test_utils.TestCase):
1143 def _get_future_cls(self):
1144 return futures._CFuture
1145
1146
1147 if __name__ == '__main__':
1148 unittest.main()