[Python-checkins] bpo-43733: netrc try to use UTF-8 before using locale encoding. (GH-25781)

methane webhook-mailer at python.org
Sun May 2 01:01:19 EDT 2021


https://github.com/python/cpython/commit/fd0bc7e7f4f2c7de98a1ebc7ad1ef65b8f8f7ad6
commit: fd0bc7e7f4f2c7de98a1ebc7ad1ef65b8f8f7ad6
branch: master
author: Inada Naoki <songofacandy at gmail.com>
committer: methane <songofacandy at gmail.com>
date: 2021-05-02T14:01:02+09:00
summary:

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

files:
A Misc/NEWS.d/next/Library/2021-05-01-22-59-20.bpo-43733.gJWwEQ.rst
M Doc/library/netrc.rst
M Lib/netrc.py

diff --git a/Doc/library/netrc.rst b/Doc/library/netrc.rst
index 3d29ac49b9191..4bf7de67c1d03 100644
--- a/Doc/library/netrc.rst
+++ b/Doc/library/netrc.rst
@@ -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
 
diff --git a/Lib/netrc.py b/Lib/netrc.py
index f0ae48cfed9e6..734d94c8a6285 100644
--- a/Lib/netrc.py
+++ b/Lib/netrc.py
@@ -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)
diff --git a/Misc/NEWS.d/next/Library/2021-05-01-22-59-20.bpo-43733.gJWwEQ.rst b/Misc/NEWS.d/next/Library/2021-05-01-22-59-20.bpo-43733.gJWwEQ.rst
new file mode 100644
index 0000000000000..5ecd928b11834
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-05-01-22-59-20.bpo-43733.gJWwEQ.rst
@@ -0,0 +1,2 @@
+Change :class:`netrc.netrc` to use UTF-8 encoding before using locale
+encoding.



More information about the Python-checkins mailing list