Skip to content

Commit 0e5bfef

Browse files
ReadList[] handling Null and Hold[] expressions (#1203)
See the `ReadList[]` example in Mathics3/Mathics3-Package-Rubi#2 --------- Co-authored-by: Aravindh Krishnamoorthy <aravindh.krishnamoorthy@fau.de>
1 parent 2636552 commit 0e5bfef

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

mathics/eval/files_io/files.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ def eval_Read(
200200
except Exception as e:
201201
print(e)
202202

203-
if expr is SymbolEndOfFile:
203+
if expr is None:
204+
result.append(None)
205+
elif expr is SymbolEndOfFile:
204206
evaluation.message(name, "readt", tmp, String(stream.name))
205207
return SymbolFailed
206208
elif isinstance(expr, BaseElement):
@@ -275,5 +277,10 @@ def eval_Read(
275277
return [from_python(part) for part in result]
276278
elif result_len == 1:
277279
result = result[0]
280+
if SymbolHoldExpression in types:
281+
if hasattr(result, "head") and result.head is SymbolHold:
282+
return from_python(result)
283+
else:
284+
return Expression(SymbolHold, from_python(result))
278285

279286
return from_python(result)

test/builtin/files_io/test_files.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,18 @@ def test_close():
295295
"{{a, 1}}",
296296
"",
297297
),
298+
(
299+
'ReadList[StringToStream["(**)"], Expression]',
300+
None,
301+
"{Null}",
302+
"",
303+
),
304+
(
305+
'ReadList[StringToStream["Hold[1+2]"], Expression]',
306+
None,
307+
"{Hold[1 + 2]}",
308+
"",
309+
),
298310
('stream = StringToStream["Mathics is cool!"];', None, "Null", ""),
299311
("SetStreamPosition[stream, -5]", ("Invalid I/O Seek.",), "0", ""),
300312
(

0 commit comments

Comments
 (0)