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

gh-77102: site: try utf-8 and fallback to locale encoding when reading pth file#117802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
methane merged 4 commits intopython:mainfrommethane:utf8-pth-file
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletionsDoc/library/site.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -74,6 +74,10 @@ with ``import`` (followed by space or tab) are executed.
Limiting a code chunk to a single line is a deliberate measure
to discourage putting anything more complex here.

..versionchanged::3.13
The:file:`.pth` files are now decoded by UTF-8 at first and then by the
:term:`locale encoding` if it fails.

..index::
single: package
triple: path; configuration; file
Expand Down
7 changes: 7 additions & 0 deletionsDoc/whatsnew/3.13.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -634,6 +634,13 @@ re
* Rename :exc:`!re.error` to :exc:`re.PatternError` for improved clarity.
:exc:`!re.error` is kept for backward compatibility.

site
----

* :file:`.pth` files are now decoded by UTF-8 first, and then by the
:term:`locale encoding` if the UTF-8 decoding fails.
(Contributed by Inada Naoki in :gh:`117802`.)

sqlite3
-------

Expand Down
61 changes: 35 additions & 26 deletionsLib/site.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -179,35 +179,44 @@ def addpackage(sitedir, name, known_paths):
return
_trace(f"Processing .pth file:{fullname!r}")
try:
# locale encoding is not ideal especially on Windows. But we have used
# it for a long time. setuptools uses the locale encoding too.
f=io.TextIOWrapper(io.open_code(fullname),encoding="locale")
withio.open_code(fullname)asf:
pth_content=f.read()
exceptOSError:
return
withf:
forn,lineinenumerate(f):
ifline.startswith("#"):
continue
ifline.strip()=="":

try:
pth_content=pth_content.decode()
exceptUnicodeDecodeError:
# Fallback to locale encoding for backward compatibility.
# We will deprecate this fallback in the future.
importlocale
pth_content=pth_content.decode(locale.getencoding())
_trace(f"Cannot read{fullname!r} as UTF-8. "
f"Using fallback encoding{locale.getencoding()!r}")

forn,lineinenumerate(pth_content.splitlines(),1):
ifline.startswith("#"):
continue
ifline.strip()=="":
continue
try:
ifline.startswith(("import ","import\t")):
exec(line)
continue
try:
ifline.startswith(("import ","import\t")):
exec(line)
continue
line=line.rstrip()
dir,dircase=makepath(sitedir,line)
ifnotdircaseinknown_pathsandos.path.exists(dir):
sys.path.append(dir)
known_paths.add(dircase)
exceptExceptionasexc:
print("Error processing line {:d} of {}:\n".format(n+1,fullname),
file=sys.stderr)
importtraceback
forrecordintraceback.format_exception(exc):
forlineinrecord.splitlines():
print(' '+line,file=sys.stderr)
print("\nRemainder of file ignored",file=sys.stderr)
break
line=line.rstrip()
dir,dircase=makepath(sitedir,line)
ifdircasenotinknown_pathsandos.path.exists(dir):
sys.path.append(dir)
known_paths.add(dircase)
exceptExceptionasexc:
print(f"Error processing line{n:d} of{fullname}:\n",
file=sys.stderr)
importtraceback
forrecordintraceback.format_exception(exc):
forlineinrecord.splitlines():
print(' '+line,file=sys.stderr)
print("\nRemainder of file ignored",file=sys.stderr)
break
ifreset:
known_paths=None
returnknown_paths
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
:mod:`site` module now parses ``.pth`` file with UTF-8 first, and
:term:`locale encoding` if ``UnicodeDecodeError`` happened. It supported
only locale encoding before.

[8]ページ先頭

©2009-2025 Movatter.jp