Skip to content

Commit

Permalink
pythonGH-119496: accept UTF-8 BOM in .pth files (pythonGH-119503)
Browse files Browse the repository at this point in the history
`Out-File -Encoding utf8` and similar commands in Windows Powershell 5.1 emit
UTF-8 with a BOM marker, which the regular `utf-8` codec decodes incorrectly.

`utf-8-sig` accepts a BOM, but also works correctly without one.

This change also makes .pth files match the way Python source files are handled.

Co-authored-by: Inada Naoki <songofacandy@gmail.com>
  • Loading branch information
ncoghlan and methane authored May 24, 2024
1 parent 92fab33 commit bf5b646
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Lib/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ def addpackage(sitedir, name, known_paths):
return

try:
pth_content = pth_content.decode()
# Accept BOM markers in .pth files as we do in source files
# (Windows PowerShell 5.1 makes it hard to emit UTF-8 files without a BOM)
pth_content = pth_content.decode("utf-8-sig")
except UnicodeDecodeError:
# Fallback to locale encoding for backward compatibility.
# We will deprecate this fallback in the future.
Expand Down

0 comments on commit bf5b646

Please sign in to comment.