summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog6
-rw-r--r--dns/name.py22
-rw-r--r--tests/name.py5
3 files changed, 27 insertions, 6 deletions
diff --git a/ChangeLog b/ChangeLog
index e9cbfe6..ce4528a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2004-09-02 Bob Halley <halley@nominum.com>
+
+ * dns/name.py (Name.to_wire): The 'file' parameter to
+ Name.to_wire() is now optional; if omitted, the wire form will
+ be returned as the value of the function.
+
2004-08-14 Bob Halley <halley@dnspython.org>
* dns/message.py (Message.find_rrset): find_rrset() now uses an
diff --git a/dns/name.py b/dns/name.py
index 11d2d71..e652931 100644
--- a/dns/name.py
+++ b/dns/name.py
@@ -21,6 +21,7 @@
@type empty: dns.name.Name object
"""
+import cStringIO
import string
import struct
import sys
@@ -343,12 +344,13 @@ class Name(object):
dlabels = ["%s%s" % (chr(len(x)), x.lower()) for x in labels]
return ''.join(dlabels)
- def to_wire(self, file, compress = None, origin = None):
+ def to_wire(self, file = None, compress = None, origin = None):
"""Convert name to wire format, possibly compressing it.
- @param file: the file where the compressed name is emitted (typically
- a cStringIO file)
- @type file: file
+ @param file: the file where the name is emitted (typically
+ a cStringIO file). If None, a string containing the wire name
+ will be returned.
+ @type file: file or None
@param compress: The compression table. If None (the default) names
will not be compressed.
@type compress: dict
@@ -359,7 +361,13 @@ class Name(object):
absolute. If self is a relative name, then an origin must be supplied;
if it is missing, then this exception is raised
"""
-
+
+ if file is None:
+ file = cStringIO.StringIO()
+ want_return = True
+ else:
+ want_return = False
+
if not self.is_absolute():
if origin is None or not origin.is_absolute():
raise NeedAbsoluteNameOrOrigin
@@ -379,7 +387,7 @@ class Name(object):
value = 0xc000 + pos
s = struct.pack('!H', value)
file.write(s)
- return
+ break
else:
if not compress is None and len(n) > 1:
pos = file.tell()
@@ -389,6 +397,8 @@ class Name(object):
file.write(chr(l))
if l > 0:
file.write(label)
+ if want_return:
+ return file.getvalue()
def __len__(self):
"""The length of the name (in labels).
diff --git a/tests/name.py b/tests/name.py
index bc7f177..9de3dbe 100644
--- a/tests/name.py
+++ b/tests/name.py
@@ -399,6 +399,11 @@ class NameTestCase(unittest.TestCase):
self.failUnless(f.getvalue() == \
'\x03FOO\x03bar\x00\x01\x61\x03foo\x03bar\x00')
+ def testToWire6(self):
+ n = dns.name.from_text('FOO.bar')
+ v = n.to_wire()
+ self.failUnless(v == '\x03FOO\x03bar\x00')
+
def testBadToWire(self):
def bad():
n = dns.name.from_text('FOO.bar', None)