Skip to content

Fallback to utf-8 or iso-8859-1 if decoding using chardet-detected encoding fails #3985

Description

@vries

Consider file gdb/testsuite/gdb.ada/non-ascii-utf-8/pack.adb from the binutils-gdb repo.

This works without problems:

$ codespell gdb/testsuite/gdb.ada/non-ascii-utf-8/pack.adb
$ 

which means utf-8 decoding is used.

However, with --hard-encoding-detection we get:

$ codespell --hard-encoding-detection gdb/testsuite/gdb.ada/non-ascii-utf-8/pack.adb
Traceback (most recent call last):
  File "/home/vries/codespell/python-3.9-venv/bin/codespell", line 6, in <module>
    sys.exit(_script_main())
  File "/home/vries/codespell/codespell.git/codespell_lib/_codespell.py", line 1294, in _script_main
    return main(*sys.argv[1:])
  File "/home/vries/codespell/codespell.git/codespell_lib/_codespell.py", line 1534, in main
    bad_count += parse_file(
  File "/home/vries/codespell/codespell.git/codespell_lib/_codespell.py", line 1217, in parse_file
    fragments, encoding = file_opener.open(filename)
  File "/home/vries/codespell/codespell.git/codespell_lib/_codespell.py", line 250, in open
    return self.open_with_chardet(filename)
  File "/home/vries/codespell/codespell.git/codespell_lib/_codespell.py", line 277, in open_with_chardet
    lines = self.get_lines(f)
  File "/home/vries/codespell/codespell.git/codespell_lib/_codespell.py", line 328, in get_lines
    fragments.append((False, line_number, f.readlines()))
  File "/usr/lib64/python3.9/encodings/cp1254.py", line 23, in decode
UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 943: character maps to <undefined>

What happens is that chardet detects CP1254 (aka Windows-1254) encoding, but decoding using that encoding fails.

When not using --hard-encoding-detection, we first try utf-8, and if that doesn't work, warn and try iso-8859-1.

It seems logical to extend the fallback handling to --hard-encoding-detection:

$ codespell --hard-encoding-detection gdb/testsuite/gdb.ada/non-ascii-utf-8/pack.adb
WARNING: Cannot decode file using encoding "Windows-1254": gdb/testsuite/gdb.ada/non-ascii-utf-8/pack.adb
WARNING: Trying next encoding "utf-8"
$

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions