summaryrefslogtreecommitdiffstats
path: root/Lib/concurrent
diff options
context:
space:
mode:
authorjhaydaman <33549221+jhaydaman@users.noreply.github.com>2018-05-30 07:15:06 (GMT)
committerAndrew Svetlov <andrew.svetlov@gmail.com>2018-05-30 07:15:06 (GMT)
commit0a28c0d12ee7201de039ced4d815f57f1f8fd48c (patch)
tree9fa72f88b1f3035ea9fe5a4bde10215a432abe30 /Lib/concurrent
parentbb9474f1fb2fc7c7ed9f826b78262d6a12b5f9e8 (diff)
downloadcpython-0a28c0d12ee7201de039ced4d815f57f1f8fd48c.zip
cpython-0a28c0d12ee7201de039ced4d815f57f1f8fd48c.tar.gz
cpython-0a28c0d12ee7201de039ced4d815f57f1f8fd48c.tar.bz2
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.
Diffstat (limited to 'Lib/concurrent')
-rw-r--r--Lib/concurrent/futures/__init__.py1
-rw-r--r--Lib/concurrent/futures/_base.py8
2 files changed, 9 insertions, 0 deletions
diff --git a/Lib/concurrent/futures/__init__.py b/Lib/concurrent/futures/__init__.py
index 8434fcf..d746aea 100644
--- a/Lib/concurrent/futures/__init__.py
+++ b/Lib/concurrent/futures/__init__.py
@@ -10,6 +10,7 @@ from concurrent.futures._base import (FIRST_COMPLETED,
ALL_COMPLETED,
CancelledError,
TimeoutError,
+ InvalidStateError,
BrokenExecutor,
Future,
Executor,
diff --git a/Lib/concurrent/futures/_base.py b/Lib/concurrent/futures/_base.py
index 4f22f7e..d4416c6 100644
--- a/Lib/concurrent/futures/_base.py
+++ b/Lib/concurrent/futures/_base.py
@@ -53,6 +53,10 @@ class TimeoutError(Error):
"""The operation exceeded the given deadline."""
pass
+class InvalidStateError(Error):
+ """The operation is not allowed in this state."""
+ pass
+
class _Waiter(object):
"""Provides the event that wait() and as_completed() block on."""
def __init__(self):
@@ -513,6 +517,8 @@ class Future(object):
Should only be used by Executor implementations and unit tests.
"""
with self._condition:
+ if self._state in {CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED}:
+ raise InvalidStateError('{}: {!r}'.format(self._state, self))
self._result = result
self._state = FINISHED
for waiter in self._waiters:
@@ -526,6 +532,8 @@ class Future(object):
Should only be used by Executor implementations and unit tests.
"""
with self._condition:
+ if self._state in {CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED}:
+ raise InvalidStateError('{}: {!r}'.format(self._state, self))
self._exception = exception
self._state = FINISHED
for waiter in self._waiters: