summaryrefslogtreecommitdiff
path: root/dns
diff options
context:
space:
mode:
authorPetr Viktorin <pviktori@redhat.com>2015-05-21 13:29:32 +0200
committerPetr Viktorin <pviktori@redhat.com>2015-05-21 13:29:32 +0200
commit8646a0410cefa18485fc1f8eaebb7faa2c964ccf (patch)
treeef129d50497eda122e06b02c3bd97143c94888f8 /dns
parent0b8ae7fd1107f330f94afc03311f233d51968a66 (diff)
downloaddnspython-8646a0410cefa18485fc1f8eaebb7faa2c964ccf.tar.gz
Fix failing test for Zone.to_file
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.
Diffstat (limited to 'dns')
-rw-r--r--dns/zone.py24
1 files changed, 16 insertions, 8 deletions
diff --git a/dns/zone.py b/dns/zone.py
index 9193a7d..b162ad3 100644
--- a/dns/zone.py
+++ b/dns/zone.py
@@ -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()