From e01ef6f379e2264d4c3ed6f0c9381f8dd9f92c25 Mon Sep 17 00:00:00 2001 From: James Sexton Date: Sat, 30 Sep 2017 02:10:31 -0500 Subject: [PATCH 1/3] bpo-30806 netrc.__repr__() is broken for writing to file (GH-2491) netrc file format doesn't support quotes and escapes. See https://linux.die.net/man/5/netrc --- Lib/netrc.py | 12 ++++++------ Lib/test/test_netrc.py | 8 ++++++-- .../next/Library/2017-09-29.bpo-30806.lP5GrH.rst | 1 + 3 files changed, 13 insertions(+), 8 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2017-09-29.bpo-30806.lP5GrH.rst diff --git a/Lib/netrc.py b/Lib/netrc.py index 4b18973d51e3449..5645adc123f447f 100644 --- a/Lib/netrc.py +++ b/Lib/netrc.py @@ -130,15 +130,15 @@ def __repr__(self): rep = "" for host in self.hosts.keys(): 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]: - rep = rep + "account " + repr(attrs[1]) - rep = rep + "\tpassword " + repr(attrs[2]) + "\n" + rep += f"\taccount {attrs[1]}\n" + rep += f"\tpassword {attrs[2]}\n" for macro in self.macros.keys(): - rep = rep + "macdef " + macro + "\n" + rep += f"macdef {macro}\n" for line in self.macros[macro]: - rep = rep + line - rep = rep + "\n" + rep += line + rep += "\n" return rep if __name__ == '__main__': diff --git a/Lib/test/test_netrc.py b/Lib/test/test_netrc.py index 4156c535eff499d..8eda596c5a0aab1 100644 --- a/Lib/test/test_netrc.py +++ b/Lib/test/test_netrc.py @@ -1,4 +1,4 @@ -import netrc, os, unittest, sys, textwrap +import netrc, os, unittest, sys, tempfile, textwrap from test import test_support temp_filename = test_support.TESTFN @@ -10,7 +10,8 @@ def make_nrc(self, test_data): mode = 'w' if sys.platform != 'cygwin': 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) self.addCleanup(os.unlink, temp_filename) return netrc.netrc(temp_filename) @@ -24,6 +25,9 @@ def test_default(self): ('log1', 'acct1', 'pass1')) self.assertEqual(nrc.hosts['default'], ('log2', None, 'pass2')) + nrc2 = self.make_nrc(nrc.__repr__()) + self.assertEqual(nrc.hosts, nrc2.hosts) + def test_macros(self): nrc = self.make_nrc("""\ macdef macro1 diff --git a/Misc/NEWS.d/next/Library/2017-09-29.bpo-30806.lP5GrH.rst b/Misc/NEWS.d/next/Library/2017-09-29.bpo-30806.lP5GrH.rst new file mode 100644 index 000000000000000..afad1b2fb266faa --- /dev/null +++ b/Misc/NEWS.d/next/Library/2017-09-29.bpo-30806.lP5GrH.rst @@ -0,0 +1 @@ +Fix the string representation of a netrc object. From 8efd8234f47ff6d45aa062833c20b5b76f5e4ea1 Mon Sep 17 00:00:00 2001 From: Steven Loria Date: Sat, 9 Dec 2017 12:00:58 -0500 Subject: [PATCH 2/3] Use str.format instead of f-strings for 2.7 compat Also, make the tests work in 2.7 --- Lib/netrc.py | 8 ++++---- Lib/test/test_netrc.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Lib/netrc.py b/Lib/netrc.py index 5645adc123f447f..16bc347023a8477 100644 --- a/Lib/netrc.py +++ b/Lib/netrc.py @@ -130,12 +130,12 @@ def __repr__(self): rep = "" for host in self.hosts.keys(): attrs = self.hosts[host] - rep += f"machine {host}\n\tlogin {attrs[0]}\n" + rep += "machine {host}\n\tlogin {attrs[0]}\n".format(host=host, attrs=attrs) if attrs[1]: - rep += f"\taccount {attrs[1]}\n" - rep += f"\tpassword {attrs[2]}\n" + rep += "\taccount {attrs[1]}\n".format(attrs=attrs) + rep += "\tpassword {attrs[2]}\n".format(attrs=attrs) for macro in self.macros.keys(): - rep += f"macdef {macro}\n" + rep += "macdef {macro}\n".format(macro=macro) for line in self.macros[macro]: rep += line rep += "\n" diff --git a/Lib/test/test_netrc.py b/Lib/test/test_netrc.py index 8eda596c5a0aab1..1809961a05645aa 100644 --- a/Lib/test/test_netrc.py +++ b/Lib/test/test_netrc.py @@ -11,7 +11,7 @@ def make_nrc(self, test_data): if sys.platform != 'cygwin': mode += 't' temp_fd, temp_filename = tempfile.mkstemp() - with os.fdopen(temp_fd, mode=mode) as fp: + with os.fdopen(temp_fd, mode) as fp: fp.write(test_data) self.addCleanup(os.unlink, temp_filename) return netrc.netrc(temp_filename) From 40b00057917377d504627ffdbb645951616f4f11 Mon Sep 17 00:00:00 2001 From: Steven Loria Date: Sat, 9 Dec 2017 12:11:04 -0500 Subject: [PATCH 3/3] Fix test on Windows --- Lib/test/test_netrc.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_netrc.py b/Lib/test/test_netrc.py index 1809961a05645aa..4d49a55cb64823a 100644 --- a/Lib/test/test_netrc.py +++ b/Lib/test/test_netrc.py @@ -1,31 +1,31 @@ -import netrc, os, unittest, sys, tempfile, textwrap +import netrc, os, unittest, sys, textwrap from test import test_support temp_filename = test_support.TESTFN class NetrcTestCase(unittest.TestCase): - def make_nrc(self, test_data): + def make_nrc(self, test_data, cleanup=True): test_data = textwrap.dedent(test_data) mode = 'w' if sys.platform != 'cygwin': mode += 't' - temp_fd, temp_filename = tempfile.mkstemp() - with os.fdopen(temp_fd, mode) as fp: + with open(temp_filename, mode) as fp: fp.write(test_data) - self.addCleanup(os.unlink, temp_filename) + if cleanup: + self.addCleanup(os.unlink, temp_filename) return netrc.netrc(temp_filename) def test_default(self): nrc = self.make_nrc("""\ machine host1.domain.com login log1 password pass1 account acct1 default login log2 password pass2 - """) + """, cleanup=False) self.assertEqual(nrc.hosts['host1.domain.com'], ('log1', 'acct1', 'pass1')) self.assertEqual(nrc.hosts['default'], ('log2', None, 'pass2')) - nrc2 = self.make_nrc(nrc.__repr__()) + nrc2 = self.make_nrc(nrc.__repr__(), cleanup=True) self.assertEqual(nrc.hosts, nrc2.hosts) def test_macros(self):