8000 gh-77102: site: try utf-8 and locale encoding when reading .pth file … · python/cpython@2a58923 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2a58923

Browse files
authored
gh-77102: site: try utf-8 and locale encoding when reading .pth file (GH-117802)
(cherry picked from commit 6dc661b)
1 parent 44eab29 commit 2a58923

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

Lib/site.py

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -179,35 +179,44 @@ def addpackage(sitedir, name, known_paths):
179179
return
180180
_trace(f"Processing .pth file: {fullname!r}")
181181
try:
182-
# locale encoding is not ideal especially on Windows. But we have used
183-
# it for a long time. setuptools uses the locale encoding too.
184-
f = io.TextIOWrapper(io.open_code(fullname), encoding="locale")
182+
with io.open_code(fullname) as f:
183+
pth_content = f.read()
185184
except OSError:
186185
return
187-
with f:
188-
for n, line in enumerate(f):
189-
if line.startswith("#"):
190-
continue
191-
if line.strip() == "":
186+
187+
try:
188+
pth_content = pth_content.decode()
189+
except UnicodeDecodeError:
190+
# Fallback to locale encoding for backward compatibility.
191+
# We will deprecate this fallback in the future.
192+
import locale
193+
pth_content = pth_content.decode(locale.getencoding())
194+
_trace(f"Cannot read {fullname!r} as UTF-8. "
195+
f"Using fallback encoding {locale.getencoding()!r}")
196+
197+
for n, line in enumerate(pth_content.splitlines(), 1):
198+
if line.startswith("#"):
199+
continue
200+
if line.strip() == "":
201+
continue
202+
try:
203+
if line.startswith(("import ", "import\t")):
204+
exec(line)
192205
continue
193-
try:
194-
if line.startswith(("import ", "import\t")):
195-
exec(line)
196-
continue
197-
line = line.rstrip()
198-
dir, dircase = makepath(sitedir, line)
199-
if not dircase in known_paths and os.path.exists(dir):
200-
sys.path.append(dir)
201-
known_paths.add(dircase)
202-
except Exception as exc:
203-
print("Error processing line {:d} of {}:\n".format(n+1, fullname),
204-
file=sys.stderr)
205-
import traceback
206-
for record in traceback.format_exception(exc):
207-
for line in record.splitlines():
208-
print(' '+line, file=sys.stderr)
209-
print("\nRemainder of file ignored", file=sys.stderr)
210-
break
206+
line = line.rstrip()
207+
dir, dircase = makepath(sitedir, line)
208+
if dircase not in known_paths and os.path.exists(dir):
209+
sys.path.append(dir)
210+
known_paths.add(dircase)
211+
except Exception as exc:
212+
print(f"Error processing line {n:d} of {fullname}:\n",
213+
file=sys.stderr)
214+
import traceback
215+
for record in traceback.format_exception(exc):
216+
for line in record.splitlines():
217+
print(' '+line, file=sys.stderr)
218+
print("\nRemainder of file ignored", file=sys.stderr)
219+
break
211220
if reset:
212221
known_paths = None
213222
return known_paths
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:mod:`site` module now parses ``.pth`` file with UTF-8 first, and
2+
:term:`locale encoding` if ``UnicodeDecodeError`` happened. It supported
3+
only locale encoding before.

0 commit comments

Comments
 (0)
0