Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-43733: netrc try to use UTF-8 before using locale encoding. #25781

Merged
merged 2 commits into from
May 2, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 deletions Doc/library/netrc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ the Unix :program:`ftp` program and other FTP clients.
:func:`os.path.expanduser` is used to find the location of the
:file:`.netrc` file when *file* is not passed as argument.

.. versionchanged:: 3.10
:class:`netrc` try UTF-8 encoding before using locale specific
encoding.


.. exception:: NetrcParseError

Expand Down
8 changes: 6 additions & 2 deletions Lib/netrc.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ def __init__(self, file=None):
file = os.path.join(os.path.expanduser("~"), ".netrc")
self.hosts = {}
self.macros = {}
with open(file) as fp:
self._parse(file, fp, default_netrc)
try:
with open(file, encoding="utf-8") as fp:
self._parse(file, fp, default_netrc)
except UnicodeDecodeError:
with open(file, encoding="locale") as fp:
self._parse(file, fp, default_netrc)

def _parse(self, file, fp, default_netrc):
lexer = shlex.shlex(fp)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Change :class:`netrc.netrc` to use UTF-8 encoding before using locale
encoding.