Change to the way the builtin BaseException
is checked for, to avoid
confusing pyflakes/pylint (plus a few whitespace/linelen tweaks)
This commit is contained in:
@@ -20,6 +20,13 @@
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.from eventlet.support import greenlets as greenlet
|
||||
|
||||
import __builtin__
|
||||
if not hasattr(__builtin__, 'BaseException'): # Python < 2.5
|
||||
class BaseException: # pylint: disable-msg=W0622
|
||||
# not subclassing from object() intentionally, because in
|
||||
# that case "raise Timeout" fails with TypeError.
|
||||
pass
|
||||
|
||||
from eventlet.support import greenlets as greenlet
|
||||
from eventlet.hubs import get_hub
|
||||
|
||||
@@ -28,14 +35,6 @@ __all__ = ['Timeout',
|
||||
|
||||
_NONE = object()
|
||||
|
||||
try:
|
||||
BaseException
|
||||
except NameError: # Python < 2.5
|
||||
class BaseException:
|
||||
# not subclassing from object() intentionally, because in
|
||||
# that case "raise Timeout" fails with TypeError.
|
||||
pass
|
||||
|
||||
# deriving from BaseException so that "except Exception, e" doesn't catch
|
||||
# Timeout exceptions.
|
||||
class Timeout(BaseException):
|
||||
@@ -61,13 +60,16 @@ class Timeout(BaseException):
|
||||
"""Schedule the timeout. This is called on construction, so
|
||||
it should not be called explicitly, unless the timer has been
|
||||
cancelled."""
|
||||
assert not self.pending, '%r is already started; to restart it, cancel it first' % self
|
||||
assert not self.pending, \
|
||||
'%r is already started; to restart it, cancel it first' % self
|
||||
if self.seconds is None: # "fake" timeout (never expires)
|
||||
self.timer = None
|
||||
elif self.exception is None or self.exception is False: # timeout that raises self
|
||||
self.timer = get_hub().schedule_call_global(self.seconds, greenlet.getcurrent().throw, self)
|
||||
self.timer = get_hub().schedule_call_global(
|
||||
self.seconds, greenlet.getcurrent().throw, self)
|
||||
else: # regular timeout with user-provided exception
|
||||
self.timer = get_hub().schedule_call_global(self.seconds, greenlet.getcurrent().throw, self.exception)
|
||||
self.timer = get_hub().schedule_call_global(
|
||||
self.seconds, greenlet.getcurrent().throw, self.exception)
|
||||
return self
|
||||
|
||||
@property
|
||||
@@ -79,11 +81,11 @@ class Timeout(BaseException):
|
||||
return False
|
||||
|
||||
def cancel(self):
|
||||
"""If the timeout is pending, cancel it. If not using Timeouts in
|
||||
``with`` statements, always call cancel() in a ``finally`` after the
|
||||
block of code that is getting timed out. If not cancelled, the timeout
|
||||
will be raised later on, in some unexpected section of the
|
||||
application."""
|
||||
"""If the timeout is pending, cancel it. If not using
|
||||
Timeouts in ``with`` statements, always call cancel() in a
|
||||
``finally`` after the block of code that is getting timed out.
|
||||
If not cancelled, the timeout will be raised later on, in some
|
||||
unexpected section of the application."""
|
||||
if self.timer is not None:
|
||||
self.timer.cancel()
|
||||
self.timer = None
|
||||
@@ -101,7 +103,8 @@ class Timeout(BaseException):
|
||||
exception = ''
|
||||
else:
|
||||
exception = ' exception=%r' % self.exception
|
||||
return '<%s at %s seconds=%s%s%s>' % (classname, hex(id(self)), self.seconds, exception, pending)
|
||||
return '<%s at %s seconds=%s%s%s>' % (
|
||||
classname, hex(id(self)), self.seconds, exception, pending)
|
||||
|
||||
def __str__(self):
|
||||
"""
|
||||
@@ -150,4 +153,3 @@ def with_timeout(seconds, function, *args, **kwds):
|
||||
raise
|
||||
finally:
|
||||
timeout.cancel()
|
||||
|
||||
|
Reference in New Issue
Block a user