Skip to content

Commit

Permalink
Use UTF-8 encoding when reading and writing TOML files (#28)
Browse files Browse the repository at this point in the history
* Use UTF-8 encoding when reading and writing TOML files

* Read and write as UTF-8 in the tests.
  • Loading branch information
domdfcoding authored Jul 11, 2021
1 parent 143ee09 commit a9ac770
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
4 changes: 2 additions & 2 deletions rtoml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def load(toml: Union[str, Path, TextIO]) -> Dict[str, Any]:
`Path` or file object from `open()`.
"""
if isinstance(toml, Path):
toml = toml.read_text()
toml = toml.read_text(encoding='UTF-8')
elif isinstance(toml, (TextIOBase, TextIO)):
toml = toml.read()

Expand Down Expand Up @@ -56,6 +56,6 @@ def dump(obj: Any, file: Union[Path, TextIO], *, pretty: bool = False) -> int:
"""
s = dumps(obj, pretty=pretty)
if isinstance(file, Path):
return file.write_text(s)
return file.write_text(s, encoding='UTF-8')
else:
return file.write(s)
16 changes: 13 additions & 3 deletions tests/test_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# order changed to avoid https://github.com/alexcrichton/toml-rs/issues/142
({'x': {'a': 1}, 'y': 4}, 'y = 4\n\n[x]\na = 1\n'),
((1, 2, 3), '[1, 2, 3]'),
({'emoji': '😷'}, 'emoji = "😷"\n'),
({'polish': 'Witaj świecie'}, 'polish = "Witaj świecie"\n'),
],
)
def test_dumps(input_obj, output_toml):
Expand All @@ -36,10 +38,18 @@ def test_dumps_pretty(input_obj, output_toml):
assert rtoml.dumps(input_obj, pretty=True) == output_toml


def test_dump_path(tmp_path):
@pytest.mark.parametrize(
'input_obj,output_toml,size',
[
({'foo': 'bar'}, 'foo = "bar"\n', 12),
({'emoji': '😷'}, 'emoji = "😷"\n', 12),
({'polish': 'Witaj świecie'}, 'polish = "Witaj świecie"\n', 25),
],
)
def test_dump_path(tmp_path, input_obj, output_toml, size):
p = tmp_path / 'test.toml'
assert rtoml.dump({'foo': 'bar'}, p) == 12
assert p.read_text() == 'foo = "bar"\n'
assert rtoml.dump(input_obj, p) == size
assert p.read_text(encoding='UTF-8') == output_toml


def test_dump_file(tmp_path):
Expand Down
14 changes: 11 additions & 3 deletions tests/test_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,18 @@ def test_load_str():
assert rtoml.load('foo = "bar"') == {'foo': 'bar'}


def test_load_path(tmp_path):
@pytest.mark.parametrize(
'input_toml,output_obj',
[
('foo = "bar"', {'foo': 'bar'}),
('emoji = "😷"', {'emoji': '😷'}),
('polish = "Witaj świecie"', {'polish': 'Witaj świecie'}),
],
)
def test_load_path(tmp_path, input_toml, output_obj):
p = tmp_path / 'test.toml'
p.write_text('foo = "bar"')
assert rtoml.load(p) == {'foo': 'bar'}
p.write_text(input_toml, encoding='UTF-8')
assert rtoml.load(p) == output_obj


def test_load_file(tmp_path):
Expand Down

0 comments on commit a9ac770

Please sign in to comment.