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

INADA Naoki webhook-mailer at python.org
Sun Dec 10 01:10:05 EST 2017


https://github.com/python/cpython/commit/3b9173d33adc2903e1af461214333b0052d7b1e9
commit: 3b9173d33adc2903e1af461214333b0052d7b1e9
branch: 2.7
author: Steven Loria <sloria1 at gmail.com>
committer: INADA Naoki <methane at users.noreply.github.com>
date: 2017-12-10T15:09:58+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

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 4b18973d51e..16bc347023a 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 += "machine {host}\n\tlogin {attrs[0]}\n".format(host=host, attrs=attrs)
             if attrs[1]:
-                rep = rep + "account " + repr(attrs[1])
-            rep = rep + "\tpassword " + repr(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 = rep + "macdef " + macro + "\n"
+            rep += "macdef {macro}\n".format(macro=macro)
             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 4156c535eff..4d49a55cb64 100644
--- a/Lib/test/test_netrc.py
+++ b/Lib/test/test_netrc.py
@@ -5,25 +5,29 @@
 
 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'
         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__(), cleanup=True)
+        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