summaryrefslogtreecommitdiff
path: root/dns/rdata.py
diff options
context:
space:
mode:
authorBrian Wellington <bwelling@xbill.org>2020-03-31 12:38:31 -0700
committerBrian Wellington <bwelling@xbill.org>2020-03-31 12:38:31 -0700
commit7cbed39294810fe72fd7439ce3d348913d66b752 (patch)
tree288dfad3166f999c7c7636556c927ef7b8c167cd /dns/rdata.py
parent665f8b0cedbf1a416b29664abd4ba1e1f28a789a (diff)
downloaddnspython-7cbed39294810fe72fd7439ce3d348913d66b752.tar.gz
Checkpoint immutable rdata.
Diffstat (limited to 'dns/rdata.py')
-rw-r--r--dns/rdata.py29
1 files changed, 26 insertions, 3 deletions
diff --git a/dns/rdata.py b/dns/rdata.py
index 640f731..51c5f0e 100644
--- a/dns/rdata.py
+++ b/dns/rdata.py
@@ -90,6 +90,21 @@ def _truncate_bitmap(what):
return what[0: i + 1]
return what[0:1]
+def _constify(o):
+ """
+ Convert mutable types to immutable types.
+ """
+ if type(o) == bytearray:
+ return bytes(o)
+ if type(o) == tuple:
+ try:
+ hash(o)
+ return o
+ except:
+ return tuple(_constify(elt) for elt in o)
+ if type(o) == list:
+ return tuple(_constify(elt) for elt in o)
+ return o
class Rdata(object):
"""Base class for all DNS rdata types."""
@@ -103,8 +118,16 @@ class Rdata(object):
*rdtype*, an ``int`` is the rdatatype of the Rdata.
"""
- self.rdclass = rdclass
- self.rdtype = rdtype
+ object.__setattr__(self, 'rdclass', rdclass)
+ object.__setattr__(self, 'rdtype', rdtype)
+
+ def __setattr__(self, name, value):
+ # Rdatas are immutable
+ raise TypeError("object doesn't support attribute assignment")
+
+ def __delattr__(self, name):
+ # Rdatas are immutable
+ raise TypeError("object doesn't support attribute deletion")
def covers(self):
"""Return the type a Rdata covers.
@@ -270,7 +293,7 @@ class GenericRdata(Rdata):
def __init__(self, rdclass, rdtype, data):
super(GenericRdata, self).__init__(rdclass, rdtype)
- self.data = data
+ object.__setattr__(self, 'data', data)
def to_text(self, origin=None, relativize=True, **kw):
return r'\# %d ' % len(self.data) + _hexify(self.data)