From eca5e41cc9a4f82c4dc4549588924afc73a3bde8 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Tue, 14 Feb 2023 17:40:49 -0800 Subject: [PATCH] Conform Python error messages. --- implementations/Python/joy.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/implementations/Python/joy.py b/implementations/Python/joy.py index 7362122..79c5793 100755 --- a/implementations/Python/joy.py +++ b/implementations/Python/joy.py @@ -458,6 +458,21 @@ Read-Evaluate-Print Loop ''' +def hack_error_message(exception): + ''' + Some of the Python exception messages (such as when you attempt to + shift a number by a negative amount of bits) are used as Joy error + messages. They should start with a capital letter and end with a + period. This function takes care of that. + ''' + message = str(exception) + if message[0].islower(): + message = message[0].swapcase() + message[1:] + if '.' != message[-1]: + message += '.' + print(message) + + def repl(stack=(), dictionary=None): ''' Read-Evaluate-Print Loop @@ -479,10 +494,12 @@ def repl(stack=(), dictionary=None): break try: stack, dictionary = run(text, stack, dictionary) + except UnknownSymbolError as sym: + print('Unknown:', sym) except SystemExit as e: raise SystemExit from e - except: - print_exc() + except Exception as e: + hack_error_message(e) print(stack_to_string(stack)) except SystemExit as e: raise SystemExit from e @@ -530,8 +547,8 @@ def interp(stack=(), dictionary=None): print(e) except SystemExit as e: raise SystemExit from e - except: - print_exc() + except Exception as e: + hack_error_message(e) print(stack_to_string(stack)) except SystemExit as e: raise SystemExit from e