[PyQt] Changing the wrapped object

Phil Thompson phil at riverbankcomputing.com
Fri May 4 16:01:38 BST 2012


On Fri, 4 May 2012 15:25:10 +0200, Jens Thoms Toerring <jt at toerring.de>
wrote:
> On Fri, May 04, 2012 at 01:42:11PM +0100, Phil Thompson wrote:
>> On Fri, 4 May 2012 14:33:10 +0200, Jens Thoms Toerring <jt at toerring.de>
>> wrote:
>> >    I have a C++ class SIP-wrapped with the '/Abstract/'
>> > annotation, Thus Pyton just has a pointer to instances of
>> > this class (and ownership is with C++). Now, at a certaind
>> > point I would like to make the Python wrapper object point
>> > to a different instance (of the same class). Finding the add-
>> > ress of the Python object is simple using sipGetPyObject().
>> > But is there some way make that Python object refer to a dif-
>> > ferent of my C++-objects? I guess that there's some pointer
>> > to my C++ object in that Python object that just need to be
>> > modified to point to the new instance of the C++ class. I've
>> > been going through all of the dicumentation several times but
>> > haven't found anything that looks as if would do something
>> > like that. And, of course, I wouldn't like to mess with un-
>> > documented features of the SIP-wrapper, resulting in every-
>> > thing breaking with the next SIP release;-)
>> 
>> There is no supported way of doing this.
> 
> Thank you. Bit of a pity. Guess I'll have to find some other
> way of dealing with this...
>  
> In case you're interested in the background here's an attempt
> to explain what I'm doing. Consider you have a class for dir-
> ectories and a class for files in that directory. Also assume
> that for some reasons (see below) the ownership must remain
> with C++. Now the directory class has a method to delete the
> directory. Thus the C++ object for the directory object as
> well all the objects for the files in the directory get de-
> leted. No problem with C++ - I simply document that if you
> have used the delete() method using the directory object and
> all the file objects derived from it can't be used anymore -
> C++ programmer are used to such things. But Python still has
> the wrapper objects with the pointers and if the user tries
> to use them afterwards the whole script will (in the best of
> all cases) crash. That doesn't look very "pythonic" to me -
> I guess a Python programmer will expect the script to abort
> with an exception, explaining about the errors of his way.
> Thus I would like to "redirect" the Python wrapper objects
> to a new C++ objects that, when used, throw an exception
> (and transfer ownership for them to Python, so they get
> deleted when Python garbage-collects the wrapper objects).
> 
> If you wonder why I need to keep the ownership to C++ there
> are two reasons (the actual stuff I do isn't about directories
> and files, it's just simpler to visualize it that way): these
> objects have a lot of data (several hundreds of MB) and making
> copies of them simply would require too much memory.

What's that got to do with ownership? Ownership is simply who has
responsibility for calling the dtor.

> Moreover,
> under some circumstanes these objects must be immutable - and
> Python doesn't seem to have C++'s notion of const-ness.
> 
>                              Best regards, Jens

Phil


More information about the PyQt mailing list