From be7bc309902ec3a4ec1142c437776d8cec2c1c3e Mon Sep 17 00:00:00 2001 From: rocky Date: Sat, 1 May 2021 20:19:11 -0400 Subject: [PATCH 1/3] Fix some bugs bugs.. * Synchronize PATH_VAR when $Path at least before mathics_open is called. * Add to_python option string_quotes: boolean. If False don't add extra surrounding quotes * Remove extra space in list in Mainloop doc --- mathics/builtin/files_io/files.py | 4 +++- mathics/builtin/files_io/filesystem.py | 1 + mathics/builtin/files_io/mainloop.py | 6 +++--- mathics/core/expression.py | 6 +++++- test/test_files.py | 10 ++++++++++ 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/mathics/builtin/files_io/files.py b/mathics/builtin/files_io/files.py index 7f6919e854..5bbd786660 100644 --- a/mathics/builtin/files_io/files.py +++ b/mathics/builtin/files_io/files.py @@ -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 @@ -57,7 +58,7 @@ INPUTFILE_VAR = "" TMP_DIR = tempfile.gettempdir() - +SymbolPath = Symbol("$Path") def _channel_to_stream(channel, mode="r"): if isinstance(channel, String): @@ -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) diff --git a/mathics/builtin/files_io/filesystem.py b/mathics/builtin/files_io/filesystem.py index 6bcde144d9..0c5131bf5a 100644 --- a/mathics/builtin/files_io/filesystem.py +++ b/mathics/builtin/files_io/filesystem.py @@ -27,6 +27,7 @@ valid_context_name, ) +import mathics.core.streams from mathics.core.streams import ( HOME_DIR, PATH_VAR, diff --git a/mathics/builtin/files_io/mainloop.py b/mathics/builtin/files_io/mainloop.py index 645485ef0f..70a7ebab25 100644 --- a/mathics/builtin/files_io/mainloop.py +++ b/mathics/builtin/files_io/mainloop.py @@ -5,9 +5,9 @@ An interactive session operates a loop, called the "main loop" in this way: diff --git a/mathics/core/expression.py b/mathics/core/expression.py index a83a1ef268..ee0e57a708 100644 --- a/mathics/core/expression.py +++ b/mathics/core/expression.py @@ -2962,7 +2962,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): + return '"%s"' % self.value # add quotes to distinguish from Symbols + else: + return self.value + def __hash__(self): return hash(("String", self.value)) diff --git a/test/test_files.py b/test/test_files.py index 014a6c2220..15e763cbd7 100644 --- a/test/test_files.py +++ b/test/test_files.py @@ -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}"]]' @@ -20,6 +22,7 @@ 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] @@ -27,6 +30,13 @@ def test_get_and_put(): 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(): From 8056f42aa49733568d3eca7a3dd911353e9cb589 Mon Sep 17 00:00:00 2001 From: rocky Date: Sun, 2 May 2021 01:35:44 -0400 Subject: [PATCH 2/3] to_python: pass *args, **kwargs --- mathics/core/expression.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mathics/core/expression.py b/mathics/core/expression.py index ee0e57a708..623c54bab3 100644 --- a/mathics/core/expression.py +++ b/mathics/core/expression.py @@ -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()) From af383fd2b7b98f3d3d2640c2362cde1825763df1 Mon Sep 17 00:00:00 2001 From: rocky Date: Sun, 2 May 2021 02:11:46 -0400 Subject: [PATCH 3/3] Add test file --- test/data/fortytwo.m | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 test/data/fortytwo.m diff --git a/test/data/fortytwo.m b/test/data/fortytwo.m new file mode 100644 index 0000000000..2971d19a3b --- /dev/null +++ b/test/data/fortytwo.m @@ -0,0 +1,2 @@ +(* Example for testing issue #1329 *) +42