-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
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
base: main
Are you sure you want to change the base?
Changes from 15 commits
52ea05f
2810677
68c946d
dce8305
a542e84
27b83e9
1920abe
8c243db
3284bbf
98d0df9
3e1a021
47399ef
e835819
7687836
7f64f23
9e63185
dc72ad1
5ed10a7
e32c360
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,7 +2,8 @@ | |||||||||||
|
||||||||||||
# Module and documentation by Eric S. Raymond, 21 Dec 1998 | ||||||||||||
|
||||||||||||
import os, stat | ||||||||||||
import os | ||||||||||||
import stat | ||||||||||||
|
||||||||||||
__all__ = ["netrc", "NetrcParseError"] | ||||||||||||
|
||||||||||||
|
@@ -22,6 +23,7 @@ def __str__(self): | |||||||||||
class _netrclex: | ||||||||||||
def __init__(self, fp): | ||||||||||||
self.lineno = 1 | ||||||||||||
self.dontskip = False | ||||||||||||
self.instream = fp | ||||||||||||
self.whitespace = "\n\t\r " | ||||||||||||
self.pushback = [] | ||||||||||||
|
@@ -33,30 +35,29 @@ def _read_char(self): | |||||||||||
return ch | ||||||||||||
|
||||||||||||
def get_token(self): | ||||||||||||
self.dontskip = False | ||||||||||||
if self.pushback: | ||||||||||||
return self.pushback.pop(0) | ||||||||||||
token = "" | ||||||||||||
fiter = iter(self._read_char, "") | ||||||||||||
for ch in fiter: | ||||||||||||
if ch in self.whitespace: | ||||||||||||
enquoted = False | ||||||||||||
while ch := self._read_char(): | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was this refactoring necessary to fix the bug? It's rather difficult to read the diff with so many lines reshuffled. If I were to guess, this might be the reason people are postponing doing reviews on this PR. |
||||||||||||
if ch == '\\': | ||||||||||||
ch = self._read_char() | ||||||||||||
token += ch | ||||||||||||
continue | ||||||||||||
if ch in self.whitespace and not enquoted: | ||||||||||||
if token == "": | ||||||||||||
continue | ||||||||||||
if ch == '\n': | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about |
||||||||||||
self.dontskip = True | ||||||||||||
return token | ||||||||||||
if ch == '"': | ||||||||||||
for ch in fiter: | ||||||||||||
if ch == '"': | ||||||||||||
return token | ||||||||||||
elif ch == "\\": | ||||||||||||
ch = self._read_char() | ||||||||||||
token += ch | ||||||||||||
if enquoted: | ||||||||||||
return token | ||||||||||||
enquoted = True | ||||||||||||
continue | ||||||||||||
else: | ||||||||||||
if ch == "\\": | ||||||||||||
ch = self._read_char() | ||||||||||||
token += ch | ||||||||||||
for ch in fiter: | ||||||||||||
if ch in self.whitespace: | ||||||||||||
return token | ||||||||||||
elif ch == "\\": | ||||||||||||
ch = self._read_char() | ||||||||||||
token += ch | ||||||||||||
return token | ||||||||||||
|
||||||||||||
def push_token(self, token): | ||||||||||||
|
@@ -66,7 +67,7 @@ def push_token(self, token): | |||||||||||
class netrc: | ||||||||||||
def __init__(self, file=None): | ||||||||||||
default_netrc = file is None | ||||||||||||
if file is None: | ||||||||||||
if default_netrc: | ||||||||||||
file = os.path.join(os.path.expanduser("~"), ".netrc") | ||||||||||||
self.hosts = {} | ||||||||||||
self.macros = {} | ||||||||||||
|
@@ -81,13 +82,15 @@ def _parse(self, file, fp, default_netrc): | |||||||||||
lexer = _netrclex(fp) | ||||||||||||
while 1: | ||||||||||||
# Look for a machine, default, or macdef top-level keyword | ||||||||||||
saved_lineno = lexer.lineno | ||||||||||||
toplevel = tt = lexer.get_token() | ||||||||||||
tt = lexer.get_token() | ||||||||||||
if not tt: | ||||||||||||
break | ||||||||||||
elif tt[0] == '#': | ||||||||||||
if lexer.lineno == saved_lineno and len(tt) == 1: | ||||||||||||
# For top level tokens, we skip line if the # is followed | ||||||||||||
# by a space / newline. Otherwise, we only skip the token. | ||||||||||||
if tt == '#' and not lexer.dontskip: | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if I understand why the entirety of
Suggested change
but then, why does it matter whether it's |
||||||||||||
lexer.instream.readline() | ||||||||||||
lexer.lineno += 1 | ||||||||||||
continue | ||||||||||||
elif tt == 'machine': | ||||||||||||
entryname = lexer.get_token() | ||||||||||||
|
@@ -98,6 +101,7 @@ def _parse(self, file, fp, default_netrc): | |||||||||||
self.macros[entryname] = [] | ||||||||||||
while 1: | ||||||||||||
line = lexer.instream.readline() | ||||||||||||
lexer.lineno += 1 | ||||||||||||
if not line: | ||||||||||||
raise NetrcParseError( | ||||||||||||
"Macro definition missing null line terminator.", | ||||||||||||
|
@@ -114,17 +118,18 @@ def _parse(self, file, fp, default_netrc): | |||||||||||
"bad toplevel token %r" % tt, file, lexer.lineno) | ||||||||||||
|
||||||||||||
if not entryname: | ||||||||||||
raise NetrcParseError("missing %r name" % tt, file, lexer.lineno) | ||||||||||||
raise NetrcParseError( | ||||||||||||
"missing %r name" % tt, file, lexer.lineno) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks like an irrelevant formatting change
Suggested change
|
||||||||||||
|
||||||||||||
# We're looking at start of an entry for a named machine or default. | ||||||||||||
login = account = password = '' | ||||||||||||
self.hosts[entryname] = {} | ||||||||||||
while 1: | ||||||||||||
prev_lineno = lexer.lineno | ||||||||||||
tt = lexer.get_token() | ||||||||||||
if tt.startswith('#'): | ||||||||||||
if lexer.lineno == prev_lineno: | ||||||||||||
if not lexer.dontskip: | ||||||||||||
lexer.instream.readline() | ||||||||||||
lexer.lineno += 1 | ||||||||||||
continue | ||||||||||||
if tt in {'', 'machine', 'default', 'macdef'}: | ||||||||||||
self.hosts[entryname] = (login, account, password) | ||||||||||||
|
@@ -165,12 +170,7 @@ def _security_check(self, fp, default_netrc, login): | |||||||||||
|
||||||||||||
def authenticators(self, host): | ||||||||||||
"""Return a (user, account, password) tuple for given host.""" | ||||||||||||
if host in self.hosts: | ||||||||||||
return self.hosts[host] | ||||||||||||
elif 'default' in self.hosts: | ||||||||||||
return self.hosts['default'] | ||||||||||||
else: | ||||||||||||
return None | ||||||||||||
return self.hosts.get(host, self.hosts.get('default')) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have a feeling that this might not be relevant to the fix and would be better in a separate refactoring PR. |
||||||||||||
|
||||||||||||
def __repr__(self): | ||||||||||||
"""Dump the class data in the format of a .netrc file.""" | ||||||||||||
|
@@ -188,5 +188,4 @@ def __repr__(self): | |||||||||||
rep += "\n" | ||||||||||||
return rep | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Revert deleting the CLI entry point logic:
Suggested change
|
||||||||||||
|
||||||||||||
sleiderr marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
if __name__ == '__main__': | ||||||||||||
print(netrc()) | ||||||||||||
sleiderr marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix incorrect comment parsing in the :mod:`netrc` module. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's undo formatting to make sure only relevant changes are visible in the diff.