Skip to content

Commit e547752

Browse files
committed
Make importexport black-safe...
We do this by removing the fragile unicode characters (which kind of isn't Python safe either), and test export *outside* of the doctest hidden mechanism which was wrong (if expedient) also.
1 parent da6fca1 commit e547752

File tree

2 files changed

+47
-44
lines changed

2 files changed

+47
-44
lines changed

mathics/builtin/files_io/importexport.py

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ class RegisterImport(Builtin):
10921092

10931093
def apply(self, formatname, function, posts, evaluation, options):
10941094
"""ImportExport`RegisterImport[formatname_String, function_, posts_,
1095-
OptionsPattern[ImportExport`RegisterImport]]"""
1095+
OptionsPattern[ImportExport`RegisterImport]]"""
10961096

10971097
if function.has_form("List", None):
10981098
leaves = function.get_leaves()
@@ -1174,7 +1174,7 @@ class RegisterExport(Builtin):
11741174

11751175
def apply(self, formatname, function, evaluation, options):
11761176
"""ImportExport`RegisterExport[formatname_String, function_,
1177-
OptionsPattern[ImportExport`RegisterExport]]"""
1177+
OptionsPattern[ImportExport`RegisterExport]]"""
11781178
EXPORTERS[formatname.get_string_value()] = (function, options)
11791179

11801180
return Symbol("Null")
@@ -1704,41 +1704,6 @@ class Export(Builtin):
17041704
17051705
## FORMATS
17061706
1707-
## Text
1708-
#> Export["abc.txt", 1 + x + y]
1709-
= abc.txt
1710-
#> FilePrint[%]
1711-
| 1 + x + y
1712-
#> DeleteFile[%%]
1713-
1714-
#> Export["abc.txt", "ä", CharacterEncoding -> "ISOLatin1"];
1715-
#> strm = OpenRead["abc.txt", BinaryFormat -> True];
1716-
#> BinaryRead[strm]
1717-
= 195
1718-
#> Close[strm];
1719-
#> DeleteFile["abc.txt"];
1720-
1721-
#> Export["abc.txt", "ä", CharacterEncoding -> "UTF-8"];
1722-
#> strm = OpenRead["abc.txt", BinaryFormat -> True];
1723-
#> BinaryRead[strm]
1724-
= 195
1725-
#> Close[strm];
1726-
#> DeleteFile["abc.txt"];
1727-
1728-
## CSV
1729-
#> Export["abc.csv", {{1, 2, 3}, {4, 5, 6}}]
1730-
= abc.csv
1731-
#> FilePrint[%]
1732-
| 1,2,3
1733-
| 4,5,6
1734-
#> DeleteFile[%%]
1735-
1736-
## SVG
1737-
#> Export["sine.svg", Plot[Sin[x], {x,0,1}]]
1738-
= sine.svg
1739-
#> FileFormat[%]
1740-
= SVG
1741-
#> DeleteFile[%%]
17421707
"""
17431708

17441709
messages = {
@@ -2193,12 +2158,6 @@ class B64Encode(Builtin):
21932158
= Integrate[f[x], {x, 0, 2}]
21942159
"""
21952160

2196-
# mmatera: please put in pytest conditionally
2197-
# >> System`Convert`B64Dump`B64Encode["∫ f  x"]
2198-
# = 4oirIGYg752MIHg=
2199-
# >> System`Convert`B64Dump`B64Decode[%]
2200-
# = ∫ f  x
2201-
22022161
context = "System`Convert`B64Dump`"
22032162
name = "B64Encode"
22042163

test/test_importexport.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# -*- coding: utf-8 -*-
2-
from .helper import check_evaluation
2+
import tempfile
3+
import os.path as osp
4+
from .helper import check_evaluation, session
35

46

57
def test_import():
@@ -12,3 +14,45 @@ def test_import():
1214
),
1315
):
1416
check_evaluation(str_expr, str_expected, message)
17+
18+
def test_export():
19+
20+
def run_export(temp_dirname: str, short_name: str, file_data:str, character_encoding):
21+
file_path = osp.join(temp_dirname, short_name)
22+
expr = f'Export["{file_path}", {file_data}'
23+
expr += ', CharacterEncoding -> "{character_encoding}"' if character_encoding else ""
24+
expr += "]"
25+
result = session.evaluate(expr)
26+
assert result.to_python() == f'"{file_path}"'
27+
return file_path
28+
29+
def check_data(temp_dirname: str, short_name: str, file_data:str,
30+
character_encoding=None, expected_data=None):
31+
file_path = run_export(temp_dirname, short_name, f'"{file_data}"', character_encoding)
32+
if expected_data is None:
33+
expected_data = file_data
34+
assert open(file_path, "r").read() == expected_data
35+
36+
37+
with tempfile.TemporaryDirectory() as temp_dirname:
38+
# Check exporting text files (file extension ".txt")
39+
check_data(temp_dirname, "add_expr.txt", "1 + x + y")
40+
check_data(temp_dirname, "AAcute.txt", "\u00C1", "ISOLatin1")
41+
check_data(temp_dirname, "AAcute.txt", "\u00C1", "UTF-8")
42+
43+
# Check exporting CSV files (file extension ".csv")
44+
file_path = run_export(temp_dirname, "csv_list.csv", "{{1, 2, 3}, {4, 5, 6}}", None)
45+
assert open(file_path, "r").read() == "1,2,3\n4,5,6"
46+
47+
# Check exporting SVG files (file extension ".svg")
48+
file_path = run_export(temp_dirname, "sine.svg", "Plot[Sin[x], {x,0,1}]", None)
49+
data = open(file_path, "r").read().strip()
50+
assert data.startswith("<svg")
51+
assert data.endswith("</svg>")
52+
53+
# TODO:
54+
# mmatera: please put in pytest conditionally
55+
# >> System`Convert`B64Dump`B64Encode["∫ f  x"]
56+
# = 4oirIGYg752MIHg=
57+
# >> System`Convert`B64Dump`B64Decode[%]
58+
# = ∫ f  x

0 commit comments

Comments
 (0)