From: Bob Halley Date: Sun, 19 Feb 2017 21:37:14 +0000 (-0800) Subject: Make sure all 'strings' passed to the txtbase constructor are encoded if X-Git-Tag: v1.16.0~42 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5f60e45d9ffb3a5756fa36886d711d3389e16a3a;p=thirdparty%2Fdnspython.git Make sure all 'strings' passed to the txtbase constructor are encoded if needed. [Issue #239] --- diff --git a/dns/rdtypes/txtbase.py b/dns/rdtypes/txtbase.py index 352b027b..aa341f1a 100644 --- a/dns/rdtypes/txtbase.py +++ b/dns/rdtypes/txtbase.py @@ -1,4 +1,4 @@ -# Copyright (C) 2006, 2007, 2009-2011 Nominum, Inc. +# Copyright (C) 2006-2017 Nominum, Inc. # # Permission to use, copy, modify, and distribute this software and its # documentation for any purpose with or without fee is hereby granted, @@ -20,24 +20,29 @@ import struct import dns.exception import dns.rdata import dns.tokenizer -from dns._compat import binary_type +from dns._compat import binary_type, string_types class TXTBase(dns.rdata.Rdata): """Base class for rdata that is like a TXT record - @ivar strings: the text strings - @type strings: list of string + @ivar strings: the strings + @type strings: list of binary @see: RFC 1035""" __slots__ = ['strings'] def __init__(self, rdclass, rdtype, strings): super(TXTBase, self).__init__(rdclass, rdtype) - if isinstance(strings, str): + if isinstance(strings, binary_type) or \ + isinstance(strings, string_types): strings = [strings] - self.strings = strings[:] + self.strings = [] + for string in strings: + if isinstance(string, string_types): + string = string.encode() + self.strings.append(string) def to_text(self, origin=None, relativize=True, **kw): txt = '' diff --git a/tests/test_bugs.py b/tests/test_bugs.py index 717cdce5..c153c543 100644 --- a/tests/test_bugs.py +++ b/tests/test_bugs.py @@ -1,4 +1,4 @@ -# Copyright (C) 2006, 2007, 2009-2011 Nominum, Inc. +# Copyright (C) 2006-2017 Nominum, Inc. # # Permission to use, copy, modify, and distribute this software and its # documentation for any purpose with or without fee is hereby granted, @@ -24,6 +24,7 @@ import binascii import dns.rdata import dns.rdataclass import dns.rdatatype +import dns.rdtypes.ANY.TXT import dns.ttl class BugsTestCase(unittest.TestCase): @@ -82,5 +83,18 @@ class BugsTestCase(unittest.TestCase): text6 = binascii.hexlify(out6).decode('ascii') self.failUnless(text6 == '0002018f000000000000000000000000000010') + def test_TXT_conversions(self): + t1 = dns.rdtypes.ANY.TXT.TXT(dns.rdataclass.IN, dns.rdatatype.TXT, + [b'foo']) + t2 = dns.rdtypes.ANY.TXT.TXT(dns.rdataclass.IN, dns.rdatatype.TXT, + b'foo') + t3 = dns.rdtypes.ANY.TXT.TXT(dns.rdataclass.IN, dns.rdatatype.TXT, + 'foo') + t4 = dns.rdtypes.ANY.TXT.TXT(dns.rdataclass.IN, dns.rdatatype.TXT, + ['foo']) + self.failUnless(t1 == t2) + self.failUnless(t1 == t2) + self.failUnless(t1 == t4) + if __name__ == '__main__': unittest.main()