Skip to content

Commit

Permalink
dns.name.to_text() should return text. [Issue rthalley#209]
Browse files Browse the repository at this point in the history
  • Loading branch information
rthalley committed Sep 27, 2016
1 parent bb0c9f2 commit ca9af3e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
10 changes: 5 additions & 5 deletions dns/name.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import dns.exception
import dns.wiredata

from ._compat import long, binary_type, text_type, unichr
from ._compat import long, binary_type, text_type, unichr, maybe_decode

try:
maxint = sys.maxint
Expand Down Expand Up @@ -466,7 +466,7 @@ def __repr__(self):
return '<DNS name ' + self.__str__() + '>'

def __str__(self):
return self.to_text(False).decode()
return self.to_text(False)

def to_text(self, omit_final_dot=False):
"""Convert name to text format.
Expand All @@ -476,15 +476,15 @@ def to_text(self, omit_final_dot=False):
"""

if len(self.labels) == 0:
return b'@'
return maybe_decode(b'@')
if len(self.labels) == 1 and self.labels[0] == b'':
return b'.'
return maybe_decode(b'.')
if omit_final_dot and self.is_absolute():
l = self.labels[:-1]
else:
l = self.labels
s = b'.'.join(map(_escapify, l))
return s
return maybe_decode(s)

def to_unicode(self, omit_final_dot=False, idna_codec=None):
"""Convert name to Unicode text format.
Expand Down
18 changes: 9 additions & 9 deletions tests/test_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,45 +207,45 @@ def testCanonicalize1(self):
def testToText1(self):
n = dns.name.from_text('FOO.bar', origin=self.origin)
t = n.to_text()
self.assertEqual(t, b'FOO.bar.example.')
self.assertEqual(t, 'FOO.bar.example.')

def testToText2(self):
n = dns.name.from_text('FOO.bar', origin=self.origin)
t = n.to_text(True)
self.assertEqual(t, b'FOO.bar.example')
self.assertEqual(t, 'FOO.bar.example')

def testToText3(self):
n = dns.name.from_text('FOO.bar', origin=None)
t = n.to_text()
self.assertEqual(t, b'FOO.bar')
self.assertEqual(t, 'FOO.bar')

def testToText4(self):
t = dns.name.empty.to_text()
self.assertEqual(t, b'@')
self.assertEqual(t, '@')

def testToText5(self):
t = dns.name.root.to_text()
self.assertEqual(t, b'.')
self.assertEqual(t, '.')

def testToText6(self):
n = dns.name.from_text('FOO bar', origin=None)
t = n.to_text()
self.assertEqual(t, br'FOO\032bar')
self.assertEqual(t, r'FOO\032bar')

def testToText7(self):
n = dns.name.from_text(r'FOO\.bar', origin=None)
t = n.to_text()
self.assertEqual(t, b'FOO\.bar')
self.assertEqual(t, 'FOO\.bar')

def testToText8(self):
n = dns.name.from_text(r'\070OO\.bar', origin=None)
t = n.to_text()
self.assertEqual(t, b'FOO\.bar')
self.assertEqual(t, 'FOO\.bar')

def testToText9(self):
n = dns.name.from_text('FOO bar', origin=None)
t = n.to_unicode()
self.assertEqual(t, r'FOO\032bar')
self.assertEqual(t, 'FOO\\032bar')

def testToText10(self):
t = dns.name.empty.to_unicode()
Expand Down

0 comments on commit ca9af3e

Please sign in to comment.