Commit 85663044 authored by Denis Bilenko's avatar Denis Bilenko

get rid of _SilentException

parent 772ff231
...@@ -171,13 +171,6 @@ except NameError: # Python < 2.5 ...@@ -171,13 +171,6 @@ except NameError: # Python < 2.5
pass pass
class _SilentException(BaseException):
"""Used internally by Timeout as an exception which is not raised outside of with-block,
and therefore is not visible by the user, unless she uses "except:" construct.
"""
__slots__ = []
class Timeout(BaseException): class Timeout(BaseException):
"""Raise an exception in the current greenlet after timeout. """Raise an exception in the current greenlet after timeout.
...@@ -199,10 +192,8 @@ class Timeout(BaseException): ...@@ -199,10 +192,8 @@ class Timeout(BaseException):
... code block ... ... code block ...
This is equivalent to try/finally block above with one additional feature: This is equivalent to try/finally block above with one additional feature:
if exception is False, code block will be interrupted "silently". Under the if exception is False, the timeout is still raised, but context manager
hood, an exception of a type _SilentException (subclass of BaseException suppresses it, so surrounding code won't see it.
but not Exception) is raised. If it exits the block it is suppressed by the
context manager, so that outside code won't see it.
This is handy for adding a timeout feature to the functions that don't This is handy for adding a timeout feature to the functions that don't
implement it themselves: implement it themselves:
...@@ -234,12 +225,9 @@ class Timeout(BaseException): ...@@ -234,12 +225,9 @@ class Timeout(BaseException):
if seconds is None: # "fake" timeout (never expires) if seconds is None: # "fake" timeout (never expires)
self.exception = None self.exception = None
self.timer = None self.timer = None
elif exception is None: # timeout that raises self elif exception is None or exception is False: # timeout that raises self
self.exception = None self.exception = exception
self.timer = core.timer(seconds, getcurrent().throw, self) self.timer = core.timer(seconds, getcurrent().throw, self)
elif exception is False: # timeout that interrupts the with-block "silently"
self.exception = _SilentException()
self.timer = core.timer(seconds, getcurrent().throw, self.exception)
else: # regular timeout with user-provided exception else: # regular timeout with user-provided exception
self.exception = exception self.exception = exception
self.timer = core.timer(seconds, getcurrent().throw, exception) self.timer = core.timer(seconds, getcurrent().throw, exception)
...@@ -282,7 +270,7 @@ class Timeout(BaseException): ...@@ -282,7 +270,7 @@ class Timeout(BaseException):
def __exit__(self, typ, value, tb): def __exit__(self, typ, value, tb):
self.cancel() self.cancel()
if typ is _SilentException and value is self.exception: if value is self and self.exception is False:
return True return True
......
...@@ -25,7 +25,6 @@ import greentest ...@@ -25,7 +25,6 @@ import greentest
import weakref import weakref
import time import time
from gevent import sleep, Timeout from gevent import sleep, Timeout
from gevent.greenlet import _SilentException
DELAY = 0.04 DELAY = 0.04
class Error(Exception): class Error(Exception):
...@@ -84,9 +83,7 @@ class Test(greentest.TestCase): ...@@ -84,9 +83,7 @@ class Test(greentest.TestCase):
timer.cancel() timer.cancel()
sleep(DELAY*2) sleep(DELAY*2)
# To silent the exception, pass False as second parameter. The with-block # To silent the exception before exiting the block, pass False as second parameter.
# will be interrupted with _SilentException, but it won't be propagated
# outside.
XDELAY=0.1 XDELAY=0.1
start = time.time() start = time.time()
with Timeout(XDELAY, False): with Timeout(XDELAY, False):
...@@ -113,11 +110,6 @@ class Test(greentest.TestCase): ...@@ -113,11 +110,6 @@ class Test(greentest.TestCase):
sleep(DELAY*3) sleep(DELAY*3)
raise AssertionError('should not get there') raise AssertionError('should not get there')
with Timeout(DELAY, _SilentException()):
with Timeout(DELAY*2, _SilentException()):
sleep(DELAY*3)
raise AssertionError('should not get there')
with Timeout(DELAY) as t1: with Timeout(DELAY) as t1:
with Timeout(DELAY*2) as t2: with Timeout(DELAY*2) as t2:
try: try:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment