Skip to content

gh-104306: Fix incorrect comment handling in the netrc module, minor refactor #104511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions Lib/netrc.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,25 @@ def __init__(self, fp):
self.instream = fp
self.whitespace = "\n\t\r "
self.pushback = []
self.char_pushback = []

def _read_char(self):
if self.char_pushback:
return self.char_pushback.pop(0)
ch = self.instream.read(1)
if ch == "\n":
self.lineno += 1
return ch

def skip_blank_lines(self):
fiter = iter(self._read_char, "")
for ch in fiter:
if ch == '\n':
self.lineno += 1
else:
self.char_pushback.append(ch)
return

def get_token(self):
if self.pushback:
return self.pushback.pop(0)
Expand Down Expand Up @@ -81,6 +93,7 @@ def _parse(self, file, fp, default_netrc):
lexer = _netrclex(fp)
while 1:
# Look for a machine, default, or macdef top-level keyword
lexer.skip_blank_lines()
saved_lineno = lexer.lineno
toplevel = tt = lexer.get_token()
if not tt:
Expand Down Expand Up @@ -120,6 +133,9 @@ def _parse(self, file, fp, default_netrc):
login = account = password = ''
self.hosts[entryname] = {}
while 1:
# Trailing blank lines would break the checks that determine if the token
# is the last one on its line.
lexer.skip_blank_lines()
prev_lineno = lexer.lineno
tt = lexer.get_token()
if tt.startswith('#'):
Expand Down
26 changes: 26 additions & 0 deletions Lib/test/test_netrc.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,14 @@ def test_comment_before_machine_line_hash_only(self):
machine bar.domain.com login foo password pass
""")

def test_comment_after_new_line(self):
self._test_comment("""\
machine foo.domain.com login bar password pass

# TEST
machine bar.domain.com login foo password pass
""")

def test_comment_after_machine_line(self):
self._test_comment("""\
machine foo.domain.com login bar password pass
Expand Down Expand Up @@ -251,6 +259,24 @@ def test_comment_after_machine_line_hash_only(self):
#
""")

def test_comment_at_first_line_trailing_new_line(self):
self._test_comment("""
# TEST
machine foo.domain.com login bar password pass
machine bar.domain.com login foo password pass
""")

def test_comment_multiple_trailing_new_lines(self):
self._test_comment("""
# TEST
machine foo.domain.com login bar password pass


#FTP

machine bar.domain.com login foo password pass
""")

def test_comment_at_end_of_machine_line(self):
self._test_comment("""\
machine foo.domain.com login bar password pass # comment
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix incorrect comment parsing in the :mod:`netrc` module.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you expand on this and include context that would give an arbitrary changelog reader an idea of how the change might impact them?

Loading