From 64f4f5c35e13afe8c2b86475f19e39cb1de21312 Mon Sep 17 00:00:00 2001 From: 0xacee <0xacee@users.noreply.github.com> Date: Mon, 21 Sep 2026 16:03:24 +0000 Subject: [PATCH] sort-simple-yaml: read and write files as UTF-8 --- pre_commit_hooks/sort_simple_yaml.py | 2 +- tests/sort_simple_yaml_test.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pre_commit_hooks/sort_simple_yaml.py b/pre_commit_hooks/sort_simple_yaml.py index 65e6b7a6..7b94a402 100644 --- a/pre_commit_hooks/sort_simple_yaml.py +++ b/pre_commit_hooks/sort_simple_yaml.py @@ -107,7 +107,7 @@ def main(argv: Sequence[str] | None = None) -> int: retval = 0 for filename in args.filenames: - with open(filename, 'r+') as f: + with open(filename, 'r+', encoding='utf-8', newline='') as f: lines = [line.rstrip() for line in f.readlines()] new_lines = sort(lines) diff --git a/tests/sort_simple_yaml_test.py b/tests/sort_simple_yaml_test.py index 6cbda857..62fad71b 100644 --- a/tests/sort_simple_yaml_test.py +++ b/tests/sort_simple_yaml_test.py @@ -52,6 +52,18 @@ def test_integration_good_bad_lines(tmpdir, bad_lines, good_lines, retval): assert [line.rstrip() for line in f.readlines()] == good_lines +def test_non_ascii_content_roundtrip(tmpdir): + file_path = os.path.join(str(tmpdir), 'foo.yaml') + + with open(file_path, 'w', encoding='utf-8') as f: + f.write('b: caf\u00e9\n\na: \u2603\n') + + assert main([file_path]) == RETVAL_BAD + + with open(file_path, encoding='utf-8') as f: + assert f.read() == 'a: \u2603\n\nb: caf\u00e9\n' + + def test_parse_header(): lines = ['# some header', '# is here', '', 'this is not a header'] assert parse_block(lines, header=True) == ['# some header', '# is here']