From a368da8191a0290a39ed6a9727be085f9be85271 Mon Sep 17 00:00:00 2001 From: Andrew Hamilton Date: Wed, 5 Jan 2022 23:46:25 +1000 Subject: [PATCH] fill3: Handle unhandled exceptions. - Make sure all exceptions are handled. Exceptions occuring in update_screen and on_terminal_input are no longer lost. the program stops and the traceback is shown. - Tried to use set_exception_handler, but no go. --- fill3/fill3/__init__.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/fill3/fill3/__init__.py b/fill3/fill3/__init__.py index 44bc657..3aaa981 100755 --- a/fill3/fill3/__init__.py +++ b/fill3/fill3/__init__.py @@ -409,9 +409,24 @@ class Fixed: ########################## +EXCEPTION = None _LAST_APPEARANCE = [] +def handle_exception(): + def decorating_func(func): + def wrapper(*args): + try: + return func(*args) + except Exception as exc: + global EXCEPTION + EXCEPTION = exc + SHUTDOWN_EVENT.set() + return wrapper + return decorating_func + + +@handle_exception() def draw_screen(widget): global _LAST_APPEARANCE appearance = widget.appearance(os.get_terminal_size()) @@ -419,6 +434,7 @@ def draw_screen(widget): _LAST_APPEARANCE = appearance +@handle_exception() def patch_screen(widget): global _LAST_APPEARANCE appearance = widget.appearance(os.get_terminal_size()) @@ -437,6 +453,7 @@ async def update_screen(screen_widget): APPEARANCE_CHANGED_EVENT.clear() +@handle_exception() def on_terminal_input(screen_widget): term_code = sys.stdin.read() if term_code.startswith(terminal.MOUSE): @@ -474,7 +491,8 @@ async def tui(title, screen_widget): loop.remove_reader(sys.stdin) finally: update_task.cancel() - + if EXCEPTION is not None: + raise EXCEPTION ########################## @@ -490,6 +508,8 @@ class _Screen: def on_keyboard_input(self, term_code): if term_code in ["q", terminal.ESC, terminal.CTRL_C]: SHUTDOWN_EVENT.set() + elif term_code == "e": + raise AssertionError # Program should shutdown and show exception. else: self.content = Filler(Text(repr(term_code))) APPEARANCE_CHANGED_EVENT.set()