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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave Huang

    #1

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

    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 misunderstandin g 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__(se lf, a, b):
    if b == maxint:
    b = self.length # this never got executed, causing problems
    --
    Name: Dave Huang | Mammal, mammal / their names are called /
    INet: khym@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++
  • Martin v. Löwis

    #2
    Re: __getslice__ passed INT_MAX rather than sys.maxint for missingendpoint ?

    Dave Huang wrote:[color=blue]
    > So, am I misunderstandin g things, or is this a doc bug, or a code bug?[/color]

    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__(se lf, a, mi):
    import sys
    sys.maxindex = mi
    _GetMaxIndex()[:]

    Regards,
    Martin

    Comment

    Working...