Skip to content

Commit 3b9173d

Browse files
sloriamethane
authored andcommitted
bpo-30806: Fix netrc.__repr__() format (GH-2491)
netrc file format doesn't support quotes and escapes. See https://linux.die.net/man/5/netrc
1 parent 292fce9 commit 3b9173d

3 files changed

Lines changed: 14 additions & 9 deletions

File tree

Lib/netrc.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,15 @@ def __repr__(self):
130130
rep = ""
131131
for host in self.hosts.keys():
132132
attrs = self.hosts[host]
133-
rep = rep + "machine "+ host + "\n\tlogin " + repr(attrs[0]) + "\n"
133+
rep += "machine {host}\n\tlogin {attrs[0]}\n".format(host=host, attrs=attrs)
134134
if attrs[1]:
135-
rep = rep + "account " + repr(attrs[1])
136-
rep = rep + "\tpassword " + repr(attrs[2]) + "\n"
135+
rep += "\taccount {attrs[1]}\n".format(attrs=attrs)
136+
rep += "\tpassword {attrs[2]}\n".format(attrs=attrs)
137137
for macro in self.macros.keys():
138-
rep = rep + "macdef " + macro + "\n"
138+
rep += "macdef {macro}\n".format(macro=macro)
139139
for line in self.macros[macro]:
140-
rep = rep + line
141-
rep = rep + "\n"
140+
rep += line
141+
rep += "\n"
142142
return rep
143143

144144
if __name__ == '__main__':

Lib/test/test_netrc.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,29 @@
55

66
class NetrcTestCase(unittest.TestCase):
77

8-
def make_nrc(self, test_data):
8+
def make_nrc(self, test_data, cleanup=True):
99
test_data = textwrap.dedent(test_data)
1010
mode = 'w'
1111
if sys.platform != 'cygwin':
1212
mode += 't'
1313
with open(temp_filename, mode) as fp:
1414
fp.write(test_data)
15-
self.addCleanup(os.unlink, temp_filename)
15+
if cleanup:
16+
self.addCleanup(os.unlink, temp_filename)
1617
return netrc.netrc(temp_filename)
1718

1819
def test_default(self):
1920
nrc = self.make_nrc("""\
2021
machine host1.domain.com login log1 password pass1 account acct1
2122
default login log2 password pass2
22-
""")
23+
""", cleanup=False)
2324
self.assertEqual(nrc.hosts['host1.domain.com'],
2425
('log1', 'acct1', 'pass1'))
2526
self.assertEqual(nrc.hosts['default'], ('log2', None, 'pass2'))
2627

28+
nrc2 = self.make_nrc(nrc.__repr__(), cleanup=True)
29+
self.assertEqual(nrc.hosts, nrc2.hosts)
30+
2731
def test_macros(self):
2832
nrc = self.make_nrc("""\
2933
macdef macro1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix the string representation of a netrc object.

0 commit comments

Comments
 (0)