]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
Fix failing test for Zone.to_file 97/head
authorPetr Viktorin <pviktori@redhat.com>
Thu, 21 May 2015 11:29:32 +0000 (13:29 +0200)
committerPetr Viktorin <pviktori@redhat.com>
Thu, 21 May 2015 11:29:32 +0000 (13:29 +0200)
A fix for https://github.com/rthalley/dnspython/issues/94

Make the to_file method work on string files, unless explicitly told
to do binary encoding.
Take the line terminator from os, rather than relying on the print
function.

Change the test to use a text file rather than binary, and add a new
test for to_file with a binary file.

dns/zone.py
tests/test_zone.py

index 9193a7dd7053d03282c68a099d1961db1f9d2a04..b162ad3016cc27fae0274bf464bfec483b085ead 100644 (file)
@@ -21,6 +21,7 @@ import builtins
 import io
 import re
 import sys
+import os
 
 import dns.exception
 import dns.name
@@ -444,7 +445,7 @@ class Zone(object):
                     for rdata in rds:
                         yield (name, rds.ttl, rdata)
 
-    def to_file(self, f, sorted=True, relativize=True, nl=None):
+    def to_file(self, f, sorted=True, relativize=True, nl=None, binary=False):
         """Write a zone to a file.
 
         @param f: file or string.  If I{f} is a string, it is treated
@@ -460,17 +461,23 @@ class Zone(object):
         output will use the platform's native end-of-line marker (i.e.
         LF on POSIX, CRLF on Windows, CR on Macintosh).
         @type nl: string or None
+        @param binary: True if the file is open in binary mode
+        @type binary: bool
         """
 
         if nl is None:
-            opts = 'w'
-        else:
-            opts = 'wb'
+            nl = os.linesep
+
         if isinstance(f, str):
-            f = open(f, opts)
+            if binary:
+                mode = 'wb'
+            else:
+                mode = 'w'
+            f = open(f, mode)
             want_close = True
         else:
             want_close = False
+
         try:
             if sorted:
                 names = builtins.sorted(self.keys())
@@ -479,11 +486,12 @@ class Zone(object):
             for n in names:
                 l = self[n].to_text(n, origin=self.origin,
                                     relativize=relativize)
-                if nl is None:
-                    print(l, file=f)
-                else:
+                if binary:
                     f.write(l.encode('ascii'))
                     f.write(nl.encode('ascii'))
+                else:
+                    f.write(l)
+                    f.write(nl)
         finally:
             if want_close:
                 f.close()
index 67fc2ae6a004e14b93dcf8456aec660bc2e3489e..08557ada552bf74ef942204db981d3ce6ab4b251 100644 (file)
@@ -121,12 +121,25 @@ class ZoneTestCase(unittest.TestCase):
                 os.unlink('example2.out')
         self.assertTrue(ok)
 
+    def testFromFile2b(self):
+        """Test to_file with a binary file"""
+        z = dns.zone.from_file('example', 'example', relativize=False)
+        ok = False
+        try:
+            with open('example2b.out', 'wb') as f:
+                z.to_file(f, relativize=False, nl='\x0a', binary=True)
+            ok = filecmp.cmp('example2b.out', 'example2.good')
+        finally:
+            if not _keep_output:
+                os.unlink('example2b.out')
+        self.assertTrue(ok)
+
     def testToText(self):
         z = dns.zone.from_file('example', 'example')
         ok = False
         try:
             text_zone = z.to_text(nl='\x0a')
-            f = open('example3.out', 'wb')
+            f = open('example3.out', 'w')
             f.write(text_zone)
             f.close()
             ok = filecmp.cmp('example3.out', 'example3.good')