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.
This commit is contained in:
Andrew Hamilton 2022-01-05 23:46:25 +10:00
parent 3a826c1bc0
commit a368da8191

View file

@ -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()