Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2a58923

Browse files
authored
gh-77102: site: try utf-8 and locale encoding when reading .pth file (GH-117802)
(cherry picked from commit6dc661b)
1 parent44eab29 commit2a58923

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+
withio.open_code(fullname)asf:
183+
pth_content=f.read()
185184
exceptOSError:
186185
return
187-
withf:
188-
forn,lineinenumerate(f):
189-
ifline.startswith("#"):
190-
continue
191-
ifline.strip()=="":
186+
187+
try:
188+
pth_content=pth_content.decode()
189+
exceptUnicodeDecodeError:
190+
# Fallback to locale encoding for backward compatibility.
191+
# We will deprecate this fallback in the future.
192+
importlocale
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+
forn,lineinenumerate(pth_content.splitlines(),1):
198+
ifline.startswith("#"):
199+
continue
200+
ifline.strip()=="":
201+
continue
202+
try:
203+
ifline.startswith(("import ","import\t")):
204+
exec(line)
192205
continue
193-
try:
194-
ifline.startswith(("import ","import\t")):
195-
exec(line)
196-
continue
197-
line=line.rstrip()
198-
dir,dircase=makepath(sitedir,line)
199-
ifnotdircaseinknown_pathsandos.path.exists(dir):
200-
sys.path.append(dir)
201-
known_paths.add(dircase)
202-
exceptExceptionasexc:
203-
print("Error processing line {:d} of {}:\n".format(n+1,fullname),
204-
file=sys.stderr)
205-
importtraceback
206-
forrecordintraceback.format_exception(exc):
207-
forlineinrecord.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+
ifdircasenotinknown_pathsandos.path.exists(dir):
209+
sys.path.append(dir)
210+
known_paths.add(dircase)
211+
exceptExceptionasexc:
212+
print(f"Error processing line{n:d} of{fullname}:\n",
213+
file=sys.stderr)
214+
importtraceback
215+
forrecordintraceback.format_exception(exc):
216+
forlineinrecord.splitlines():
217+
print(' '+line,file=sys.stderr)
218+
print("\nRemainder of file ignored",file=sys.stderr)
219+
break
211220
ifreset:
212221
known_paths=None
213222
returnknown_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)

[8]ページ先頭

©2009-2026 Movatter.jp