forked from Charcoal-SE/SmokeDetector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
excepthook.py
50 lines (44 loc) · 1.7 KB
/
excepthook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# coding=utf-8
from datetime import datetime
import os
import traceback
import threading
import sys
# noinspection PyPackageRequirements
from websocket import WebSocketConnectionClosedException
import requests
from helpers import exit_mode, log, log_exception
from globalvars import GlobalVars
# noinspection PyProtectedMember
def uncaught_exception(exctype, value, tb):
delta = datetime.utcnow() - GlobalVars.startup_utc_date
log_exception(exctype, value, tb)
if delta.total_seconds() < 180 and exctype not in \
{KeyboardInterrupt, SystemExit, requests.ConnectionError, WebSocketConnectionClosedException}:
exit_mode("early_exception")
else:
exit_mode("restart")
def install_thread_excepthook():
"""
Workaround for sys.excepthook thread bug
From
http://spyced.blogspot.com/2007/06/workaround-for-sysexcepthook-bug.html
(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 psyco.cannotcompile(threading.Thread.run)
since this replaces a new-style class method.
"""
init_old = threading.Thread.__init__
def init(self, *args, **kwargs):
init_old(self, *args, **kwargs)
run_old = self.run
# noinspection PyBroadException,PyShadowingNames
def run_with_except_hook(*args, **kw):
try:
run_old(*args, **kw)
except Exception: # Broad exception makes sense here
sys.excepthook(*sys.exc_info())
except BaseException: # KeyboardInterrupt and SystemExit
raise
self.run = run_with_except_hook
threading.Thread.__init__ = init