Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 38 additions & 15 deletions mathics/builtin/files_io/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ class EndOfFile(Builtin):
</dl>
"""

SymbolEndOfFile = Symbol("EndOfFile")


# TODO: Improve docs for these Read[] arguments.
class Byte(Builtin):
Expand Down Expand Up @@ -236,18 +238,24 @@ class Read(Builtin):
<dt>'Read[$stream$, $type$]'
<dd>reads the input stream and returns an object of the given type.

<dt>'Read[$stream$, $type$]'
<dd>reads the input stream and returns an object of the given type.

<dt>'Read[$stream$, Hold[Expression]]'
<dd>reads the input stream for an Expression and puts it inside 'Hold'.

</dl>
$type$ is one of:
<ul>
<li>Byte</li>
<li>Character</li>
<li>Expression</li>
<li>HoldExpression</li>
<li>Number</li>
<li>Real</li>
<li>Record</li>
<li>String</li>
<li>Word</li>
<li>Byte
<li>Character
<li>Expression
<li>HoldExpression
<li>Number
<li>Real
<li>Record
<li>String
<li>Word
</ul>

## Malformed InputString
Expand Down Expand Up @@ -330,12 +338,24 @@ class Read(Builtin):

## HoldExpression:
>> stream = StringToStream["2+2\\n2+3"];

'Read' with a 'Hold[Expression]' returns the expression it reads unevaluated so it can be later inspected and evaluated:

>> Read[stream, Hold[Expression]]
= Hold[2 + 2]

>> Read[stream, Expression]
= 5
>> Close[stream];

Reading a comment however will return the empy list:
>> stream = StringToStream["(* ::Package:: *)"];

>> Read[stream, Hold[Expression]]
= {}

>> Close[stream];

## Multiple types
>> stream = StringToStream["123 abc"];
>> Read[stream, {Number, Word}]
Expand Down Expand Up @@ -612,12 +632,12 @@ def reader(stream, word_separators, accepted=None):
nextline = next(read_record)
tmp = tmp + "\n" + nextline
except EOFError:
expr = Symbol("EndOfFile")
expr = SymbolEndOfFile
break
except Exception as e:
print(e)

if expr == Symbol("EndOfFile"):
if expr == SymbolEndOfFile:
evaluation.message(
"Read", "readt", tmp, Expression("InputSteam", name, n)
)
Expand All @@ -626,6 +646,9 @@ def reader(stream, word_separators, accepted=None):
if typ == Symbol("HoldExpression"):
expr = Expression("Hold", expr)
result.append(expr)
# else:
# TODO: Supposedly we can't get here
# what code should we put here?

elif typ == Symbol("Number"):
tmp = next(read_number)
Expand Down Expand Up @@ -663,7 +686,7 @@ def reader(stream, word_separators, accepted=None):
result.append(next(read_word))

except EOFError:
return Symbol("EndOfFile")
return SymbolEndOfFile
except UnicodeDecodeError:
evaluation.message("General", "ucdec")

Expand Down Expand Up @@ -1695,7 +1718,7 @@ def apply(self, name, n, typ, evaluation):
try:
result.append(self.readers[t](stream.io))
except struct.error:
result.append(Symbol("EndOfFile"))
result.append(SymbolEndOfFile)

if typ.has_form("List", None):
return Expression("List", *result)
Expand Down Expand Up @@ -2324,7 +2347,7 @@ def apply(self, channel, types, evaluation, options):
if tmp == SymbolFailed:
return

if tmp == Symbol("EndOfFile"):
if tmp == SymbolEndOfFile:
break
result.append(tmp)
return from_python(result)
Expand Down Expand Up @@ -2686,7 +2709,7 @@ def apply(self, name, n, types, m, evaluation, options):
return
for i in range(py_m):
result = super(Skip, self).apply(channel, types, evaluation, options)
if result == Symbol("EndOfFile"):
if result == SymbolEndOfFile:
return result
return SymbolNull

Expand Down