Keeping the wrapper alive when it would go out of scope

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Keeping the wrapper alive when it would go out of scope

Nyall Dawson
Hi list,

If I have a method in a class with a /Transfer/ed argument:

   void addTask( MyClass* task /Transfer/ );

and I attempt to call it from a code which looks like this:

   def scoped_function():
        t = MyClass()
        t.some_python_property = 555
        m.addTask( t )

   scoped_function()

Then the Python wrapper for t (but not the transferred cpp object) is
deleted when t goes out of scope at the end of the function call, as
expected.

Is there anyway to indicate to sip that the lifetime of the wrapper
should be bound to the cpp object, and only deleted when the cpp
object is? What I'm trying to achieve is that the wrapper, including
the python-only "some_python_property", is not cleaned up when
scoped_function() exits, and is instead only cleaned up when the
MyClass cpp base class object is deleted.

Thanks,

Nyall
_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Reply | Threaded
Open this post in threaded view
|

Re: Keeping the wrapper alive when it would go out of scope

Denis Rouzaud
Hi Nyall,

Isn't KeepReference what you are looking for?

Denis

On Mon, 3 Dec 2018, 21:43 Nyall Dawson, <[hidden email]> wrote:
Hi list,

If I have a method in a class with a /Transfer/ed argument:

   void addTask( MyClass* task /Transfer/ );

and I attempt to call it from a code which looks like this:

   def scoped_function():
        t = MyClass()
        t.some_python_property = 555
        m.addTask( t )

   scoped_function()

Then the Python wrapper for t (but not the transferred cpp object) is
deleted when t goes out of scope at the end of the function call, as
expected.

Is there anyway to indicate to sip that the lifetime of the wrapper
should be bound to the cpp object, and only deleted when the cpp
object is? What I'm trying to achieve is that the wrapper, including
the python-only "some_python_property", is not cleaned up when
scoped_function() exits, and is instead only cleaned up when the
MyClass cpp base class object is deleted.

Thanks,

Nyall
_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
--

_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Reply | Threaded
Open this post in threaded view
|

Re: Keeping the wrapper alive when it would go out of scope

Phil Thompson-5
In reply to this post by Nyall Dawson
On 4 Dec 2018, at 1:42 am, Nyall Dawson <[hidden email]> wrote:

>
> Hi list,
>
> If I have a method in a class with a /Transfer/ed argument:
>
>   void addTask( MyClass* task /Transfer/ );
>
> and I attempt to call it from a code which looks like this:
>
>   def scoped_function():
>        t = MyClass()
>        t.some_python_property = 555
>        m.addTask( t )
>
>   scoped_function()
>
> Then the Python wrapper for t (but not the transferred cpp object) is
> deleted when t goes out of scope at the end of the function call, as
> expected.
>
> Is there anyway to indicate to sip that the lifetime of the wrapper
> should be bound to the cpp object, and only deleted when the cpp
> object is? What I'm trying to achieve is that the wrapper, including
> the python-only "some_python_property", is not cleaned up when
> scoped_function() exits, and is instead only cleaned up when the
> MyClass cpp base class object is deleted.

That is what /Transfer/ does. You need to provide a complete test to demonstrate the problem.

Phil
_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Reply | Threaded
Open this post in threaded view
|

Re: Keeping the wrapper alive when it would go out of scope

Phil Thompson-5
On 4 Dec 2018, at 9:06 am, Phil Thompson <[hidden email]> wrote:

>
> On 4 Dec 2018, at 1:42 am, Nyall Dawson <[hidden email]> wrote:
>>
>> Hi list,
>>
>> If I have a method in a class with a /Transfer/ed argument:
>>
>>  void addTask( MyClass* task /Transfer/ );
>>
>> and I attempt to call it from a code which looks like this:
>>
>>  def scoped_function():
>>       t = MyClass()
>>       t.some_python_property = 555
>>       m.addTask( t )
>>
>>  scoped_function()
>>
>> Then the Python wrapper for t (but not the transferred cpp object) is
>> deleted when t goes out of scope at the end of the function call, as
>> expected.
>>
>> Is there anyway to indicate to sip that the lifetime of the wrapper
>> should be bound to the cpp object, and only deleted when the cpp
>> object is? What I'm trying to achieve is that the wrapper, including
>> the python-only "some_python_property", is not cleaned up when
>> scoped_function() exits, and is instead only cleaned up when the
>> MyClass cpp base class object is deleted.
>
> That is what /Transfer/ does. You need to provide a complete test to demonstrate the problem.

Also, you could call sip.dump(t) either side of the call to addTask() to see the effects.

Phil
_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Reply | Threaded
Open this post in threaded view
|

Re: Keeping the wrapper alive when it would go out of scope

Nyall Dawson
In reply to this post by Phil Thompson-5
On Tue, 4 Dec 2018 at 19:06, Phil Thompson <[hidden email]> wrote:

> That is what /Transfer/ does. You need to provide a complete test to demonstrate the problem.

Ok, thanks for the confirmation. I'll try to distill this down into a
standalone demo.

Nyall
_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt