mirror of
https://github.com/python/cpython.git
synced 2025-07-19 01:05:26 +00:00
bpo-33238: Add InvalidStateError to concurrent.futures. (GH-7056)
Future.set_result and Future.set_exception now raise InvalidStateError if the futures are not pending or running. This mirrors the behavior of asyncio.Future, and prevents AssertionErrors in asyncio.wrap_future when set_result is called multiple times.
This commit is contained in:
parent
bb9474f1fb
commit
0a28c0d12e
6 changed files with 59 additions and 6 deletions
|
@ -1206,6 +1206,34 @@ class FutureTests(BaseTestCase):
|
|||
self.assertTrue(isinstance(f1.exception(timeout=5), OSError))
|
||||
t.join()
|
||||
|
||||
def test_multiple_set_result(self):
|
||||
f = create_future(state=PENDING)
|
||||
f.set_result(1)
|
||||
|
||||
with self.assertRaisesRegex(
|
||||
futures.InvalidStateError,
|
||||
'FINISHED: <Future at 0x[0-9a-f]+ '
|
||||
'state=finished returned int>'
|
||||
):
|
||||
f.set_result(2)
|
||||
|
||||
self.assertTrue(f.done())
|
||||
self.assertEqual(f.result(), 1)
|
||||
|
||||
def test_multiple_set_exception(self):
|
||||
f = create_future(state=PENDING)
|
||||
e = ValueError()
|
||||
f.set_exception(e)
|
||||
|
||||
with self.assertRaisesRegex(
|
||||
futures.InvalidStateError,
|
||||
'FINISHED: <Future at 0x[0-9a-f]+ '
|
||||
'state=finished raised ValueError>'
|
||||
):
|
||||
f.set_exception(Exception())
|
||||
|
||||
self.assertEqual(f.exception(), e)
|
||||
|
||||
|
||||
@test.support.reap_threads
|
||||
def test_main():
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue