Skip to content

Commit f35d15e

Browse files
authored
Merge pull request #1449 from mathics/black-importexport
Make importexport black-safe...
2 parents da6fca1 + dfb84f8 commit f35d15e

3 files changed

Lines changed: 54 additions & 45 deletions

File tree

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_datentime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def test_datelist():
5454
check_evaluation(str_expr, str_expected)
5555

5656

57-
def test_datelist():
57+
def test_datestring():
5858
for str_expr, str_expected in (
5959
## Check Leading 0s
6060
# (

test/test_importexport.py

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

48

59
def test_import():
@@ -12,3 +16,49 @@ def test_import():
1216
),
1317
):
1418
check_evaluation(str_expr, str_expected, message)
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 = fr'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(string_quotes=False) == 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, fr'"{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+
# Github Action Windows CI servers have problems with releasing files using
38+
# a tempfile.TemporaryDirectory context manager.
39+
# Leave out until we figure how to work around this.
40+
if not (os.environ.get("CI", False) or sys.platform in ("win32",)):
41+
def test_export():
42+
with tempfile.TemporaryDirectory(prefix="mtest-") as temp_dirname:
43+
# Check exporting text files (file extension ".txt")
44+
check_data(temp_dirname, "add_expr.txt", "1 + x + y")
45+
check_data(temp_dirname, "AAcute.txt", "\u00C1", "ISOLatin1")
46+
check_data(temp_dirname, "AAcuteUTF.txt", "\u00C1", "UTF-8")
47+
48+
# Check exporting CSV files (file extension ".csv")
49+
file_path = run_export(temp_dirname, "csv_list.csv", "{{1, 2, 3}, {4, 5, 6}}", None)
50+
assert open(file_path, "r").read() == "1,2,3\n4,5,6"
51+
52+
# Check exporting SVG files (file extension ".svg")
53+
file_path = run_export(temp_dirname, "sine.svg", "Plot[Sin[x], {x,0,1}]", None)
54+
data = open(file_path, "r").read().strip()
55+
if not os.environ.get("CI", None):
56+
assert data.startswith("<svg")
57+
assert data.endswith("</svg>")
58+
59+
# TODO:
60+
# mmatera: please put in pytest conditionally
61+
# >> System`Convert`B64Dump`B64Encode["∫ f  x"]
62+
# = 4oirIGYg752MIHg=
63+
# >> System`Convert`B64Dump`B64Decode[%]
64+
# = ∫ f  x

0 commit comments

Comments
 (0)