| Store | Cart

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

From: Martin v. Löwis <mar...@v.loewis.de>
Sun, 27 Mar 2005 13:10:30 +0200
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


Recent Messages in this Thread
Dave Huang Mar 26, 2005 08:29 am
"Martin v. Löwis" Mar 27, 2005 11:10 am
Messages in this thread