Skip to content

Commit

Permalink
ReadList[] handling Null and Hold[] expressions (#1203)
Browse files Browse the repository at this point in the history
See the `ReadList[]` example in Mathics3/Mathics3-Rubi#2

---------

Co-authored-by: Aravindh Krishnamoorthy <[email protected]>
  • Loading branch information
rocky and aravindh-krishnamoorthy authored Dec 4, 2024
1 parent 2636552 commit 0e5bfef
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
9 changes: 8 additions & 1 deletion mathics/eval/files_io/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
12 changes: 12 additions & 0 deletions test/builtin/files_io/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", ""),
(
Expand Down

0 comments on commit 0e5bfef

Please sign in to comment.