Skip to content

Commit 9806b48

Browse files
authored
Introduce expired property (#16)
* Introduce expired property * Update README * Update CHANGES
1 parent 2553670 commit 9806b48

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ CHANGES
77
* Don't suppress nested exception on timeout. Exception context points
88
on cancelled line with suspended `await` (#13)
99

10+
* Introduce `.timeout` property (#16)
11+
1012
1.2.1 (2017-05-02)
1113
------------------
1214

README.rst

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ because ``timeout`` doesn't create a new task.
1616
The ``timeout(timeout, *, loop=None)`` call returns a context manager
1717
that cancels a block on *timeout* expiring::
1818

19-
with timeout(1.5):
20-
yield from inner()
19+
with timeout(1.5):
20+
await inner()
2121

2222
1. If ``inner()`` is executed faster than in ``1.5`` seconds nothing
2323
happens.
@@ -27,6 +27,20 @@ that cancels a block on *timeout* expiring::
2727

2828
*timeout* parameter could be ``None`` for skipping timeout functionality.
2929

30+
31+
Context manager has ``.expired`` property for check if timeout happens
32+
exactly in context manager::
33+
34+
with timeout(1.5) as cm:
35+
await inner()
36+
print(cm.expired)
37+
38+
The property is ``True`` is ``inner()`` execution is cancelled by
39+
timeout context manager.
40+
41+
If ``inner()`` call explicitly raises ``TimeoutError`` ``cm.expired``
42+
is ``False``.
43+
3044
Installation
3145
------------
3246

async_timeout/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ def __exit__(self, exc_type, exc_val, exc_tb):
5050
self._task = None
5151

5252
def _cancel_task(self):
53-
self._cancelled = self._task.cancel()
53+
self._task.cancel()
54+
self._cancelled = True
55+
56+
@property
57+
def expired(self):
58+
return self._cancelled
5459

5560

5661
def current_task(loop):

tests/test_timeout.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,31 @@ def outer():
199199

200200
@asyncio.coroutine
201201
def test_timeout_suppress_exception_chain(loop):
202-
203202
with pytest.raises(asyncio.TimeoutError) as ctx:
204203
with timeout(0.01, loop=loop):
205204
yield from asyncio.sleep(10, loop=loop)
206205
assert not ctx.value.__suppress_context__
206+
207+
208+
@asyncio.coroutine
209+
def test_timeout_expired(loop):
210+
with pytest.raises(asyncio.TimeoutError):
211+
with timeout(0.01, loop=loop) as cm:
212+
yield from asyncio.sleep(10, loop=loop)
213+
assert cm.expired
214+
215+
216+
@asyncio.coroutine
217+
def test_timeout_inner_timeout_error(loop):
218+
with pytest.raises(asyncio.TimeoutError):
219+
with timeout(0.01, loop=loop) as cm:
220+
raise asyncio.TimeoutError
221+
assert not cm.expired
222+
223+
224+
@asyncio.coroutine
225+
def test_timeout_inner_other_error(loop):
226+
with pytest.raises(RuntimeError):
227+
with timeout(0.01, loop=loop) as cm:
228+
raise RuntimeError
229+
assert not cm.expired

0 commit comments

Comments
 (0)