Hacktoberfest 2026:維護者為十月標記出來的 issue,仍然開放、適合新手。 瀏覽 Hacktoberfest issue

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

未關閉
#3,985 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

@ekanshul 已經在處理了。

開始於 2026年9月20日。

  • #4030 來自 @ekanshul —— 未關閉

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
25/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
停滯
技術堆疊
python
領域
tooling

研究方向

Start in codespell_lib/_codespell.py, especially open_with_chardet and get_lines, which appear in the traceback. Check the existing tests for hard encoding detection and run the relevant tests. Done means a decoding failure with the detected encoding falls back to utf-8 or iso-8859-1 instead of crashing; an open pull request is already linked.

由索引模型根據 Issue 內容生成。

描述

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"
$
主要語言
Python
星號
2.4k
分支
531
平均合併
15 小時 55 分鐘
30 天內合併 PR
17

環境準備

在 Codespaces 中開啟

在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 沒有貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

codespell-project/codespell 的其他 Issue

查看 codespell-project/codespell 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。