Home | Trees | Indices | Help |
|
---|
|
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 38 39 _unspecified_bound = _SliceUnspecifiedBound()[1:] 40 4143 # WireData is a binary type with stricter slicing 448246 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: 60 raise dns.exception.FormError 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): 71 raise dns.exception.FormError 72 73 return WireData(super(WireData, self).__getitem__( 74 slice(start, stop))) 75 return bytearray(self.unwrap())[key] 76 except IndexError: 77 raise dns.exception.FormError78 79 if PY2: 81 return self.__getitem__(slice(i, j))84 i = 0 85 while 1: 86 try: 87 yield self[i] 88 i += 1 89 except dns.exception.FormError: 90 raise StopIteration91 94 9597 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))104
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Sat Dec 8 10:59:56 2018 | http://epydoc.sourceforge.net |