From fd0bc7e7f4f2c7de98a1ebc7ad1ef65b8f8f7ad6 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Sun, 2 May 2021 14:01:02 +0900 Subject: bpo-43733: netrc try to use UTF-8 before using locale encoding. (GH-25781) --- Doc/library/netrc.rst | 4 ++++ Lib/netrc.py | 8 ++++++-- Misc/NEWS.d/next/Library/2021-05-01-22-59-20.bpo-43733.gJWwEQ.rst | 2 ++ 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2021-05-01-22-59-20.bpo-43733.gJWwEQ.rst diff --git a/Doc/library/netrc.rst b/Doc/library/netrc.rst index 3d29ac4..4bf7de6 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 f0ae48c..734d94c 100644 --- a/Lib/netrc.py +++ b/Lib/netrc.py @@ -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 index 0000000..5ecd928 --- /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. -- cgit v0.12