From 0e5bfef651d851b7589200052f9e7b21c1843682 Mon Sep 17 00:00:00 2001 From: "R. Bernstein" Date: Wed, 4 Dec 2024 06:25:07 -0500 Subject: [PATCH] `ReadList[]` handling `Null` and `Hold[]` expressions (#1203) See the `ReadList[]` example in https://github.com/Mathics3/Mathics3-Rubi/pull/2 --------- Co-authored-by: Aravindh Krishnamoorthy --- mathics/eval/files_io/files.py | 9 ++++++++- test/builtin/files_io/test_files.py | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/mathics/eval/files_io/files.py b/mathics/eval/files_io/files.py index 0a1578c3f..14b0980c1 100644 --- a/mathics/eval/files_io/files.py +++ b/mathics/eval/files_io/files.py @@ -200,7 +200,9 @@ def eval_Read( except Exception as e: print(e) - if expr is SymbolEndOfFile: + if expr is None: + result.append(None) + elif expr is SymbolEndOfFile: evaluation.message(name, "readt", tmp, String(stream.name)) return SymbolFailed elif isinstance(expr, BaseElement): @@ -275,5 +277,10 @@ def eval_Read( return [from_python(part) for part in result] elif result_len == 1: result = result[0] + if SymbolHoldExpression in types: + if hasattr(result, "head") and result.head is SymbolHold: + return from_python(result) + else: + return Expression(SymbolHold, from_python(result)) return from_python(result) diff --git a/test/builtin/files_io/test_files.py b/test/builtin/files_io/test_files.py index 17c11a2d5..c1decd9b3 100644 --- a/test/builtin/files_io/test_files.py +++ b/test/builtin/files_io/test_files.py @@ -295,6 +295,18 @@ def test_close(): "{{a, 1}}", "", ), + ( + 'ReadList[StringToStream["(**)"], Expression]', + None, + "{Null}", + "", + ), + ( + 'ReadList[StringToStream["Hold[1+2]"], Expression]', + None, + "{Hold[1 + 2]}", + "", + ), ('stream = StringToStream["Mathics is cool!"];', None, "Null", ""), ("SetStreamPosition[stream, -5]", ("Invalid I/O Seek.",), "0", ""), (