[C++-sig] Re: New slice implementation

Jonathan Brandmeyer jbrandmeyer at earthlink.net
Thu Jan 8 00:33:13 CET 2004


On Tue, 2004-01-06 at 11:09, Raoul Gough wrote:
> Jonathan Brandmeyer <jbrandmeyer at earthlink.net> writes:
> 
> > I have written a new implementation for free-standing slice objects. 
> > This allows you to create and query slice objects that include a
> > step-size, as well as export a form of __getitem__ and/or __setitem__
> > that can receive slice arguments and tuples of slice arguments.
> 
> Not sure how much overlap there is, 

It turns out that there isn't much.  slice::get_indicies() overlaps with
the indexing suite's slicing support, but in a different way such that
it might be OK to keep, anyway.  My object really just provides a object
manager for PySliceObject, rather than provide a container of objects
initialized by a slice (such as the indexing_suite does).

> but have you looked into the
> container indexing suite for existing __getitem__, __setitem__ and
> __delitem__ support? Joel's code is currently only in the CVS, but
> will be included in the 1.31 release of boost, AFAIK. It includes
> support for Python slices.

Well, I've looked at the indexing suite now, and there are several
problems with its slice support.

1) Non-None values of step size are silently ignored.
I wrote a patch for slice_helper<>::base_get_slice_data() to throw
IndexError if step size is given.

2) Does not clip slice ranges to the max/min as appropriate for slices,
instead it raises IndexError.  (Patch fixes this)

3) Crashes when given slice indexes that run cross to each other (in STL
parlance, stop is not reachable from start).  I believe that the
responsibility for handling this case is in the hands of the respective
policies classes.  Therefore, my patch targets vector_indexing_suite
vice indexing_suite for these:
   foo = bar[-1:0] crashes, should return empty container. (Patched)
   del bar[-1:0] crashes, should be a no-op. (Patched)
   bar[-1:0] = foo crashes.  It has weird insert()-like semantics in 
       practice for lists, but I haven't seen it documented
       anywhere. (Patch throws IndexError in this case since performing
       a slice insertion to an empty slice should be an undefined
       operation).

-Jonathan Brandmeyer

---crash_test.py---
# Uses the existing vector_indexing_suite_ext.cpp test modul
from vector_indexing_suite_ext import *
foo = FloatVec()
# Weird initialization, why not supported by a constructor?
foo[:] = [1,2,3,4,5,6]
def print_vector(foo):
    s = '[ '
    for x in foo:
        s += repr(x) + ' '
    s += ']'
    print s

# Should raise IndexError, or print backwards; actually prints the 
# original
print_vector(foo[::-1])

# Should print the original, but instead raises IndexError
print_vector(foo[-10:10])

# Should print an empty vector( "[ ]"); crashes
print_vector(foo[-1:0])

# Should do nothing; crashes
del foo[-1:0]

# I think this should raise IndexError; crashes.
foo[-1:0] = [7, 8, 9]


-------------- next part --------------
A non-text attachment was scrubbed...
Name: indexing_suite.diff
Type: text/x-patch
Size: 4008 bytes
Desc: 
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20040107/3d371397/attachment.bin>


More information about the Cplusplus-sig mailing list