GH-119496: accept UTF-8 BOM in .pth files by ncoghlan · Pull Request #119503 · python/cpython · GitHub
Skip to content

GH-119496: accept UTF-8 BOM in .pth files - #119503

Merged
ncoghlan merged 3 commits into
python:mainfrom
ncoghlan:gh-119496-accept-utf8-bom-in-pth-files
May 24, 2024
Merged

GH-119496: accept UTF-8 BOM in .pth files#119503
ncoghlan merged 3 commits into
python:mainfrom
ncoghlan:gh-119496-accept-utf8-bom-in-pth-files

Conversation

@ncoghlan

@ncoghlan ncoghlan commented May 24, 2024

Copy link
Copy Markdown
Contributor

Out-File -Encoding utf8 in Windows Powershell 5.1 emits
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.

ncoghlan added 2 commits May 24, 2024 22:21
`Out-File -Encoding utf8` in Windows Powershell 5.1 emits
UTF-8 with a BOM maker, which the regular `utf-8` codec
decodes incorrectly. `utf-8-sig` accepts a BOM, but also
works correctly without one.
@ncoghlan

ncoghlan commented May 24, 2024

Copy link
Copy Markdown
Contributor Author

@methane

methane commented May 24, 2024

Copy link
Copy Markdown
Member

PowerShell 5.1 is very old and will be EOL in 2025. I am not sure about adding some support for it is worth enough.
But "as we do in source files" seems good reason. LGTM.

Comment thread Lib/site.py Outdated
@zooba

zooba commented May 24, 2024

Copy link
Copy Markdown
Member

+1 from me. Any time we know we're decoding the start of a file, there's no harm in using utf-8-sig.

I wouldn't use it anywhere we write to a file. It's a bit unfortunate we don't have an encoding that will silently drop a BOM at the start of a buffer without also adding one going the other direction. (Or an encoding that will detect a BOM and switch to UTF-8, or else use 'locale', which is the typical way Windows apps handle this.)

Co-authored-by: Inada Naoki <songofacandy@gmail.com>
@ncoghlan
ncoghlan enabled auto-merge (squash) May 24, 2024 14:08
@ncoghlan
ncoghlan merged commit bf5b646 into python:main May 24, 2024
@miss-islington-app

Copy link
Copy Markdown

Thanks @ncoghlan for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 24, 2024
`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.

(cherry picked from commit bf5b646)

Co-authored-by: Alyssa Coghlan <ncoghlan@gmail.com>
Co-authored-by: Inada Naoki <songofacandy@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 24, 2024
`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.

(cherry picked from commit bf5b646)

Co-authored-by: Alyssa Coghlan <ncoghlan@gmail.com>
Co-authored-by: Inada Naoki <songofacandy@gmail.com>
@bedevere-app

bedevere-app Bot commented May 24, 2024

Copy link
Copy Markdown

GH-119508 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label May 24, 2024
@bedevere-app

bedevere-app Bot commented May 24, 2024

Copy link
Copy Markdown

GH-119509 is a backport of this pull request to the 3.12 branch.

@bedevere-bot

Copy link
Copy Markdown

estyxx pushed a commit to estyxx/cpython that referenced this pull request Jul 17, 2024
`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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants