Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gevent
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
gevent
Commits
85663044
Commit
85663044
authored
Jul 30, 2009
by
Denis Bilenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
get rid of _SilentException
parent
772ff231
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
6 additions
and
26 deletions
+6
-26
gevent/greenlet.py
gevent/greenlet.py
+5
-17
greentest/test__api_timeout.py
greentest/test__api_timeout.py
+1
-9
No files found.
gevent/greenlet.py
View file @
85663044
...
@@ -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
...
...
greentest/test__api_timeout.py
View file @
85663044
...
@@ -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
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment