dnspython  1.16.0
About: dnspython is a DNS toolkit (for Python 2.x) that supports almost all record types.
  Fossies Dox: dnspython-1.16.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

wiredata.py
Go to the documentation of this file.
1 # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
2 
3 # Copyright (C) 2011,2017 Nominum, Inc.
4 #
5 # Permission to use, copy, modify, and distribute this software and its
6 # documentation for any purpose with or without fee is hereby granted,
7 # provided that the above copyright notice and this permission notice
8 # appear in all copies.
9 #
10 # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
16 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 
18 """DNS Wire Data Helper"""
19 
20 import dns.exception
21 from ._compat import binary_type, string_types, PY2
22 
23 # Figure out what constant python passes for an unspecified slice bound.
24 # It's supposed to be sys.maxint, yet on 64-bit windows sys.maxint is 2^31 - 1
25 # but Python uses 2^63 - 1 as the constant. Rather than making pointless
26 # extra comparisons, duplicating code, or weakening WireData, we just figure
27 # out what constant Python will use.
28 
29 
31 
32  def __getitem__(self, key):
33  return key.stop
34 
35  if PY2:
36  def __getslice__(self, i, j): # pylint: disable=getslice-method
37  return self.__getitem__(slice(i, j))
38 
39 _unspecified_bound = _SliceUnspecifiedBound()[1:]
40 
41 
43  # WireData is a binary type with stricter slicing
44 
45  def __getitem__(self, key):
46  try:
47  if isinstance(key, slice):
48  # make sure we are not going outside of valid ranges,
49  # do stricter control of boundaries than python does
50  # by default
51  start = key.start
52  stop = key.stop
53 
54  if PY2:
55  if stop == _unspecified_bound:
56  # handle the case where the right bound is unspecified
57  stop = len(self)
58 
59  if start < 0 or stop < 0:
61  # If it's not an empty slice, access left and right bounds
62  # to make sure they're valid
63  if start != stop:
64  super(WireData, self).__getitem__(start)
65  super(WireData, self).__getitem__(stop - 1)
66  else:
67  for index in (start, stop):
68  if index is None:
69  continue
70  elif abs(index) > len(self):
72 
73  return WireData(super(WireData, self).__getitem__(
74  slice(start, stop)))
75  return bytearray(self.unwrap())[key]
76  except IndexError:
78 
79  if PY2:
80  def __getslice__(self, i, j): # pylint: disable=getslice-method
81  return self.__getitem__(slice(i, j))
82 
83  def __iter__(self):
84  i = 0
85  while 1:
86  try:
87  yield self[i]
88  i += 1
90  raise StopIteration
91 
92  def unwrap(self):
93  return binary_type(self)
94 
95 
96 def maybe_wrap(wire):
97  if isinstance(wire, WireData):
98  return wire
99  elif isinstance(wire, binary_type):
100  return WireData(wire)
101  elif isinstance(wire, string_types):
102  return WireData(wire.encode())
103  raise ValueError("unhandled type %s" % type(wire))
dns.wiredata._SliceUnspecifiedBound.__getitem__
def __getitem__(self, key)
Definition: wiredata.py:32
dns.exception.FormError
Definition: exception.py:109
dns.wiredata._SliceUnspecifiedBound.__getslice__
def __getslice__(self, i, j)
Definition: wiredata.py:36
dns.wiredata.WireData.unwrap
def unwrap(self)
Definition: wiredata.py:92
dns.wiredata.WireData
Definition: wiredata.py:42
dns.wiredata.WireData.__getitem__
def __getitem__(self, key)
Definition: wiredata.py:45
dns.wiredata.WireData.__iter__
def __iter__(self)
Definition: wiredata.py:83
dns.wiredata.WireData.__getslice__
def __getslice__(self, i, j)
Definition: wiredata.py:80
dns.wiredata._SliceUnspecifiedBound
Definition: wiredata.py:30
dns.exception
Definition: exception.py:1
dns._compat.binary_type
binary_type
Definition: _compat.py:19
dns.wiredata.maybe_wrap
def maybe_wrap(wire)
Definition: wiredata.py:96