Skip to content

Commit 401a1ab

Browse files
committed
Merge pull request #4 from BD2KGenomics/features/simplify-panic
Simplify panic() and minor fix to InnerClass
2 parents 8c9fb7b + ff0415f commit 401a1ab

2 files changed

Lines changed: 15 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

src/bd2k/util/objects.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ def __repr__( self ):
210210
return "%s bound to %s" % (super( BoundInner, self ).__repr__( ), repr( _outer ))
211211

212212
BoundInner.__name__ = self.inner_class.__name__
213+
BoundInner.__module__ = self.inner_class.__module__
213214
return BoundInner
214215

215216
def __call__( *args, **kwargs ):

0 commit comments

Comments
 (0)