mirror of
https://github.com/python/cpython.git
synced 2025-09-30 20:31:52 +00:00
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
(cherry picked from commit b24cd055ec
)
This commit is contained in:
parent
7891556b5c
commit
5fbe5e161c
3 changed files with 13 additions and 9 deletions
12
Lib/netrc.py
12
Lib/netrc.py
|
@ -127,15 +127,15 @@ class netrc:
|
||||||
rep = ""
|
rep = ""
|
||||||
for host in self.hosts.keys():
|
for host in self.hosts.keys():
|
||||||
attrs = self.hosts[host]
|
attrs = self.hosts[host]
|
||||||
rep = rep + "machine "+ host + "\n\tlogin " + repr(attrs[0]) + "\n"
|
rep += f"machine {host}\n\tlogin {attrs[0]}\n"
|
||||||
if attrs[1]:
|
if attrs[1]:
|
||||||
rep = rep + "account " + repr(attrs[1])
|
rep += f"\taccount {attrs[1]}\n"
|
||||||
rep = rep + "\tpassword " + repr(attrs[2]) + "\n"
|
rep += f"\tpassword {attrs[2]}\n"
|
||||||
for macro in self.macros.keys():
|
for macro in self.macros.keys():
|
||||||
rep = rep + "macdef " + macro + "\n"
|
rep += f"macdef {macro}\n"
|
||||||
for line in self.macros[macro]:
|
for line in self.macros[macro]:
|
||||||
rep = rep + line
|
rep += line
|
||||||
rep = rep + "\n"
|
rep += "\n"
|
||||||
return rep
|
return rep
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import netrc, os, unittest, sys, textwrap
|
import netrc, os, unittest, sys, tempfile, textwrap
|
||||||
from test import support
|
from test import support
|
||||||
|
|
||||||
temp_filename = support.TESTFN
|
|
||||||
|
|
||||||
class NetrcTestCase(unittest.TestCase):
|
class NetrcTestCase(unittest.TestCase):
|
||||||
|
|
||||||
|
@ -10,7 +9,8 @@ class NetrcTestCase(unittest.TestCase):
|
||||||
mode = 'w'
|
mode = 'w'
|
||||||
if sys.platform != 'cygwin':
|
if sys.platform != 'cygwin':
|
||||||
mode += 't'
|
mode += 't'
|
||||||
with open(temp_filename, mode) as fp:
|
temp_fd, temp_filename = tempfile.mkstemp()
|
||||||
|
with os.fdopen(temp_fd, mode=mode) as fp:
|
||||||
fp.write(test_data)
|
fp.write(test_data)
|
||||||
self.addCleanup(os.unlink, temp_filename)
|
self.addCleanup(os.unlink, temp_filename)
|
||||||
return netrc.netrc(temp_filename)
|
return netrc.netrc(temp_filename)
|
||||||
|
@ -24,6 +24,9 @@ class NetrcTestCase(unittest.TestCase):
|
||||||
('log1', 'acct1', 'pass1'))
|
('log1', 'acct1', 'pass1'))
|
||||||
self.assertEqual(nrc.hosts['default'], ('log2', None, 'pass2'))
|
self.assertEqual(nrc.hosts['default'], ('log2', None, 'pass2'))
|
||||||
|
|
||||||
|
nrc2 = self.make_nrc(nrc.__repr__())
|
||||||
|
self.assertEqual(nrc.hosts, nrc2.hosts)
|
||||||
|
|
||||||
def test_macros(self):
|
def test_macros(self):
|
||||||
nrc = self.make_nrc("""\
|
nrc = self.make_nrc("""\
|
||||||
macdef macro1
|
macdef macro1
|
||||||
|
|
1
Misc/NEWS.d/next/Library/2017-09-29.bpo-30806.lP5GrH.rst
Normal file
1
Misc/NEWS.d/next/Library/2017-09-29.bpo-30806.lP5GrH.rst
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Fix the string representation of a netrc object.
|
Loading…
Add table
Add a link
Reference in a new issue