From 9329daf40d252f25597f44d5e1db8347304d707f Mon Sep 17 00:00:00 2001 From: Bob Halley Date: Thu, 24 Jul 2014 08:11:36 -0700 Subject: Cope with 64-bit Python on Windows passing the wrong value for the unbounded slice constant. --- ChangeLog | 8 ++++++++ dns/wiredata.py | 14 +++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index f210688..32ce3ff 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2014-07-24 Bob Halley + + * The 64-bit version of Python on Windows has sys.maxint set to 2^31-1, + yet passes 2^63-1 as the "unspecified bound" value in slices. + This is a bug in Python as the documentation says the unspecified + bound value should be sys.maxint. We now cope with this. Thanks to + Matthäus Wander for reporting the problem. + 2014-06-21 Bob Halley * When reading from a masterfile, if the first content line started diff --git a/dns/wiredata.py b/dns/wiredata.py index 86d954a..1d14bd3 100644 --- a/dns/wiredata.py +++ b/dns/wiredata.py @@ -19,6 +19,18 @@ import sys import dns.exception +# Figure out what constant python passes for an unspecified slice bound. +# It's supposed to be sys.maxint, yet on 64-bit windows sys.maxint is 2^31 - 1 +# but Python uses 2^63 - 1 as the constant. Rather than making pointless +# extra comparisons, duplicating code, or weakening WireData, we just figure +# out what constant Python will use. + +class _SliceUnspecifiedBound(str): + def __getslice__(self, i, j): + return j + +_unspecified_bound = _SliceUnspecifiedBound('')[1:] + class WireData(str): # WireData is a string with stricter slicing def __getitem__(self, key): @@ -28,7 +40,7 @@ class WireData(str): raise dns.exception.FormError def __getslice__(self, i, j): try: - if j == sys.maxint: + if j == _unspecified_bound: # handle the case where the right bound is unspecified j = len(self) if i < 0 or j < 0: -- cgit v1.2.1