Pointer to member memory management in Python

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

Pointer to member memory management in Python

Jake
When a function returns a pointer/reference to a member, we don't want the owning object to 
get cleaned up while the managed code still holds the wrapper of the pointer.  For C#, the 
following strategy is given in the docs:


The Python binding does not seem to have a "pythonout" typemap where the 
reference could be stored, so how can we achieve the same behavior in the Python 
bindings?

-Jake Cobb

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Pointer to member memory management in Python

Jake
I found a solution using %pythonappend:


Although undocumented as far as I can tell, this causes the wrapped value to be 
stored in a local variable called "val", so the equivalent of the C# example becomes:

%pythonappend getWheel %{
  val._bike = self
%}

I think it would be worth updating the docs to at minimum state that you use 
the variable "val" with %pythonappend, and maybe also give this reference example 
since C# and Java both have an example for this use case and it's relevant to 
most (all?) memory managed language targets.

-Jake Cobb

On Fri, Feb 17, 2017 at 12:20 PM, Jake <[hidden email]> wrote:
When a function returns a pointer/reference to a member, we don't want the owning object to 
get cleaned up while the managed code still holds the wrapper of the pointer.  For C#, the 
following strategy is given in the docs:


The Python binding does not seem to have a "pythonout" typemap where the 
reference could be stored, so how can we achieve the same behavior in the Python 
bindings?

-Jake Cobb


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Pointer to member memory management in Python

William S Fulton
Hi Jake

SWIG is an all volunteer project and we accept documentation patches to the html documentation too, so anyone can contribute. The python docs are in Doc/Manual/Python.html and this would make a nice example.

William

On 21 February 2017 at 15:17, Jake <[hidden email]> wrote:
I found a solution using %pythonappend:


Although undocumented as far as I can tell, this causes the wrapped value to be 
stored in a local variable called "val", so the equivalent of the C# example becomes:

%pythonappend getWheel %{
  val._bike = self
%}

I think it would be worth updating the docs to at minimum state that you use 
the variable "val" with %pythonappend, and maybe also give this reference example 
since C# and Java both have an example for this use case and it's relevant to 
most (all?) memory managed language targets.

-Jake Cobb

On Fri, Feb 17, 2017 at 12:20 PM, Jake <[hidden email]> wrote:
When a function returns a pointer/reference to a member, we don't want the owning object to 
get cleaned up while the managed code still holds the wrapper of the pointer.  For C#, the 
following strategy is given in the docs:


The Python binding does not seem to have a "pythonout" typemap where the 
reference could be stored, so how can we achieve the same behavior in the Python 
bindings?

-Jake Cobb


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Pointer to member memory management in Python

Jake
Sure, I've opened PR 929 to add it:


-Jake

On Mon, Feb 27, 2017 at 3:59 PM, William S Fulton <[hidden email]> wrote:
Hi Jake

SWIG is an all volunteer project and we accept documentation patches to the html documentation too, so anyone can contribute. The python docs are in Doc/Manual/Python.html and this would make a nice example.

William

On 21 February 2017 at 15:17, Jake <[hidden email]> wrote:
I found a solution using %pythonappend:


Although undocumented as far as I can tell, this causes the wrapped value to be 
stored in a local variable called "val", so the equivalent of the C# example becomes:

%pythonappend getWheel %{
  val._bike = self
%}

I think it would be worth updating the docs to at minimum state that you use 
the variable "val" with %pythonappend, and maybe also give this reference example 
since C# and Java both have an example for this use case and it's relevant to 
most (all?) memory managed language targets.

-Jake Cobb

On Fri, Feb 17, 2017 at 12:20 PM, Jake <[hidden email]> wrote:
When a function returns a pointer/reference to a member, we don't want the owning object to 
get cleaned up while the managed code still holds the wrapper of the pointer.  For C#, the 
following strategy is given in the docs:


The Python binding does not seem to have a "pythonout" typemap where the 
reference could be stored, so how can we achieve the same behavior in the Python 
bindings?

-Jake Cobb


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user




------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Pointer to member memory management in Python

Daniel.Vollmer
Hi Jake,

thanks for this.

I'm not entirely sure, but isn't this (i.e. the keeping of a reference to self) something that the "default" typemap for returned references from non-static member methods in classes should (or could) do? At least right now I cannot think of an example where these don't return an interior reference (at least in our code-bases).

As another use of "val", I'm using it to cast to the derived type for a method that returns references of the base class, e.g.
%pythonappend Para::ArrayNode::operator[]
%{
    val = getattr(val, "_Cast" + val.GetNodeType())()
%}


Best regards

Daniel Vollmer

--------------------------
Deutsches Zentrum für Luft- und Raumfahrt e.V. (DLR)
German Aerospace Center
Institute of Aerodynamics and Flow Technology | Lilienthalplatz 7 | 38108 Braunschweig | Germany

Daniel Vollmer | AS C²A²S²E
www.DLR.de

Von: Jake [[hidden email]]
Gesendet: Dienstag, 28. Februar 2017 20:41
An: William S Fulton
Cc: swig-user
Betreff: Re: [Swig-user] Pointer to member memory management in Python

Sure, I've opened PR 929 to add it:


-Jake

On Mon, Feb 27, 2017 at 3:59 PM, William S Fulton <[hidden email]> wrote:
Hi Jake

SWIG is an all volunteer project and we accept documentation patches to the html documentation too, so anyone can contribute. The python docs are in Doc/Manual/Python.html and this would make a nice example.

William

On 21 February 2017 at 15:17, Jake <[hidden email]> wrote:
I found a solution using %pythonappend:


Although undocumented as far as I can tell, this causes the wrapped value to be 
stored in a local variable called "val", so the equivalent of the C# example becomes:

%pythonappend getWheel %{
  val._bike = self
%}

I think it would be worth updating the docs to at minimum state that you use 
the variable "val" with %pythonappend, and maybe also give this reference example 
since C# and Java both have an example for this use case and it's relevant to 
most (all?) memory managed language targets.

-Jake Cobb

On Fri, Feb 17, 2017 at 12:20 PM, Jake <[hidden email]> wrote:
When a function returns a pointer/reference to a member, we don't want the owning object to 
get cleaned up while the managed code still holds the wrapper of the pointer.  For C#, the 
following strategy is given in the docs:


The Python binding does not seem to have a "pythonout" typemap where the 
reference could be stored, so how can we achieve the same behavior in the Python 
bindings?

-Jake Cobb


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user




------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Loading...