-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
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 16 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(): | ||||||||||||
|
||||||||||||
| if ch == '\\': | ||||||||||||
| ch = self._read_char() | ||||||||||||
| token += ch | ||||||||||||
| continue | ||||||||||||
| if ch in self.whitespace and not enquoted: | ||||||||||||
| if token == "": | ||||||||||||
| continue | ||||||||||||
| if ch == '\n': | ||||||||||||
|
||||||||||||
| 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: | ||||||||||||
|
||||||||||||
| if tt == '#' and not lexer.dontskip: | |
| if len(tt) == 1 and not lexer.dontskip: |
but then, why does it matter whether it's # thing vs #thing? Typically, comment parsers just disregard whatever's after the hash and don't interpret that in any way…
Outdated
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.
looks like an irrelevant formatting change
| raise NetrcParseError( | |
| "missing %r name" % tt, file, lexer.lineno) | |
| raise NetrcParseError("missing %r name" % tt, file, lexer.lineno) |
Outdated
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.
I have a feeling that this might not be relevant to the fix and would be better in a separate refactoring PR.
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.
Revert deleting the CLI entry point logic:
| return rep | |
| return rep | |
| 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.