Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Lib/netrc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_netrc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed regression in netrc file newline and comment handling.