Better handle non-built-in exceptions

This commit is contained in:
Vgr E.Barry 2015-08-10 09:49:52 -04:00
parent d58a9249ab
commit 3534cc7d78

View File

@ -139,11 +139,25 @@ class ErrorHandler(io.TextIOWrapper):
def flush(self): def flush(self):
self.buffer.flush() self.buffer.flush()
import builtins
exc = self.data[-1].partition(":")[0] exc = self.data[-1].partition(":")[0]
if not issubclass(getattr(builtins, exc, BaseException), Exception): import builtins
if "." in exc:
import importlib
module, dot, name = exc.rpartition(".")
try:
module = importlib.import_module(module)
except ImportError:
exc = Exception
else:
exc = getattr(module, name.strip())
elif hasattr(builtins, exc):
exc = getattr(builtins, exc)
if not issubclass(exc, Exception):
del self.data[:]
return # not an actual exception return # not an actual exception
msg = "An error has occurred and has been logged." msg = "An error has occurred and has been logged."