[C++-sig] Rational for 'Zero-length slice' RuntimeError?

Hans Meine meine at informatik.uni-hamburg.de
Mon Jan 5 15:43:06 CET 2009

On Sonntag 04 Januar 2009, Hans Meine wrote:
> I have exported an array-like class using the __getitem__ implementation
> given below among its overloads.  It uses
> boost::python::slice.get_indices(), and I am getting a RuntimeError when I
> try to acces e.g. [1:-1] on a 2-element array.  What is the rationale for
> this "Zero-length slice" error?  I would expect an empty iterator range. 
> OK, if end < begin, that could make problems, but what am I expected to do
> to prevent this RuntimeError? (Yes, I did RTFM, but maybe I am overlooking
> something?)

Yes, I was overlooking quite some things.  Quoting the docs:
> Rationale: [...]
>  exceptions on empty slice: It is impossible to define a closed interval
> over an empty range, so some other form of error checking would have to be
> used to prevent undefined behavior. In the case where the exception is not
> caught, it will simply be translated to Python by the default exception
> handling mechanisms.

I was tired and the automatic translation of std::invalid_argument into a 
RuntimeError obviously outsmarted me.

So obviously, the answer is that I have to copy the boilerplate-try..catch 
from the docs (and handle the empty interval case).

Ciao, /  /                                                    .o.
     /--/                                                     ..o
    /  / ANS                                                  ooo
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20090105/422484ac/attachment.pgp>

More information about the Cplusplus-sig mailing list