[Python-checkins] bpo-30806: Fix netrc.__repr__() format (GH-2491)

INADA Naoki webhook-mailer at python.org
Fri Nov 3 01:36:48 EDT 2017


https://github.com/python/cpython/commit/5fbe5e161c969bc8a0d44a301152f8bf5afe0fc7
commit: 5fbe5e161c969bc8a0d44a301152f8bf5afe0fc7
branch: 3.6
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: INADA Naoki <methane at users.noreply.github.com>
date: 2017-11-03T14:36:45+09:00
summary:

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 b24cd055ecb3eea9a15405a6ca72dafc739e6531)

files:
A Misc/NEWS.d/next/Library/2017-09-29.bpo-30806.lP5GrH.rst
M Lib/netrc.py
M Lib/test/test_netrc.py

diff --git a/Lib/netrc.py b/Lib/netrc.py
index bbb3d23b543..1d90f5d9008 100644
--- a/Lib/netrc.py
+++ b/Lib/netrc.py
@@ -127,15 +127,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 60a3ec9565b..ca6f27d03c3 100644
--- a/Lib/test/test_netrc.py
+++ b/Lib/test/test_netrc.py
@@ -1,7 +1,6 @@
-import netrc, os, unittest, sys, textwrap
+import netrc, os, unittest, sys, tempfile, textwrap
 from test import support
 
-temp_filename = support.TESTFN
 
 class NetrcTestCase(unittest.TestCase):
 
@@ -10,7 +9,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 +24,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 00000000000..afad1b2fb26
--- /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.



More information about the Python-checkins mailing list