From 9fd5b161960c30c4b1646ab0b7481eb78f0487a4 Mon Sep 17 00:00:00 2001 From: Max Rozentsveyg Date: Sat, 16 May 2020 20:49:10 -0400 Subject: [PATCH] Specify encoding in open() call --- pre_commit_hooks/check_toml.py | 2 +- tests/check_toml_test.py | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/pre_commit_hooks/check_toml.py b/pre_commit_hooks/check_toml.py index 51a1f15e..9d9f4549 100644 --- a/pre_commit_hooks/check_toml.py +++ b/pre_commit_hooks/check_toml.py @@ -13,7 +13,7 @@ def main(argv: Optional[Sequence[str]] = None) -> int: retval = 0 for filename in args.filenames: try: - with open(filename) as f: + with open(filename, encoding='UTF-8') as f: toml.load(f) except toml.TomlDecodeError as exc: print(f'{filename}: {exc}') diff --git a/tests/check_toml_test.py b/tests/check_toml_test.py index 9f186d15..4e52f07e 100644 --- a/tests/check_toml_test.py +++ b/tests/check_toml_test.py @@ -1,7 +1,7 @@ from pre_commit_hooks.check_toml import main -def test_toml_good(tmpdir): +def test_toml_bad(tmpdir): filename = tmpdir.join('f') filename.write(""" key = # INVALID @@ -12,7 +12,7 @@ def test_toml_good(tmpdir): assert ret == 1 -def test_toml_bad(tmpdir): +def test_toml_good(tmpdir): filename = tmpdir.join('f') filename.write( """ @@ -27,3 +27,10 @@ def test_toml_bad(tmpdir): ) ret = main((filename.strpath,)) assert ret == 0 + + +def test_toml_good_unicode(tmpdir): + filename = tmpdir.join('f') + filename.write_binary('letter = "\N{SNOWMAN}"'.encode()) + ret = main((filename.strpath,)) + assert ret == 0