Skip to content

Commit fd0bc7e

Browse files
authored
bpo-43733: netrc try to use UTF-8 before using locale encoding. (GH-25781)
1 parent 49b26fa commit fd0bc7e

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

Doc/library/netrc.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ the Unix :program:`ftp` program and other FTP clients.
3838
:func:`os.path.expanduser` is used to find the location of the
3939
:file:`.netrc` file when *file* is not passed as argument.
4040

41+
.. versionchanged:: 3.10
42+
:class:`netrc` try UTF-8 encoding before using locale specific
43+
encoding.
44+
4145

4246
.. exception:: NetrcParseError
4347

Lib/netrc.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ def __init__(self, file=None):
2626
file = os.path.join(os.path.expanduser("~"), ".netrc")
2727
self.hosts = {}
2828
self.macros = {}
29-
with open(file) as fp:
30-
self._parse(file, fp, default_netrc)
29+
try:
30+
with open(file, encoding="utf-8") as fp:
31+
self._parse(file, fp, default_netrc)
32+
except UnicodeDecodeError:
33+
with open(file, encoding="locale") as fp:
34+
self._parse(file, fp, default_netrc)
3135

3236
def _parse(self, file, fp, default_netrc):
3337
lexer = shlex.shlex(fp)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Change :class:`netrc.netrc` to use UTF-8 encoding before using locale
2+
encoding.

0 commit comments

Comments
 (0)