From aec8ceb3f2bb3216121a9d139933893edbb7d6a2 Mon Sep 17 00:00:00 2001 From: Denis Bilenko Date: Wed, 10 Dec 2008 01:53:39 +0600 Subject: [PATCH] api.timeout: removed doctest, added a unittest. added a testcase for nested timeouts --- eventlet/api.py | 46 +++++-------------------- greentest/test__api_timeout.py | 61 ++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 37 deletions(-) create mode 100644 greentest/test__api_timeout.py diff --git a/eventlet/api.py b/eventlet/api.py index 0d31e00..aa15b63 100644 --- a/eventlet/api.py +++ b/eventlet/api.py @@ -327,46 +327,18 @@ class _SilentException(BaseException): pass class timeout: - """ - >>> from __future__ import with_statement - >>> from eventlet.api import sleep - >>> DELAY = 0.01 - - Nothing happens if with-block finishes before the timeout expires - >>> with timeout(DELAY*2): - ... sleep(DELAY) - >>> sleep(DELAY*2) # check if timer was actually cancelled + """Raise an exception in the block after timeout. - An exception will be raised if it's not - >>> with timeout(DELAY): - ... sleep(DELAY*2) - Traceback (most recent call last): - ... - TimeoutError - - You can customize the exception raised: - >>> with timeout(DELAY, IOError("Operation takes way too long")): - ... sleep(DELAY*2) - Traceback (most recent call last): - ... - IOError: Operation takes way too long + with timeout(seconds[, exc]): + ... code block ... - It's possible to cancel the timer inside the block: - >>> with timeout(DELAY) as timer: - ... timer.cancel() - ... sleep(DELAY*2) - - To silent the exception, pass None as second parameter. The with-block - will be interrupted with _SilentException, but it won't be propogated - outside. - >>> DELAY=0.1 - >>> import time - >>> start = time.time() - >>> with timeout(DELAY, None): - ... sleep(DELAY*2) - >>> (time.time()-start)