Skip to content

Commit

Permalink
Roidelapluie functionkwargs (#65)
Browse files Browse the repository at this point in the history
* Allow to use timeout in function kwargs even when initial seconds is None

Initial support for timeout in function kwargs was implemented by 187ee34

* drop support for 2.6, 3.4, 3.5 and add support for 3.7, 3.8, 3.9

* drop support for 2.6, 3.4, 3.5 and add support for 3.7, 3.8, 3.9

* WIP trying to fix bild

* WIP trying to fix bild

* fix build

* bump version to 0.5.0

* bump version to 0.5.0

Co-authored-by: Julien Pivotto <roidelapluie@inuits.eu>
Co-authored-by: Patrick N <patricng>
  • Loading branch information
pnpnpn and roidelapluie committed Nov 15, 2020
1 parent 3c4bad7 commit 9fbc3ef
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 16 deletions.
5 changes: 2 additions & 3 deletions .travis.yml
@@ -1,11 +1,10 @@
language: python
sudo: false
python:
- '2.6'
- '2.7'
- '3.4'
- '3.5'
- '3.6'
- '3.7'
- '3.8'
install:
- pip install python-coveralls tox tox-travis
script: tox --recreate
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -27,7 +27,7 @@

setup(
name='timeout-decorator',
version='0.4.1',
version='0.5.0',
description='Timeout decorator',
long_description=long_description,
author='Patrick Ng',
Expand Down
8 changes: 8 additions & 0 deletions tests/test_timeout_decorator.py
Expand Up @@ -45,6 +45,14 @@ def f():
f(timeout=1)


def test_timeout_kwargs_with_initial_timeout_none(use_signals):
@timeout(use_signals=use_signals)
def f():
time.sleep(2)
with pytest.raises(TimeoutError):
f(timeout=1)


def test_timeout_no_seconds(use_signals):
@timeout(use_signals=use_signals)
def f():
Expand Down
2 changes: 1 addition & 1 deletion timeout_decorator/__init__.py
Expand Up @@ -4,4 +4,4 @@
from .timeout_decorator import TimeoutError

__title__ = 'timeout_decorator'
__version__ = '0.4.1'
__version__ = '0.5.0'
12 changes: 7 additions & 5 deletions timeout_decorator/timeout_decorator.py
Expand Up @@ -64,9 +64,6 @@ def timeout(seconds=None, use_signals=True, timeout_exception=TimeoutError, exce
"""
def decorate(function):

if not seconds:
return function

if use_signals:
def handler(signum, frame):
_raise_exception(timeout_exception, exception_message)
Expand All @@ -77,6 +74,10 @@ def new_function(*args, **kwargs):
if new_seconds:
old = signal.signal(signal.SIGALRM, handler)
signal.setitimer(signal.ITIMER_REAL, new_seconds)

if not seconds:
return function(*args, **kwargs)

try:
return function(*args, **kwargs)
finally:
Expand Down Expand Up @@ -144,7 +145,8 @@ def __call__(self, *args, **kwargs):
kwargs=kwargs)
self.__process.daemon = True
self.__process.start()
self.__timeout = self.__limit + time.time()
if self.__limit is not None:
self.__timeout = self.__limit + time.time()
while not self.ready:
time.sleep(0.01)
return self.value
Expand All @@ -159,7 +161,7 @@ def cancel(self):
@property
def ready(self):
"""Read-only property indicating status of "value" property."""
if self.__timeout < time.time():
if self.__limit and self.__timeout < time.time():
self.cancel()
return self.__queue.full() and not self.__queue.empty()

Expand Down
11 changes: 5 additions & 6 deletions tox.ini
@@ -1,17 +1,16 @@
[tox]
distshare={homedir}/.tox/distshare
envlist=py{26,27,34,35,36}
envlist=py{27,36,37,38}
skip_missing_interpreters=true
indexserver=
pypi = https://pypi.python.org/simple

[testenv]
commands=
py.test timeout_decorator tests --pep8
deps =
py32: pytest<3.0
py{26,27,34,35,36}: pytest>=3.0
pytest-pep8==1.0.6
py.test timeout_decorator tests
deps =
pytest
pytest-pep8

[pytest]
addopts = -vvl
Expand Down

0 comments on commit 9fbc3ef

Please sign in to comment.