By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,932 Members | 1,944 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,932 IT Pros & Developers. It's quick & easy.

__getslice__ passed INT_MAX rather than sys.maxint for missing endpoint?

P: n/a
Hi, I don't actually know Python; I'm just trying to debug a problem
I encounted in another program, so apologies if this has been
covered before. I did do some Google searches though, and didn't
find anything that specifically addressed this :)

According to the documentation at
<http://docs.python.org/ref/sequence-methods.html>, __getslice__
is passed sys.maxint for a missing endpoint (foo[i:]). However, as
far as I can tell, it's actually passing INT_MAX. I'm running Python
2.4 on an Alpha running NetBSD 2.0, an LP64 platform, so the two
aren't the same. INT_MAX is 2^32-1, whereas sys.maxint is LONG_MAX:
2^64-1.

So, am I misunderstanding things, or is this a doc bug, or a code bug?

FWIW, the problem I encountered was some code that did something like:
def __getslice__(self, a, b):
if b == maxint:
b = self.length # this never got executed, causing problems
--
Name: Dave Huang | Mammal, mammal / their names are called /
INet: kh**@azeotrope.org | they raise a paw / the bat, the cat /
FurryMUCK: Dahan | dolphin and dog / koala bear and hog -- TMBG
Dahan: Hani G Y+C 29 Y++ L+++ W- C++ T++ A+ E+ S++ V++ F- Q+++ P+ B+ PA+ PL++
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Dave Huang wrote:
So, am I misunderstanding things, or is this a doc bug, or a code bug?


Strictly speaking, it is both a doc bug and a code bug.

In CPython, indexes are typically represented internally through C int,
and this is also used to represent the size of the standard containers
(lists, strings, tuples). So "infinity" is appropriately represented
as INT_MAX for such containers.

OTOH, Python int objects are implemented through C longs, so sys.maxint
might be larger if longs are wider than ints. For the indexing issue,
this should be irrelevant: an omitted upper bound should still be the
"infinity" of the index type, so it is a bug when the documentation says
its maxint. However, it is also a bug that the index type is int, as it
should rather be size_t (more precisely, ssize_t). Unfortunately, long
and ssize_t are also of different widths on some platforms.

So I think there should be a sys.maxindex, which is the maximum value
for ssize_t, and that should be the implicit upper bound for getslice,
and the documentation should be fixed accordingly.

For 2.4.x, it is *just* that the documentation should be fixed; the
code needs to stay as it is even though there is no convenient way
to get at the maximum index. Just trying one time at startup would
be the recommended way:

class _GetMaxIndex:
def __getslice__(self, a, mi):
import sys
sys.maxindex = mi
_GetMaxIndex()[:]

Regards,
Martin
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.