diff options
| author | Martin Basti <martin.basti@gmail.com> | 2016-08-02 21:21:27 +0200 |
|---|---|---|
| committer | Martin <martin.basti@gmail.com> | 2016-08-05 01:34:11 +0200 |
| commit | 9cd52c285461ac6e64644fdb4195d14e11c94db9 (patch) | |
| tree | 85d1b3fa048d0fed698f076e6c5b9a2bfb891bdb /dns/wiredata.py | |
| parent | 5ea92537d776687e1718a3fba11ff5509166f6e8 (diff) | |
| download | dnspython-9cd52c285461ac6e64644fdb4195d14e11c94db9.tar.gz | |
Py3: remove __getslice__ method
__getslice__ is deprecated in py2 and it is not used in py3 at all.
Instead of this __getitem__ with slice() index should be used.
Please note that WireData class must still use __getslice__, because it
inherites from 'binary_type' class that has implemented __getslice__
thus this method has to be overriden in WireData class.
Diffstat (limited to 'dns/wiredata.py')
| -rw-r--r-- | dns/wiredata.py | 59 |
1 files changed, 39 insertions, 20 deletions
diff --git a/dns/wiredata.py b/dns/wiredata.py index b381f7b..ccef595 100644 --- a/dns/wiredata.py +++ b/dns/wiredata.py @@ -15,6 +15,7 @@ """DNS Wire Data Helper""" +import sys import dns.exception from ._compat import binary_type, string_types @@ -26,12 +27,16 @@ from ._compat import binary_type, string_types # out what constant Python will use. -class _SliceUnspecifiedBound(str): +class _SliceUnspecifiedBound(binary_type): - def __getslice__(self, i, j): - return j + def __getitem__(self, key): + return key.stop + + if sys.version_info < (3,): + def __getslice__(self, i, j): # pylint: disable=getslice-method + return self.__getitem__(slice(i, j)) -_unspecified_bound = _SliceUnspecifiedBound('')[1:] +_unspecified_bound = _SliceUnspecifiedBound()[1:] class WireData(binary_type): @@ -40,26 +45,40 @@ class WireData(binary_type): def __getitem__(self, key): try: if isinstance(key, slice): - return WireData(super(WireData, self).__getitem__(key)) + # make sure we are not going outside of valid ranges, + # do stricter control of boundaries than python does + # by default + start = key.start + stop = key.stop + + if sys.version_info < (3,): + if stop == _unspecified_bound: + # handle the case where the right bound is unspecified + stop = len(self) + + if start < 0 or stop < 0: + raise dns.exception.FormError + # If it's not an empty slice, access left and right bounds + # to make sure they're valid + if start != stop: + super(WireData, self).__getitem__(start) + super(WireData, self).__getitem__(stop - 1) + else: + for index in (start, stop): + if index is None: + continue + elif abs(index) > len(self): + raise dns.exception.FormError + + return WireData(super(WireData, self).__getitem__( + slice(start, stop))) return bytearray(self.unwrap())[key] except IndexError: raise dns.exception.FormError - def __getslice__(self, i, j): - try: - if j == _unspecified_bound: - # handle the case where the right bound is unspecified - j = len(self) - if i < 0 or j < 0: - raise dns.exception.FormError - # If it's not an empty slice, access left and right bounds - # to make sure they're valid - if i != j: - super(WireData, self).__getitem__(i) - super(WireData, self).__getitem__(j - 1) - return WireData(super(WireData, self).__getslice__(i, j)) - except IndexError: - raise dns.exception.FormError + if sys.version_info < (3,): + def __getslice__(self, i, j): # pylint: disable=getslice-method + return self.__getitem__(slice(i, j)) def __iter__(self): i = 0 |
