[C++-sig] Re: New slice implementation
Raoul Gough
RaoulGough at yahoo.co.uk
Sat Jan 10 00:41:13 CET 2004
Jonathan Brandmeyer <jbrandmeyer at earthlink.net> writes:
> On Fri, 2004-01-09 at 08:19, Raoul Gough wrote:
>> Jonathan Brandmeyer <jbrandmeyer at earthlink.net> writes:
>>
>> > On Thu, 2004-01-08 at 20:55, Raoul Gough wrote:
>> [snip]
>> >> part = v[1::4]
>> >>
>> >> calls v.__getitem__ with a PySliceObject (1, None, 4)
>> >
>> > *I* won't end up with a PySliceObject created by the Python interpreter,
>> > I will end up with a boost::python::slice object manager that has been
>> > automatically converted by def() and/or class_::def() from the original
>> > PySliceObject.
>>
>> Well, that's the bit I don't understand. How do you create a
>> boost::python::slice object from a PySliceObject without a suitable
>> constructor? Unless you're just not interested in covering this case.
>
> What I believe happens is that PySlice_Check() is called on the
> PyObject* that is being tested to see if it can be wrapped. If true,
> then a slice object is created by calling boost::python::slice(
> detail::borrowed_reference(PyObject*)). Then, if you want to get the
> objects that the slice was defined with by calling slice::start(),
> slice::stop(), or slice::step(), each of those functions must cast the
> wrapped PyObject* to a PySliceObject*. This is guaranteed to be safe
> since the slice object is never created without either a) creating a new
> PySliceObject, or b) calling PySlice_Check() first. To the best of my
> knowledge, that's how most of the object managers work.
>
> Can you give me a complete example where you need this extra
> constructor? Maybe then I'll know how to answer you better.
Sorry - my mistake! I missed the following in your code (at the end of
the slice class):
// This declaration, in conjunction with the specialization of
// object_manager_traits<> below, allows C++ functions accepting
// slice arguments to be called from from Python. These
// constructors should never be used in client code.
BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(slice, object)
I thought I'd seen all of the constructors, but missed this one, and
searching for PySliceObject didn't find anything of course. Thanks for
clarifying this for me.
[snip]
> So how do you define the point to perform the insertion? If the user
> asks to replace every element that is greater than or equal to the
> fourth and less than the first element, where do you place the new
> sequence?
Well, that's an interesting question when you put it like that,
independant of implementation issues. In practice, I guess it's just
natural that it should go at the starting index, since you would have
initialized some kind of variable to this before determining that
you're already outside the range of the slice. I know that's how my
code works.
--
Raoul Gough.
export LESS='-X'
More information about the Cplusplus-sig
mailing list