Skip to content

Commit 8b286b7

Browse files
committed
Now app shows some inner-thread exceptions while in developer mode
1 parent 06e2012 commit 8b286b7

4 files changed

Lines changed: 40 additions & 23 deletions

File tree

active_window_checker.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from app_close import AppCloseManager
2525
from commented_config import CommentsHolder
2626
from inversion_rules import InversionRulesController
27+
from utils import show_exceptions
2728

2829
user32 = ctypes.windll.user32
2930
ole32 = ctypes.windll.ole32
@@ -174,6 +175,7 @@ def titles(iterable):
174175
yield win32gui.GetWindowText(hwnd)
175176

176177

178+
@show_exceptions()
177179
@inject.autoparams()
178180
def listen_switch_events(callback, close_manager: AppCloseManager):
179181
ole32.CoInitialize(0)

main_thread_loop.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
from dataclasses import dataclass, field
33
from queue import PriorityQueue
44
from typing import Callable
5+
56
import inject
67

8+
from utils import show_exceptions
9+
710

811
@dataclass(order=True)
912
class Callback:
@@ -25,8 +28,9 @@ async def run_loop(self):
2528
raise RuntimeError()
2629

2730
while self._alive:
28-
callback = self.callbacks.get()
29-
callback.func()
31+
with show_exceptions():
32+
callback = self.callbacks.get()
33+
callback.func()
3034

3135
def send_callback(self, callback: Callback):
3236
self.callbacks.put_nowait(callback)

tray/tray.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
from asyncio import to_thread
2-
from traceback import print_exc
2+
from contextlib import closing
3+
34
import inject
4-
import win32gui
5-
from win32con import SW_HIDE, SW_SHOW
65
import win32console
6+
import win32gui
77
from PIL import Image
88
from pystray import Menu, MenuItem, Icon
9+
from win32con import SW_HIDE, SW_SHOW
10+
911
import _meta as app
1012
from active_window_checker import AppMode
11-
from tray.utils import ref, make_toggle, make_radiobutton
12-
from uac import has_admin_rights
1313
from app_close import AppCloseManager
1414
from auto_update import AutoUpdater
1515
from interaction import InteractionManager
1616
from inversion_rules import InversionRulesController
17-
from settings import UserSettingsController, UserSettings, OPTION_PATH, OPTION_CHANGE_HANDLER, T
18-
from utils import explore, app_abs_path
17+
from settings import UserSettingsController, OPTION_PATH, OPTION_CHANGE_HANDLER, T
18+
from tray.utils import ref, make_toggle, make_radiobutton
19+
from uac import has_admin_rights
20+
from utils import explore, app_abs_path, show_exceptions
1921

2022

2123
class Tray:
@@ -39,23 +41,18 @@ def setup(self):
3941
))
4042
win32gui.ShowWindow(self.console_hwnd, SW_HIDE)
4143

44+
@show_exceptions()
4245
def run(self):
43-
try:
44-
self.tray = Icon(
45-
app.__product_name__,
46-
Image.open(app_abs_path(app.__icon__)),
47-
menu=self.build_menu()
48-
)
49-
self.tray.run()
50-
except:
51-
print_exc()
52-
raise
46+
self.tray = Icon(
47+
app.__product_name__,
48+
Image.open(app_abs_path(app.__icon__)),
49+
menu=self.build_menu()
50+
)
51+
self.tray.run()
5352

5453
async def run_async(self):
55-
try:
54+
with closing(self):
5655
await to_thread(self.run)
57-
finally:
58-
self.close()
5956

6057
def close(self):
6158
if self.tray:

utils.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import contextlib
12
import os
2-
from _meta import APP_DIR
33
import re
44
import subprocess
5+
import threading
6+
from traceback import print_exc
57

8+
from _meta import APP_DIR, __developer_mode__
69

710
FILEBROWSER_PATH = os.path.join(os.getenv('WINDIR'), 'explorer.exe')
811

@@ -149,3 +152,14 @@ def public_fields(object):
149152

150153
def app_abs_path(path: str):
151154
return os.path.join(APP_DIR, os.path.normpath(path))
155+
156+
157+
@contextlib.contextmanager
158+
def show_exceptions():
159+
try:
160+
yield
161+
except:
162+
if __developer_mode__:
163+
print("in Tread:", threading.current_thread().name)
164+
print_exc()
165+
raise

0 commit comments

Comments
 (0)