Skip to content
Merged
Show file tree
Hide file tree
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
35 changes: 23 additions & 12 deletions mathics/builtin/files_io/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -1696,13 +1696,15 @@ class Write(Builtin):
def apply(self, channel, expr, evaluation):
"Write[channel_, expr___]"

strm = channel_to_stream(channel)
stream = None
if isinstance(channel, String):
stream = {"stdout": 1, "stderr": 2}.get(channel.value, None)

if strm is None:
return

n = strm.elements[1].get_int_value()
stream = stream_manager.lookup_stream(n)
if stream is None:
strm = channel_to_stream(channel, "w")
if strm is None:
return
stream = stream_manager.lookup_stream(strm.elements[1].get_int_value())

if stream is None or stream.io is None or stream.io.closed:
evaluation.message("General", "openx", channel)
Expand Down Expand Up @@ -1763,6 +1765,9 @@ class WriteString(Builtin):
#> DeleteFile[pathname];
#> Clear[pathname];


If stream is the string "stdout" or "stderr", writes to the system standard output/ standard error channel:
>> WriteString["stdout", "Hola"]
"""

summary_text = "write a sequence of strings to a stream, with no extra newlines"
Expand All @@ -1773,12 +1778,18 @@ class WriteString(Builtin):

def apply(self, channel, expr, evaluation):
"WriteString[channel_, expr___]"
strm = channel_to_stream(channel, "w")

if strm is None:
return

stream = stream_manager.lookup_stream(strm.elements[1].get_int_value())
stream = None
if isinstance(channel, String):
if channel.value == "stdout":
stream = stream_manager.lookup_stream(1)
elif channel.value == "stderr":
stream = stream_manager.lookup_stream(2)

if stream is None:
strm = channel_to_stream(channel, "w")
if strm is None:
return
stream = stream_manager.lookup_stream(strm.elements[1].get_int_value())

if stream is None or stream.io is None or stream.io.closed:
return None
Expand Down
2 changes: 1 addition & 1 deletion mathics/builtin/files_io/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2274,7 +2274,7 @@ def apply_2(self, url, filename, evaluation, **options):
url = url.value
if filename is None:
result = urlsave_tmp(url, None, **options)
elif filename.get_head_name() == "String":
elif isinstance(filename, String):
filename = filename.value
result = urlsave_tmp(url, filename, **options)
else:
Expand Down
5 changes: 3 additions & 2 deletions mathics/core/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ def urlsave_tmp(url, location=None, **kwargs):
with open(location, "wb") as fp:
fp.write(r.content)
result = fp.name
return result
except Exception:
result = None
return result
return None
return None


def path_search(filename: str) -> Tuple[str, bool]:
Expand Down