From: Bob Halley Date: Fri, 2 Sep 2005 05:23:30 +0000 (+0000) Subject: The 'file' parameter to Name.to_wire() is now optional. The 'file' X-Git-Tag: v1.3.4~18 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cf5ba2546bb64b76c0c4fd31c64654c19c647f71;p=thirdparty%2Fdnspython.git The 'file' parameter to Name.to_wire() is now optional. The 'file' parameter to Name.to_wire() is now optional; if omitted, the wire form will be returned as the value of the function. This makes getting the wire form of a name more convenient. Original author: Bob Halley Date: 2004-09-02 20:12:17 --- diff --git a/ChangeLog b/ChangeLog index e9cbfe69..ce4528ac 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2004-09-02 Bob Halley + + * 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 * dns/message.py (Message.find_rrset): find_rrset() now uses an diff --git a/dns/name.py b/dns/name.py index 11d2d711..e6529315 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 bc7f1777..9de3dbe5 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)