[PyKDE] Request for more SIP features

Phil Thompson phil at riverbankcomputing.co.uk
Fri Nov 26 09:24:47 GMT 2004


> Easy ones first:
>
> (1) an uninstall target in the Makefile to undo the work of the install
> target

I don't like uninstall targets because I'm never clear what they do. Do
they remove what was previously installed? Or do they restore the state
prior to the install (ie. put back any old version overwritten by the
install)?

> (2) the enums wrapped by SIP are mutable, but Python has the facility to
> define real constants:
>
>>>> import termios
>>>> print termios.B110
> 3
>>>> termios.B110 = 0
>>>> print termios.B110
> 0
>>>>

What you mean is that B110 should be a read-only property of termios. This
would be implementable for enums defined within a class, but not for those
(like this) defined within a module. I'd rather be consistent. It would be
nice if Python implemented a module as a new-style class.

> (3) support for iterators. Motivation: I started to experiment with SIP
> template files
>     to wrap the STL.  Of course, an iterator is not needed to traverse a
> std::vector, but
>     a Python iterator can remember the C++ pointer into a more advanced
> container while
>     returning a Python object.  A naive approach does not work:
>
> SIP file snippet:
>
> class CellIterator
> {
> %TypeHeaderCode
> #include <vector>
> #include <sipIterCell.h>
> class CellIterator
> {
> public:
>     CellIterator(Cell &cell): it(cell.begin()) {};
>     CellIterator * __iter__() { return this; }
>     int next() { return *it++; }
> private:
>     Cell::iterator it;
> };
> %End
>
> public:
>     CellIterator(Cell &);
>     int next();
>     CellIterator * __iter__();
> };
>
> Python interpreter (this would work for a Python class which implements
> __iter__() and next():
>
>>>> import Iter
>>>> c = Iter.Cell()
>>>> c.push_back(0)
>>>> c.push_back(1)
>>>> for i in c: print i
> ...
> 0
> 1
>>>> it = Iter.CellIterator(c)
>>>> dir(it)
> ['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__',
> '__init__', '__iter__', '__module__', '__new__', '__reduce__',
> '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__',
> 'next']
>>>> for i in it: print i
> ...
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: iteration over non-sequence
>>>>

Iterator support was on the original SIP v4 roadmap, but I dropped it when
I didn't have a case that really needed it. Normally I would much rather
add the necessary Python slots to make Cell behave as a sequence type and
iterate over it directly. I guess there are classes where this approach
would be too expensive.

Phil




More information about the PyQt mailing list