From 9f3e4f3d4a8dc4038f61e44f2bf21bf1898926fd Mon Sep 17 00:00:00 2001 From: Kumi Date: Thu, 23 May 2024 14:52:26 +0200 Subject: [PATCH] feat(interpreter): enhance result representation Improve the handling of expression evaluation results in the interpreter by attempting to use `__repr__`, falling back to `str`, and finally relying on a basic string conversion if necessary. This approach ensures more robust and informative output for various types of objects, addressing potential issues where the direct conversion to a string might fail or not provide sufficiently detailed information. This enhancement makes the interpreter more user-friendly and resilient, delivering clearer feedback to users about the outcomes of their operations and making debugging more straightforward. --- src/deuthon/interpreter.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/deuthon/interpreter.py b/src/deuthon/interpreter.py index 873b3c2..1d1aadd 100644 --- a/src/deuthon/interpreter.py +++ b/src/deuthon/interpreter.py @@ -14,6 +14,15 @@ def interpreter(): result = eval(python_code, globals()) if result is not None: - print(result) + try: + result_string = result.__repr__() + except Exception: + try: + result_string = str(result) + except Exception: + result_string = f"{result}" + + print(result_string) + except Exception: print(traceback.format_exc())