FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-158560: Quote values in `netrc.__repr__()` so the output can be parsed back by Prafyl · Pull Request #158561 · python/cpython · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
13 changes: 10 additions & 3 deletions Lib/netrc.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ def push_token(self, token):
self.pushback.append(token)


def _quote(token):
"""Quote a token so that _netrclex reads back the same value."""
if token and not any(ch in token for ch in ' \t\r\n"\\'):
return token
return '"' + token.replace('\\', '\\\\').replace('"', '\\"') + '"'


class netrc:
def __init__(self, file=None):
default_netrc = file is None
Expand Down Expand Up @@ -190,10 +197,10 @@ def __repr__(self):
rep = ""
for host in self.hosts.keys():
attrs = self.hosts[host]
rep += f"machine {host}\n\tlogin {attrs[0]}\n"
rep += f"machine {_quote(host)}\n\tlogin {_quote(attrs[0])}\n"
if attrs[1]:
rep += f"\taccount {attrs[1]}\n"
rep += f"\tpassword {attrs[2]}\n"
rep += f"\taccount {_quote(attrs[1])}\n"
rep += f"\tpassword {_quote(attrs[2])}\n"
for macro in self.macros.keys():
rep += f"macdef {macro}\n"
for line in self.macros[macro]:
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_netrc.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ def test_toplevel_tokens(self):
self.assertEqual(nrc.hosts['host.domain.com'], ('log1', 'acct1', 'pass1'))
self.assertEqual(nrc.hosts['default'], ('log2', 'acct2', 'pass2'))

def test_repr_roundtrip(self):
# __repr__() writes the data in .netrc format, so parsing its output
# should give back the same entries.
values = ['pass', 'pass word', ' pass', 'pass\tword', 'pass\nword',
'pass"word', 'pass\\word', '']
for value in values:
with self.subTest(value=value):
nrc = self.make_nrc("""\
machine host.domain.com login log password pass
""")
nrc.hosts['host.domain.com'] = (value, 'acct', value)
nrc2 = self.make_nrc(repr(nrc))
self.assertEqual(nrc2.hosts, nrc.hosts)

def test_macros(self):
data = """\
macdef macro1
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :meth:`!netrc.netrc.__repr__` writing values without quotes, so that an
empty value or one containing whitespace, a quote or a backslash could not be
parsed back correctly.
Loading

Back | FazBrowse Home | New Git URL