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.
This commit is contained in:
Kumi 2024-05-23 14:52:26 +02:00
parent 36f37c18b1
commit 9f3e4f3d4a
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -14,6 +14,15 @@ def interpreter():
result = eval(python_code, globals()) result = eval(python_code, globals())
if result is not None: 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: except Exception:
print(traceback.format_exc()) print(traceback.format_exc())