Skip to content

Commit 3448389

Browse files
committed
0 is no-op timeout
1 parent dfc3eec commit 3448389

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

CHANGES.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
CHANGES
22
=======
33

4-
1.1.1 (2017-03-11)
4+
1.2.0 (2017-03-11)
55
------------------
66

77
* Extra check on context manager exit
88

9+
* 0 is no-op timeout
10+
911

1012
1.1.0 (2016-10-20)
1113
------------------

async_timeout/__init__.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import asyncio
22

33

4-
__version__ = '1.1.1'
4+
__version__ = '1.2.0'
55

66

77
class timeout:
@@ -19,6 +19,8 @@ class timeout:
1919
loop - asyncio compatible event loop
2020
"""
2121
def __init__(self, timeout, *, loop=None):
22+
if timeout is not None and timeout == 0:
23+
timeout = None
2224
self._timeout = timeout
2325
if loop is None:
2426
loop = asyncio.get_event_loop()
@@ -28,11 +30,11 @@ def __init__(self, timeout, *, loop=None):
2830
self._cancel_handler = None
2931

3032
def __enter__(self):
31-
self._task = asyncio.Task.current_task(loop=self._loop)
32-
if self._task is None:
33-
raise RuntimeError('Timeout context manager should be used '
34-
'inside a task')
3533
if self._timeout is not None:
34+
self._task = asyncio.Task.current_task(loop=self._loop)
35+
if self._task is None:
36+
raise RuntimeError('Timeout context manager should be used '
37+
'inside a task')
3638
self._cancel_handler = self._loop.call_later(
3739
self._timeout, self._cancel_task)
3840
return self

tests/test_timeout.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,21 @@ def long_running_task():
8080
assert 0.09 < dt < 0.13, dt
8181

8282

83+
@asyncio.coroutine
84+
def test_timeout_disable_zero(loop):
85+
@asyncio.coroutine
86+
def long_running_task():
87+
yield from asyncio.sleep(0.1, loop=loop)
88+
return 'done'
89+
90+
t0 = loop.time()
91+
with timeout(0, loop=loop):
92+
resp = yield from long_running_task()
93+
assert resp == 'done'
94+
dt = loop.time() - t0
95+
assert 0.09 < dt < 0.13, dt
96+
97+
8398
@asyncio.coroutine
8499
def test_timeout_not_relevant_exception(loop):
85100
yield from asyncio.sleep(0, loop=loop)

0 commit comments

Comments
 (0)