From 42fce4714f1c3e0e01e505ae8c3f3bd5282472c6 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] check-yaml: report invalid UTF-8 instead of raising a traceback --- pre_commit_hooks/check_yaml.py | 3 +++ tests/check_yaml_test.py | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/pre_commit_hooks/check_yaml.py b/pre_commit_hooks/check_yaml.py index c94ea716..6524971f 100644 --- a/pre_commit_hooks/check_yaml.py +++ b/pre_commit_hooks/check_yaml.py @@ -65,6 +65,9 @@ def main(argv: Sequence[str] | None = None) -> int: except ruamel.yaml.YAMLError as exc: print(exc) retval = 1 + except UnicodeDecodeError as exc: + print(f'{filename}: invalid UTF-8: {exc}') + retval = 1 return retval diff --git a/tests/check_yaml_test.py b/tests/check_yaml_test.py index 54eb16e8..e59627b5 100644 --- a/tests/check_yaml_test.py +++ b/tests/check_yaml_test.py @@ -17,6 +17,12 @@ def test_main(filename, expected_retval): assert ret == expected_retval +def test_main_non_utf8_file(tmpdir): + f = tmpdir.join('latin1.yaml') + f.write_binary(b'key: caf\xe9\n') + assert main((str(f),)) + + def test_main_allow_multiple_documents(tmpdir): f = tmpdir.join('test.yaml') f.write('---\nfoo\n---\nbar\n')