]> granicus.if.org Git - python/commitdiff
bpo-30806 netrc.__repr__() is broken for writing to file (GH-2491)
authorJames Sexton <TwistedLadder@gmail.com>
Sat, 30 Sep 2017 07:10:31 +0000 (02:10 -0500)
committerINADA Naoki <methane@users.noreply.github.com>
Sat, 30 Sep 2017 07:10:31 +0000 (16:10 +0900)
netrc file format doesn't support quotes and escapes.

See https://linux.die.net/man/5/netrc

Lib/netrc.py
Lib/test/test_netrc.py
Misc/NEWS.d/next/Library/2017-09-29.bpo-30806.lP5GrH.rst [new file with mode: 0644]

index aa8eea3c4ddecae6549f6d6637fd469331cbc88f..baf8f1d99d9d305c60750252d465c34a62108eaa 100644 (file)
@@ -127,15 +127,15 @@ class netrc:
         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__':
index 60a3ec9565b4cf77f265dde72706a09590358e08..ca6f27d03c3afd273c381ef9fc00cd8f41772861 100644 (file)
@@ -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 @@ class NetrcTestCase(unittest.TestCase):
         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 @@ class NetrcTestCase(unittest.TestCase):
                          ('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 (file)
index 0000000..afad1b2
--- /dev/null
@@ -0,0 +1 @@
+Fix the string representation of a netrc object.