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
4 changes: 3 additions & 1 deletion mathics/builtin/files_io/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
path_search,
stream_manager,
)
import mathics
from mathics.builtin.base import Builtin, Predefined, BinaryOperator, PrefixOperator
from mathics.builtin.strings import to_python_encoding
from mathics.builtin.base import MessageException
Expand All @@ -57,7 +58,7 @@
INPUTFILE_VAR = ""

TMP_DIR = tempfile.gettempdir()

SymbolPath = Symbol("$Path")

def _channel_to_stream(channel, mode="r"):
if isinstance(channel, String):
Expand Down Expand Up @@ -2024,6 +2025,7 @@ def check_options(options):
result = None
pypath = path.get_string_value()
definitions = evaluation.definitions
mathics.core.streams.PATH_VAR = SymbolPath.evaluate(evaluation).to_python(string_quotes=False)
try:
if trace_fn:
trace_fn(pypath)
Expand Down
1 change: 1 addition & 0 deletions mathics/builtin/files_io/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
valid_context_name,
)

import mathics.core.streams
from mathics.core.streams import (
HOME_DIR,
PATH_VAR,
Expand Down
6 changes: 3 additions & 3 deletions mathics/builtin/files_io/mainloop.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
An interactive session operates a loop, called the "main loop" in this way:

<ul>
<li>read input</li>
<li>process input</li>
<li>format and print results</li>
<li>read input
<li>process input
<li>format and print results
<li>repeat
</ul>

Expand Down
9 changes: 7 additions & 2 deletions mathics/core/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -2617,7 +2617,8 @@ def to_sympy(self, **kwargs):
return self.real.to_sympy() + sympy.I * self.imag.to_sympy()

def to_python(self, *args, **kwargs):
return complex(self.real.to_python(), self.imag.to_python())
return complex(self.real.to_python(*args, **kwargs),
self.imag.to_python(*args, **kwargs))

def to_mpmath(self):
return mpmath.mpc(self.real.to_mpmath(), self.imag.to_mpmath())
Expand Down Expand Up @@ -2962,7 +2963,11 @@ def to_sympy(self, **kwargs):
return None

def to_python(self, *args, **kwargs) -> str:
return '"%s"' % self.value # add quotes to distinguish from Symbols
if kwargs.get("string_quotes", True):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just in case, I shouldn't be useful to have string_quotes with True as default value, for backward compatibility?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand. If you haven't set "string_quotes", it defaults to True. Almost all of the code currently is this way: no parameter is set.

If you pass this parameter, then you set it accordingly.

I had thought about a better name, and at some in the past called it something else, but I can't remember the name.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right! Discard my comment, please.

return '"%s"' % self.value # add quotes to distinguish from Symbols
else:
return self.value


def __hash__(self):
return hash(("String", self.value))
Expand Down
2 changes: 2 additions & 0 deletions test/data/fortytwo.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(* Example for testing issue #1329 *)
42
10 changes: 10 additions & 0 deletions test/test_files.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# -*- coding: utf-8 -*-
import os.path as osp
import sys
from .helper import check_evaluation, evaluate


def test_compress():
for text in ("", "abc", " "):
str_expr = f'Uncompress[Compress["{text}"]]'
Expand All @@ -20,13 +22,21 @@ def test_unprotected():


if sys.platform not in ("win32",):

def test_get_and_put():
temp_filename = evaluate('$TemporaryDirectory<>"/testfile"').to_python()
temp_filename_strip = temp_filename[1:-1]
check_evaluation(f"40! >> {temp_filename_strip}", "Null")
check_evaluation(f"<< {temp_filename_strip}", "40!")
check_evaluation(f"DeleteFile[{temp_filename}]", "Null")

def test_get_path_search():
# Check that AppendTo[$Path] works in conjunction with Get[]
dirname = osp.join(osp.dirname(osp.abspath(__file__)), "data")
evaled = evaluate(f"""AppendTo[$Path, "{dirname}"]""")
assert evaled.has_form("List", 1, None)
check_evaluation('Get["fortytwo.m"]', "42")


# I do not know what is it supposed to test with this...
# def test_Inputget_and_put():
Expand Down