From 9bfae49c6852aad5c99e6b58bbea8abfa8503ae5 Mon Sep 17 00:00:00 2001 From: Bo Bayles Date: Fri, 20 Jul 2018 19:47:45 -0500 Subject: [PATCH 1/3] Re-adapt Python 2 solution --- Lib/netrc.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Lib/netrc.py b/Lib/netrc.py index f0ae48cfed9e67..35320adca0a870 100644 --- a/Lib/netrc.py +++ b/Lib/netrc.py @@ -35,13 +35,16 @@ def _parse(self, file, fp, default_netrc): lexer.commenters = lexer.commenters.replace('#', '') while 1: # Look for a machine, default, or macdef top-level keyword - saved_lineno = lexer.lineno + saved_pos = lexer.instream.tell() toplevel = tt = lexer.get_token() if not tt: break elif tt[0] == '#': - if lexer.lineno == saved_lineno and len(tt) == 1: - lexer.instream.readline() + # seek to beginning of comment, in case reading the token put + # us on a new line, and then skip the rest of the line. + new_pos = lexer.instream.tell() + lexer.instream.seek(new_pos - len(tt) - 1) + lexer.instream.readline() continue elif tt == 'machine': entryname = lexer.get_token() From db738e71834be509e5d64ce14229d965ea09ac31 Mon Sep 17 00:00:00 2001 From: Bo Bayles Date: Fri, 20 Jul 2018 19:54:10 -0500 Subject: [PATCH 2/3] Add NEWS entry --- .../NEWS.d/next/Library/2018-07-20-19-54-05.bpo-34132.FK4czq.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2018-07-20-19-54-05.bpo-34132.FK4czq.rst diff --git a/Misc/NEWS.d/next/Library/2018-07-20-19-54-05.bpo-34132.FK4czq.rst b/Misc/NEWS.d/next/Library/2018-07-20-19-54-05.bpo-34132.FK4czq.rst new file mode 100644 index 00000000000000..01a4fa37c29b79 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-07-20-19-54-05.bpo-34132.FK4czq.rst @@ -0,0 +1 @@ +Fixed regression in netrc file newline and comment handling. From ccb25990f96236fb850264254e80511627e1c86b Mon Sep 17 00:00:00 2001 From: Bo Bayles Date: Fri, 20 Jul 2018 19:55:13 -0500 Subject: [PATCH 3/3] Add tests for regression --- Lib/test/test_netrc.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Lib/test/test_netrc.py b/Lib/test/test_netrc.py index f59e5371acad3b..35734bf5673c53 100644 --- a/Lib/test/test_netrc.py +++ b/Lib/test/test_netrc.py @@ -165,6 +165,22 @@ def fake_expanduser(s): self.assertTrue(called) + def test_newline_comment_space(self): + nrc = self.make_nrc( + '\n' + '# Comment\n' + 'default login anonymous password user@site\n' + ) + self.assertEqual(nrc.hosts['default'], ('anonymous', None, 'user@site')) + + def test_newline_comment_no_space(self): + nrc = self.make_nrc( + '\n' + '#Comment\n' + 'default login anonymous password user@site\n' + ) + self.assertEqual(nrc.hosts['default'], ('anonymous', None, 'user@site')) + if __name__ == "__main__": unittest.main()