Improve class name lookup when reraising

This commit is contained in:
Jonas Schneider
2016-04-30 22:31:18 -07:00
parent 5ac4eaea91
commit 4e575e7103

View File

@@ -11,20 +11,28 @@ def reraise(prefix=None, suffix=None):
old_exc_type, old_exc_value, traceback = sys.exc_info()
if old_exc_value is None:
old_exc_value = old_exc_type()
e = ReraisedException(old_exc_type, old_exc_value, prefix, suffix)
e = ReraisedException(old_exc_value, prefix, suffix)
reraise_impl(e, traceback)
# http://stackoverflow.com/a/13653312
def full_class_name(o):
module = o.__class__.__module__
if module is None or module == str.__class__.__module__:
return o.__class__.__name__
return module + '.' + o.__class__.__name__
class ReraisedException(Exception):
def __init__(self, old_exc_type, old_exc_value, prefix, suffix):
self.old_exc_type = old_exc_type
self.old_exc_value = old_exc_value
def __init__(self, old_exc, prefix, suffix):
self.old_exc = old_exc
self.prefix = prefix
self.suffix = suffix
def __str__(self):
klass = self.old_exc_type
orig = "%s.%s: %s" % (klass.__module__, klass.__name__, klass.__str__(self.old_exc_value))
klass = self.old_exc.__class__
orig = "%s: %s" % (full_class_name(self.old_exc), klass.__str__(self.old_exc))
prefixpart = suffixpart = ''
if self.prefix != '':
prefixpart = self.prefix + "\n"