diff options
| author | Brian Wellington <bwelling@xbill.org> | 2020-05-15 13:48:34 -0700 |
|---|---|---|
| committer | Brian Wellington <bwelling@xbill.org> | 2020-05-15 13:48:34 -0700 |
| commit | df5c61379263a902dac60ebc1cf496fb03b55dca (patch) | |
| tree | 580572888c80bf542b9851e8fdb8216eb3f19674 /dns/name.py | |
| parent | 6e0857eb518ba6ceb71856e6a58f6dcb936e9535 (diff) | |
| download | dnspython-df5c61379263a902dac60ebc1cf496fb03b55dca.tar.gz | |
Optimize name.to_digestable() and to_wire().
Diffstat (limited to 'dns/name.py')
| -rw-r--r-- | dns/name.py | 32 |
1 files changed, 19 insertions, 13 deletions
diff --git a/dns/name.py b/dns/name.py index 1d151c6..ce90f91 100644 --- a/dns/name.py +++ b/dns/name.py @@ -594,16 +594,17 @@ class Name(object): Returns a ``bytes``. """ + out = bytearray() + for label in self.labels: + out.append(len(label)) + out += label.lower() if not self.is_absolute(): if origin is None or not origin.is_absolute(): raise NeedAbsoluteNameOrOrigin - labels = list(self.labels) - labels.extend(list(origin.labels)) - else: - labels = self.labels - dlabels = [struct.pack('!B%ds' % len(x), len(x), x.lower()) - for x in labels] - return b''.join(dlabels) + for label in origin.labels: + out.append(len(label)) + out += label.lower() + return bytes(out) def to_wire(self, file=None, compress=None, origin=None): """Convert name to wire format, possibly compressing it. @@ -629,10 +630,17 @@ class Name(object): """ if file is None: - file = io.BytesIO() - want_return = True - else: - want_return = False + out = bytearray() + for label in self.labels: + out.append(len(label)) + out += label + if not self.is_absolute(): + if origin is None or not origin.is_absolute(): + raise NeedAbsoluteNameOrOrigin + for label in origin.labels: + out.append(len(label)) + out += label + return bytes(out) if not self.is_absolute(): if origin is None or not origin.is_absolute(): @@ -663,8 +671,6 @@ class Name(object): file.write(struct.pack('!B', l)) if l > 0: file.write(label) - if want_return: - return file.getvalue() def __len__(self): """The length of the name (in labels). |
