aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/asyncio/timeouts.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/asyncio/timeouts.py')
-rw-r--r--Lib/asyncio/timeouts.py12
1 files changed, 8 insertions, 4 deletions
diff --git a/Lib/asyncio/timeouts.py b/Lib/asyncio/timeouts.py
index 029c468739b..30042abb3ad 100644
--- a/Lib/asyncio/timeouts.py
+++ b/Lib/asyncio/timeouts.py
@@ -49,8 +49,9 @@ class Timeout:
def reschedule(self, when: Optional[float]) -> None:
"""Reschedule the timeout."""
- assert self._state is not _State.CREATED
if self._state is not _State.ENTERED:
+ if self._state is _State.CREATED:
+ raise RuntimeError("Timeout has not been entered")
raise RuntimeError(
f"Cannot change state of {self._state.value} Timeout",
)
@@ -82,11 +83,14 @@ class Timeout:
return f"<Timeout [{self._state.value}]{info_str}>"
async def __aenter__(self) -> "Timeout":
+ if self._state is not _State.CREATED:
+ raise RuntimeError("Timeout has already been entered")
+ task = tasks.current_task()
+ if task is None:
+ raise RuntimeError("Timeout should be used inside a task")
self._state = _State.ENTERED
- self._task = tasks.current_task()
+ self._task = task
self._cancelling = self._task.cancelling()
- if self._task is None:
- raise RuntimeError("Timeout should be used inside a task")
self.reschedule(self._when)
return self