+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
@type empty: dns.name.Name object
"""
+import cStringIO
import string
import struct
import sys
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
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
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()
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).
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)