Skip to content

Commit 9f9c5b8

Browse files
authored
Merge pull request #606 from Mathics3/fix_URLSave
fix a bug that makes that URLSave always fails
2 parents 3528e0d + b0b52b5 commit 9f9c5b8

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

mathics/builtin/files_io/files.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,13 +1696,15 @@ class Write(Builtin):
16961696
def apply(self, channel, expr, evaluation):
16971697
"Write[channel_, expr___]"
16981698

1699-
strm = channel_to_stream(channel)
1699+
stream = None
1700+
if isinstance(channel, String):
1701+
stream = {"stdout": 1, "stderr": 2}.get(channel.value, None)
17001702

1701-
if strm is None:
1702-
return
1703-
1704-
n = strm.elements[1].get_int_value()
1705-
stream = stream_manager.lookup_stream(n)
1703+
if stream is None:
1704+
strm = channel_to_stream(channel, "w")
1705+
if strm is None:
1706+
return
1707+
stream = stream_manager.lookup_stream(strm.elements[1].get_int_value())
17061708

17071709
if stream is None or stream.io is None or stream.io.closed:
17081710
evaluation.message("General", "openx", channel)
@@ -1763,6 +1765,9 @@ class WriteString(Builtin):
17631765
#> DeleteFile[pathname];
17641766
#> Clear[pathname];
17651767
1768+
1769+
If stream is the string "stdout" or "stderr", writes to the system standard output/ standard error channel:
1770+
>> WriteString["stdout", "Hola"]
17661771
"""
17671772

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

17741779
def apply(self, channel, expr, evaluation):
17751780
"WriteString[channel_, expr___]"
1776-
strm = channel_to_stream(channel, "w")
1777-
1778-
if strm is None:
1779-
return
1780-
1781-
stream = stream_manager.lookup_stream(strm.elements[1].get_int_value())
1781+
stream = None
1782+
if isinstance(channel, String):
1783+
if channel.value == "stdout":
1784+
stream = stream_manager.lookup_stream(1)
1785+
elif channel.value == "stderr":
1786+
stream = stream_manager.lookup_stream(2)
1787+
1788+
if stream is None:
1789+
strm = channel_to_stream(channel, "w")
1790+
if strm is None:
1791+
return
1792+
stream = stream_manager.lookup_stream(strm.elements[1].get_int_value())
17821793

17831794
if stream is None or stream.io is None or stream.io.closed:
17841795
return None

mathics/builtin/files_io/filesystem.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2274,7 +2274,7 @@ def apply_2(self, url, filename, evaluation, **options):
22742274
url = url.value
22752275
if filename is None:
22762276
result = urlsave_tmp(url, None, **options)
2277-
elif filename.get_head_name() == "String":
2277+
elif isinstance(filename, String):
22782278
filename = filename.value
22792279
result = urlsave_tmp(url, filename, **options)
22802280
else:

mathics/core/streams.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ def urlsave_tmp(url, location=None, **kwargs):
4343
with open(location, "wb") as fp:
4444
fp.write(r.content)
4545
result = fp.name
46+
return result
4647
except Exception:
47-
result = None
48-
return result
48+
return None
49+
return None
4950

5051

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

0 commit comments

Comments
 (0)