diff --git a/Lib/netrc.py b/Lib/netrc.py index b285fd8e357ddb..f75d0adf2bae82 100644 --- a/Lib/netrc.py +++ b/Lib/netrc.py @@ -45,7 +45,10 @@ def get_token(self): if ch == '"': return token elif ch == "\\": + backslash = ch ch = self._read_char() + if ch != '"': + token += backslash token += ch else: if ch == "\\": @@ -54,8 +57,6 @@ def get_token(self): for ch in fiter: if ch in self.whitespace: return token - elif ch == "\\": - ch = self._read_char() token += ch return token diff --git a/Lib/test/test_netrc.py b/Lib/test/test_netrc.py index 573d636de956d1..a864851ebbaa75 100644 --- a/Lib/test/test_netrc.py +++ b/Lib/test/test_netrc.py @@ -156,6 +156,28 @@ def test_token_value_non_ascii(self): machine host.domain.com login log password \xa1\xa2 account acct """, 'password', '\xa1\xa2') + def test_token_value_with_backslash(self): + self._test_token_x("""\ + machine host.domain.com login lo\gin password pass account acct + """, 'login', 'lo\gin') + self._test_token_x("""\ + machine host.domain.com login log password pass account \account + """, 'account', '\account') + self._test_token_x("""\ + machine host.domain.com login log password pass\word account acct + """, 'password', 'pass\word') + + def test_token_value_with_backslash_enclosed_in_quotes(self): + self._test_token_x("""\ + machine host.domain.com login "lo\gin" password pass account acct + """, 'login', 'lo\gin') + self._test_token_x("""\ + machine host.domain.com login log password pass account "acc\ount" + """, 'account', 'acc\ount') + self._test_token_x("""\ + machine host.domain.com login log password "pass\word" account acct + """, 'password', 'pass\word') + def test_token_value_leading_hash(self): self._test_token_x("""\ machine host.domain.com login #log password pass account acct diff --git a/Misc/NEWS.d/next/Library/2022-11-04-08-32-16.gh-issue-99080.-a_SsH.rst b/Misc/NEWS.d/next/Library/2022-11-04-08-32-16.gh-issue-99080.-a_SsH.rst new file mode 100644 index 00000000000000..0f43ed12195b84 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-11-04-08-32-16.gh-issue-99080.-a_SsH.rst @@ -0,0 +1 @@ +Treat backslashes in .netrc files as escaping characters only for quotes in quotes