Skip to content

Commit f649290

Browse files
committed
Implement panic() with magic methods rather than @contextmanager
1 parent 8c9fb7b commit f649290

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

src/bd2k/util/exceptions.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
import sys
33

44

5-
@contextmanager
6-
def panic( log=None ):
5+
class panic( object ):
76
"""
87
The Python idiom for reraising a primary exception fails when the except block raises a
98
secondary exception, e.g. while trying to cleanup. In that case the original exception is
@@ -21,13 +20,18 @@ def panic( log=None ):
2120
If a logging logger is passed to panic(), any secondary Exception raised within the with
2221
block will be logged. Otherwise those exceptions are swallowed. At the end of the with block
2322
the primary exception will be reraised.
24-
2523
"""
26-
exc_type, exc_value, traceback = sys.exc_info( )
27-
try:
28-
yield
29-
except Exception as e:
30-
if log is not None:
31-
log.warn( "Exception during panic", exc_info=True )
32-
finally:
24+
25+
def __init__( self, log=None ):
26+
super( panic, self ).__init__( )
27+
self.log = log
28+
self.exc_info = None
29+
30+
def __enter__( self ):
31+
self.exc_info = sys.exc_info( )
32+
33+
def __exit__( self, *exc_info ):
34+
if self.log is not None:
35+
self.log.warn( "Exception during panic", exc_info=exc_info )
36+
exc_type, exc_value, traceback = self.exc_info
3337
raise exc_type, exc_value, traceback

0 commit comments

Comments
 (0)