From d14784eeb98000b30af8bb285011eb9506e419da Mon Sep 17 00:00:00 2001 From: Denis Bilenko Date: Wed, 10 Dec 2008 01:57:29 +0600 Subject: [PATCH] fixed bug with nested api.with_timeout interfering with each other; added a testcase --- eventlet/api.py | 7 ++++--- greentest/test__api.py | 7 ++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/eventlet/api.py b/eventlet/api.py index aa15b63..e99cacf 100644 --- a/eventlet/api.py +++ b/eventlet/api.py @@ -395,12 +395,13 @@ def with_timeout(seconds, func, *args, **kwds): # pass timeout_value through to func(). has_timeout_value = "timeout_value" in kwds timeout_value = kwds.pop("timeout_value", None) - timeout = exc_after(seconds, TimeoutError()) + error = TimeoutError() + timeout = exc_after(seconds, error) try: try: return func(*args, **kwds) - except TimeoutError: - if has_timeout_value: + except TimeoutError, ex: + if ex is error and has_timeout_value: return timeout_value raise finally: diff --git a/greentest/test__api.py b/greentest/test__api.py index 584cf2a..31d7350 100644 --- a/greentest/test__api.py +++ b/greentest/test__api.py @@ -1,5 +1,5 @@ import unittest -from eventlet.api import sleep, spawn, kill +from eventlet.api import sleep, spawn, kill, with_timeout, TimeoutError DELAY = 0.1 @@ -29,6 +29,11 @@ class Test(unittest.TestCase): sleep(DELAY) assert state == ['start', 'except', 'finished'], state + def test_nested_with_timeout(self): + def func(): + return with_timeout(0.2, sleep, 2, timeout_value=1) + self.assertRaises(TimeoutError, with_timeout, 0.1, func) + if __name__=='__main__': unittest.main()