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
45 changes: 2 additions & 43 deletions mathics/builtin/files_io/importexport.py
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ class RegisterImport(Builtin):

def apply(self, formatname, function, posts, evaluation, options):
"""ImportExport`RegisterImport[formatname_String, function_, posts_,
OptionsPattern[ImportExport`RegisterImport]]"""
OptionsPattern[ImportExport`RegisterImport]]"""

if function.has_form("List", None):
leaves = function.get_leaves()
Expand Down Expand Up @@ -1174,7 +1174,7 @@ class RegisterExport(Builtin):

def apply(self, formatname, function, evaluation, options):
"""ImportExport`RegisterExport[formatname_String, function_,
OptionsPattern[ImportExport`RegisterExport]]"""
OptionsPattern[ImportExport`RegisterExport]]"""
EXPORTERS[formatname.get_string_value()] = (function, options)

return Symbol("Null")
Expand Down Expand Up @@ -1704,41 +1704,6 @@ class Export(Builtin):

## FORMATS

## Text
#> Export["abc.txt", 1 + x + y]
= abc.txt
#> FilePrint[%]
| 1 + x + y
#> DeleteFile[%%]

#> Export["abc.txt", "ä", CharacterEncoding -> "ISOLatin1"];
#> strm = OpenRead["abc.txt", BinaryFormat -> True];
#> BinaryRead[strm]
= 195
#> Close[strm];
#> DeleteFile["abc.txt"];

#> Export["abc.txt", "ä", CharacterEncoding -> "UTF-8"];
#> strm = OpenRead["abc.txt", BinaryFormat -> True];
#> BinaryRead[strm]
= 195
#> Close[strm];
#> DeleteFile["abc.txt"];

## CSV
#> Export["abc.csv", {{1, 2, 3}, {4, 5, 6}}]
= abc.csv
#> FilePrint[%]
| 1,2,3
| 4,5,6
#> DeleteFile[%%]

## SVG
#> Export["sine.svg", Plot[Sin[x], {x,0,1}]]
= sine.svg
#> FileFormat[%]
= SVG
#> DeleteFile[%%]
"""

messages = {
Expand Down Expand Up @@ -2193,12 +2158,6 @@ class B64Encode(Builtin):
= Integrate[f[x], {x, 0, 2}]
"""

# mmatera: please put in pytest conditionally
# >> System`Convert`B64Dump`B64Encode["∫ f  x"]
# = 4oirIGYg752MIHg=
# >> System`Convert`B64Dump`B64Decode[%]
# = ∫ f  x

context = "System`Convert`B64Dump`"
name = "B64Encode"

Expand Down
2 changes: 1 addition & 1 deletion test/test_datentime.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test_datelist():
check_evaluation(str_expr, str_expected)


def test_datelist():
def test_datestring():
for str_expr, str_expected in (
## Check Leading 0s
# (
Expand Down
52 changes: 51 additions & 1 deletion test/test_importexport.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# -*- coding: utf-8 -*-
from .helper import check_evaluation
import tempfile
import os
import os.path as osp
import sys
from .helper import check_evaluation, session


def test_import():
Expand All @@ -12,3 +16,49 @@ def test_import():
),
):
check_evaluation(str_expr, str_expected, message)

def run_export(temp_dirname: str, short_name: str, file_data:str, character_encoding):
file_path = osp.join(temp_dirname, short_name)
expr = fr'Export["{file_path}", {file_data}'
expr += ', CharacterEncoding -> "{character_encoding}"' if character_encoding else ""
expr += "]"
result = session.evaluate(expr)
assert result.to_python(string_quotes=False) == file_path
return file_path

def check_data(temp_dirname: str, short_name: str, file_data:str,
character_encoding=None, expected_data=None):
file_path = run_export(temp_dirname, short_name, fr'"{file_data}"', character_encoding)
if expected_data is None:
expected_data = file_data
assert open(file_path, "r").read() == expected_data


# Github Action Windows CI servers have problems with releasing files using
# a tempfile.TemporaryDirectory context manager.
# Leave out until we figure how to work around this.
if not (os.environ.get("CI", False) or sys.platform in ("win32",)):
def test_export():
with tempfile.TemporaryDirectory(prefix="mtest-") as temp_dirname:
# Check exporting text files (file extension ".txt")
check_data(temp_dirname, "add_expr.txt", "1 + x + y")
check_data(temp_dirname, "AAcute.txt", "\u00C1", "ISOLatin1")
check_data(temp_dirname, "AAcuteUTF.txt", "\u00C1", "UTF-8")

# Check exporting CSV files (file extension ".csv")
file_path = run_export(temp_dirname, "csv_list.csv", "{{1, 2, 3}, {4, 5, 6}}", None)
assert open(file_path, "r").read() == "1,2,3\n4,5,6"

# Check exporting SVG files (file extension ".svg")
file_path = run_export(temp_dirname, "sine.svg", "Plot[Sin[x], {x,0,1}]", None)
data = open(file_path, "r").read().strip()
if not os.environ.get("CI", None):
assert data.startswith("<svg")
assert data.endswith("</svg>")

# TODO:
# mmatera: please put in pytest conditionally
# >> System`Convert`B64Dump`B64Encode["∫ f  x"]
# = 4oirIGYg752MIHg=
# >> System`Convert`B64Dump`B64Decode[%]
# = ∫ f  x