python / python/cpython

Improve handling of UTF-16-LE encoded .pth files

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

還沒有人認領這個 Issue。

type-feature
主要語言
Python
星號
77.2k
分支
35.9k
PR 合併指標
PR 指標待擷取

描述

Feature or enhancement

I just finished an extended Windows bug hunt that I eventually tracked down to a .pth file being encoded in UTF-16-LE rather than an ASCII-compatible encoding.

I only figured it out by turning off frozen modules and hacking site.py to dump the output of .pth files as it tried to process them (at which point I checked the .pth file encoding in VSCode and sure enough, UTF-16-LE was down in the corner of the file window).

I hit the bug by porting a Linux shell script to Windows PowerShell, not thinking about the fact that | Out-File on Windows Powershell 5.1 defaults to UTF-16-LE (newer versions of PowerShell that aren't the ones baked into the OS default to UTF-8 without a BOM).

Given the inevitable presence of NULLs in a UTF-16-LE file, while there shouldn't be any in a UTF-8 or locale encoding file, it seems to me we should be able to handle such situations more gracefully (at the very least logging an error if NULL bytes are present in the file, but potentially even just making UTF-16-LE encoded .pth files straight up work by checking for NULL bytes, and using UTF-16-LE instead of UTF-8 and the locale encoding if we find one, or else trying UTF-16-LE before trying the locale encoding on Windows)

(This is somewhat related to #77102)

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs
  • gh-119503
  • gh-119508
  • gh-119509

貢獻指南

開啟貢獻指南

從這裡開始

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

研究方向

首先閱讀 site.py 對 .pth 檔案的處理方式,並使用 PowerShell 5.1 Out-File 產生的 UTF-16-LE 檔案重現 Windows 情況。在決定行為之前,檢視連結的 PR gh-119503、gh-119508 和 gh-119509。完成的標準是能妥善處理 UTF-16-LE .pth 檔案:要麼正常運作,要麼回報有用的錯誤。

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

評估

技術堆疊
python
領域
operating-systems
Issue 類型
功能
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
25/100

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

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