]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
The 'file' parameter to Name.to_wire() is now optional. The 'file'
authorBob Halley <halley@dnspython.org>
Fri, 2 Sep 2005 05:23:30 +0000 (05:23 +0000)
committerBob Halley <halley@dnspython.org>
Fri, 2 Sep 2005 05:23:30 +0000 (05:23 +0000)
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 <halley@dnspython.org>
Date: 2004-09-02 20:12:17

ChangeLog
dns/name.py
tests/name.py

index e9cbfe69792d2a99fcc9f9de2114ed9ab7eeb48c..ce4528ac10c3c86f2ab0bebe050b5642b4c5b3f8 100644 (file)
--- 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
index 11d2d711b1c8cb4a1575759602b1f31dcfc4c3d2..e6529315c566601d6d14640cdc8b94bcac3f7813 100644 (file)
@@ -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).
index bc7f1777606e9d4fda18abc1468fea787003678b..9de3dbe5157b392c4fe68c4a92ab5cb3d895a0c7 100644 (file)
@@ -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)