diff options
| author | Bob Halley <halley@dnspython.org> | 2017-02-19 13:37:14 -0800 |
|---|---|---|
| committer | Bob Halley <halley@dnspython.org> | 2017-02-19 13:37:14 -0800 |
| commit | 5f60e45d9ffb3a5756fa36886d711d3389e16a3a (patch) | |
| tree | 2a9a8910894c35c6d8de1529b46ed94230ff1644 | |
| parent | 4f26a2c7af86474a3800c616aa26f2f419dd28c5 (diff) | |
| download | dnspython-5f60e45d9ffb3a5756fa36886d711d3389e16a3a.tar.gz | |
Make sure all 'strings' passed to the txtbase constructor are encoded if
needed. [Issue #239]
| -rw-r--r-- | dns/rdtypes/txtbase.py | 17 | ||||
| -rw-r--r-- | tests/test_bugs.py | 16 |
2 files changed, 26 insertions, 7 deletions
diff --git a/dns/rdtypes/txtbase.py b/dns/rdtypes/txtbase.py index 352b027..aa341f1 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 717cdce..c153c54 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() |
