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:
parent
36f37c18b1
commit
9f3e4f3d4a
1 changed files with 10 additions and 1 deletions
|
@ -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())
|
||||
|
|
Loading…
Reference in a new issue