]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-119496: accept UTF-8 BOM in .pth files (GH-119503)
authorAlyssa Coghlan <ncoghlan@gmail.com>
Fri, 24 May 2024 14:29:19 +0000 (00:29 +1000)
committerGitHub <noreply@github.com>
Fri, 24 May 2024 14:29:19 +0000 (14:29 +0000)
`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>
Lib/site.py

index f1a6d9cf66fdc3ab15c0b5018f1218d036c51c43..7eace190f5ab21863cbca5bf76c2f859a1ccfc01 100644 (file)
@@ -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.