]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43733: netrc try to use UTF-8 before using locale encoding. (GH-25781)
authorInada Naoki <songofacandy@gmail.com>
Sun, 2 May 2021 05:01:02 +0000 (14:01 +0900)
committerGitHub <noreply@github.com>
Sun, 2 May 2021 05:01:02 +0000 (14:01 +0900)
Doc/library/netrc.rst
Lib/netrc.py
Misc/NEWS.d/next/Library/2021-05-01-22-59-20.bpo-43733.gJWwEQ.rst [new file with mode: 0644]

index 3d29ac49b9191a1853d32122c4af284d434b17a1..4bf7de67c1d03e57b2506bec1933d5daaa8ea659 100644 (file)
@@ -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
 
index f0ae48cfed9e67e8192b82fc66c0755a8cfdb270..734d94c8a628535274a58a84c272a9e4924434e5 100644 (file)
@@ -26,8 +26,12 @@ class netrc:
             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 (file)
index 0000000..5ecd928
--- /dev/null
@@ -0,0 +1,2 @@
+Change :class:`netrc.netrc` to use UTF-8 encoding before using locale
+encoding.