Created
August 26, 2013 08:03
-
-
Save ov7a/6339051 to your computer and use it in GitHub Desktop.
Python global exception hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def log_uncaught_exceptions(ex_cls, ex, tb): | |
logging.critical(''.join(traceback.format_tb(tb))) | |
logging.critical('{0}: {1}'.format(ex_cls, ex)) | |
os._exit(1) | |
sys.excepthook = log_uncaught_exceptions | |
def install_thread_excepthook(): | |
""" | |
Workaround for sys.excepthook thread bug | |
(https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1230540&group_id=5470). | |
Call once from __main__ before creating any threads. | |
If using psyco, call psycho.cannotcompile(threading.Thread.run) | |
since this replaces a new-style class method. | |
""" | |
run_old = threading.Thread.run | |
def run(*args, **kwargs): | |
try: | |
run_old(*args, **kwargs) | |
except: | |
sys.excepthook(*sys.exc_info()) | |
threading.Thread.run = run | |
install_thread_excepthook() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment