: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
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)