Skip to content

Commit

Permalink
Cope with 64-bit Python on Windows passing the wrong value for the un…
Browse files Browse the repository at this point in the history
…bounded slice constant.
  • Loading branch information
rthalley committed Jul 24, 2014
1 parent 25e6313 commit 9329daf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 8 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
2014-07-24 Bob Halley <[email protected]>

* 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 <[email protected]>

* When reading from a masterfile, if the first content line started
Expand Down
14 changes: 13 additions & 1 deletion dns/wiredata.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@

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):
Expand All @@ -28,7 +40,7 @@ def __getitem__(self, key):
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:
Expand Down

0 comments on commit 9329daf

Please sign in to comment.