Skip to content

Commit 8088091

Browse files
authored
Merge pull request #1343 from mathics/Get-relative-file
Fix a number of small bugs
2 parents 2375269 + af383fd commit 8088091

6 files changed

Lines changed: 26 additions & 6 deletions

File tree

mathics/builtin/files_io/files.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
path_search,
4747
stream_manager,
4848
)
49+
import mathics
4950
from mathics.builtin.base import Builtin, Predefined, BinaryOperator, PrefixOperator
5051
from mathics.builtin.strings import to_python_encoding
5152
from mathics.builtin.base import MessageException
@@ -57,7 +58,7 @@
5758
INPUTFILE_VAR = ""
5859

5960
TMP_DIR = tempfile.gettempdir()
60-
61+
SymbolPath = Symbol("$Path")
6162

6263
def _channel_to_stream(channel, mode="r"):
6364
if isinstance(channel, String):
@@ -2024,6 +2025,7 @@ def check_options(options):
20242025
result = None
20252026
pypath = path.get_string_value()
20262027
definitions = evaluation.definitions
2028+
mathics.core.streams.PATH_VAR = SymbolPath.evaluate(evaluation).to_python(string_quotes=False)
20272029
try:
20282030
if trace_fn:
20292031
trace_fn(pypath)

mathics/builtin/files_io/filesystem.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
valid_context_name,
2828
)
2929

30+
import mathics.core.streams
3031
from mathics.core.streams import (
3132
HOME_DIR,
3233
PATH_VAR,

mathics/builtin/files_io/mainloop.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
An interactive session operates a loop, called the "main loop" in this way:
66
77
<ul>
8-
<li>read input</li>
9-
<li>process input</li>
10-
<li>format and print results</li>
8+
<li>read input
9+
<li>process input
10+
<li>format and print results
1111
<li>repeat
1212
</ul>
1313

mathics/core/expression.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,7 +2617,8 @@ def to_sympy(self, **kwargs):
26172617
return self.real.to_sympy() + sympy.I * self.imag.to_sympy()
26182618

26192619
def to_python(self, *args, **kwargs):
2620-
return complex(self.real.to_python(), self.imag.to_python())
2620+
return complex(self.real.to_python(*args, **kwargs),
2621+
self.imag.to_python(*args, **kwargs))
26212622

26222623
def to_mpmath(self):
26232624
return mpmath.mpc(self.real.to_mpmath(), self.imag.to_mpmath())
@@ -2962,7 +2963,11 @@ def to_sympy(self, **kwargs):
29622963
return None
29632964

29642965
def to_python(self, *args, **kwargs) -> str:
2965-
return '"%s"' % self.value # add quotes to distinguish from Symbols
2966+
if kwargs.get("string_quotes", True):
2967+
return '"%s"' % self.value # add quotes to distinguish from Symbols
2968+
else:
2969+
return self.value
2970+
29662971

29672972
def __hash__(self):
29682973
return hash(("String", self.value))

test/data/fortytwo.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
(* Example for testing issue #1329 *)
2+
42

test/test_files.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# -*- coding: utf-8 -*-
2+
import os.path as osp
23
import sys
34
from .helper import check_evaluation, evaluate
45

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

2123

2224
if sys.platform not in ("win32",):
25+
2326
def test_get_and_put():
2427
temp_filename = evaluate('$TemporaryDirectory<>"/testfile"').to_python()
2528
temp_filename_strip = temp_filename[1:-1]
2629
check_evaluation(f"40! >> {temp_filename_strip}", "Null")
2730
check_evaluation(f"<< {temp_filename_strip}", "40!")
2831
check_evaluation(f"DeleteFile[{temp_filename}]", "Null")
2932

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

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

0 commit comments

Comments
 (0)