From 9806737c97c29ff27e4d8b75c19ff80e8dd2a040 Mon Sep 17 00:00:00 2001 From: ANSHUL SINGH <72524975+ekanshul@users.noreply.github.com> Date: Sun, 20 Sep 2026 17:35:16 +0000 Subject: [PATCH 1/4] Fall back to utf-8 when chardet's encoding cannot decode a file --hard-encoding-detection crashed if chardet picked an encoding that failed while reading, because only open() was wrapped and there was no fallback. Catch UnicodeDecodeError and LookupError from the chardet path, warn, and reuse the utf-8 / iso-8859-1 opener. Fixes #3985 --- codespell_lib/_codespell.py | 31 ++++++++++++++--------------- codespell_lib/tests/test_basic.py | 33 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py index 1ec09fdf8f..ac85fb04e3 100644 --- a/codespell_lib/_codespell.py +++ b/codespell_lib/_codespell.py @@ -247,7 +247,16 @@ def init_chardet(self) -> None: def open(self, filename: str) -> tuple[list[tuple[bool, int, list[str]]], str]: if self.use_chardet: - return self.open_with_chardet(filename) + try: + return self.open_with_chardet(filename) + except (UnicodeDecodeError, LookupError): + encoding = getattr(self, "detected_encoding", None) + if not self.quiet_level & QuietLevels.ENCODING: + print( + f'WARNING: Cannot decode file using encoding "{encoding}": ' + f"{filename}", + file=sys.stderr, + ) return self.open_with_internal(filename) def open_with_chardet( @@ -261,23 +270,13 @@ def open_with_chardet( break self.encdetector.close() encoding = self.encdetector.result["encoding"] + self.detected_encoding = encoding + if not encoding: + raise LookupError(encoding) - try: - f = open(filename, encoding=encoding, newline="") - except UnicodeDecodeError: - print(f"ERROR: Could not detect encoding: {filename}", file=sys.stderr) - raise - except LookupError: - print( - f"ERROR: Don't know how to handle encoding {encoding}: {filename}", - file=sys.stderr, - ) - raise - else: + with open(filename, encoding=encoding, newline="") as f: lines = self.get_lines(f) - f.close() - - return lines, f.encoding + return lines, f.encoding def open_with_internal( self, filename: str diff --git a/codespell_lib/tests/test_basic.py b/codespell_lib/tests/test_basic.py index 0127f57013..01ab604390 100644 --- a/codespell_lib/tests/test_basic.py +++ b/codespell_lib/tests/test_basic.py @@ -20,6 +20,7 @@ EX_DATAERR, EX_OK, EX_USAGE, + FileOpener, _builtin_dictionaries, uri_regex_def, ) @@ -693,6 +694,38 @@ def test_unknown_encoding_chardet( assert cs.main("--hard-encoding-detection", fname) == 0 +def test_chardet_falls_back_when_detected_encoding_fails( + tmp_path: Path, + capsys: pytest.CaptureFixture[str], +) -> None: + """If chardet picks an encoding that cannot decode the file, try utf-8.""" + fname = tmp_path / "tmp" + fname.write_bytes("naïve\nspeling\n".encode()) + opener = FileOpener(False, 0, None) + opener.use_chardet = True + + class FakeDetector: + done = True + result = {"encoding": "ascii"} + + def reset(self) -> None: + return None + + def feed(self, _line: bytes) -> None: + return None + + def close(self) -> None: + return None + + opener.encdetector = FakeDetector() + lines, encoding = opener.open(str(fname)) + assert encoding == "utf-8" + assert any("speling" in "".join(chunk) for _ignored, _lineno, chunk in lines) + captured = capsys.readouterr() + assert "ascii" in captured.err + assert "WARNING" in captured.err + + def test_ignore( tmp_path: Path, capsys: pytest.CaptureFixture[str], From f6b5e8185fd22d2b036638c7f0d48a3a2a75e70d Mon Sep 17 00:00:00 2001 From: ANSHUL SINGH <72524975+ekanshul@users.noreply.github.com> Date: Tue, 24 Nov 2026 16:29:37 +0530 Subject: [PATCH 2/4] Use a mock detector in the encoding fallback regression test --- codespell_lib/tests/test_basic.py | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/codespell_lib/tests/test_basic.py b/codespell_lib/tests/test_basic.py index 01ab604390..a3dccd244c 100644 --- a/codespell_lib/tests/test_basic.py +++ b/codespell_lib/tests/test_basic.py @@ -700,24 +700,11 @@ def test_chardet_falls_back_when_detected_encoding_fails( ) -> None: """If chardet picks an encoding that cannot decode the file, try utf-8.""" fname = tmp_path / "tmp" - fname.write_bytes("naïve\nspeling\n".encode()) + fname.write_bytes("na\u00efve\nspeling\n".encode()) opener = FileOpener(False, 0, None) opener.use_chardet = True - class FakeDetector: - done = True - result = {"encoding": "ascii"} - - def reset(self) -> None: - return None - - def feed(self, _line: bytes) -> None: - return None - - def close(self) -> None: - return None - - opener.encdetector = FakeDetector() + opener.encdetector = mock.Mock(done=True, result={"encoding": "ascii"}) lines, encoding = opener.open(str(fname)) assert encoding == "utf-8" assert any("speling" in "".join(chunk) for _ignored, _lineno, chunk in lines) From 6ebfc6e41b9872c37acc768884d7c2a495ed3023 Mon Sep 17 00:00:00 2001 From: ANSHUL SINGH <72524975+ekanshul@users.noreply.github.com> Date: Tue, 1 Dec 2026 08:51:40 +0530 Subject: [PATCH 3/4] Cover missing codecs and quiet encoding fallback --- codespell_lib/tests/test_basic.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/codespell_lib/tests/test_basic.py b/codespell_lib/tests/test_basic.py index a3dccd244c..8549646990 100644 --- a/codespell_lib/tests/test_basic.py +++ b/codespell_lib/tests/test_basic.py @@ -694,23 +694,30 @@ def test_unknown_encoding_chardet( assert cs.main("--hard-encoding-detection", fname) == 0 +@pytest.mark.parametrize("detected_encoding", ["ascii", None, "unknown-codec"]) +@pytest.mark.parametrize("quiet_level", [0, 1]) def test_chardet_falls_back_when_detected_encoding_fails( + detected_encoding: Optional[str], + quiet_level: int, tmp_path: Path, capsys: pytest.CaptureFixture[str], ) -> None: """If chardet picks an encoding that cannot decode the file, try utf-8.""" fname = tmp_path / "tmp" fname.write_bytes("na\u00efve\nspeling\n".encode()) - opener = FileOpener(False, 0, None) + opener = FileOpener(False, quiet_level, None) opener.use_chardet = True - opener.encdetector = mock.Mock(done=True, result={"encoding": "ascii"}) + opener.encdetector = mock.Mock(done=True, result={"encoding": detected_encoding}) lines, encoding = opener.open(str(fname)) assert encoding == "utf-8" assert any("speling" in "".join(chunk) for _ignored, _lineno, chunk in lines) captured = capsys.readouterr() - assert "ascii" in captured.err - assert "WARNING" in captured.err + if quiet_level: + assert not captured.err + else: + assert str(detected_encoding) in captured.err + assert "WARNING" in captured.err def test_ignore( From a4de5ee6e48408ef775bd2dec4688fb562e1e327 Mon Sep 17 00:00:00 2001 From: ANSHUL SINGH <72524975+ekanshul@users.noreply.github.com> Date: Tue, 1 Dec 2026 16:59:50 +0530 Subject: [PATCH 4/4] Keep detected encoding local and distinguish fallback warnings --- codespell_lib/_codespell.py | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py index b6c81c9ca1..e94581974f 100644 --- a/codespell_lib/_codespell.py +++ b/codespell_lib/_codespell.py @@ -285,16 +285,7 @@ def init_chardet(self) -> None: def open(self, filename: str) -> tuple[list[tuple[bool, int, list[str]]], str]: if self.use_chardet: - try: - return self.open_with_chardet(filename) - except (UnicodeDecodeError, LookupError): - encoding = getattr(self, "detected_encoding", None) - if not self.quiet_level & QuietLevels.ENCODING: - print( - f'WARNING: Cannot decode file using encoding "{encoding}": ' - f"{filename}", - file=sys.stderr, - ) + return self.open_with_chardet(filename) return self.open_with_internal(filename) def open_with_chardet( @@ -308,13 +299,24 @@ def open_with_chardet( break self.encdetector.close() encoding = self.encdetector.result["encoding"] - self.detected_encoding = encoding - if not encoding: - raise LookupError(encoding) - - with open(filename, encoding=encoding, newline="") as f: - lines = self.get_lines(f) - return lines, f.encoding + try: + with open(filename, encoding=encoding or "", newline="") as f: + return self.get_lines(f), f.encoding + except UnicodeDecodeError: + if not self.quiet_level & QuietLevels.ENCODING: + print( + f'WARNING: Cannot decode file using encoding "{encoding}": ' + f"{filename}", + file=sys.stderr, + ) + except LookupError: + if not self.quiet_level & QuietLevels.ENCODING: + print( + f"WARNING: Don't know how to handle encoding {encoding}: " + f"{filename}", + file=sys.stderr, + ) + return self.open_with_internal(filename) def open_with_internal( self, filename: str