[python] Extending: Understanding ownership of pointers
Hi,
I had a bit of trouble deciding which mailing list to post this question
(Python or boost). I decided to post it here since the way I'm extending is
boost-specific, however the fundamental question is in regards to how Python
internally handles ownership. I do not feel there is a perfect location for
this question, so I hope you'll forgive the semi-off topicness of the
question.
Right now I'm exposing a function to Python that returns a pointer to a
custom object owned by a third party library. In most cases I can manage to
use boost::shared_ptr when I'm returning pointers, however since this is a
third party library I cannot do this. Since the pointer I'm returning is
owned and managed by another object, I gave my def() the following return
policy:
return_value_policy
On Fri, Mar 21, 2008 at 11:32 AM, Robert Dailey
Hi,
I had a bit of trouble deciding which mailing list to post this question (Python or boost). I decided to post it here since the way I'm extending is boost-specific, however the fundamental question is in regards to how Python internally handles ownership. I do not feel there is a perfect location for this question, so I hope you'll forgive the semi-off topicness of the question.
Right now I'm exposing a function to Python that returns a pointer to a custom object owned by a third party library. In most cases I can manage to use boost::shared_ptr when I'm returning pointers, however since this is a third party library I cannot do this. Since the pointer I'm returning is owned and managed by another object, I gave my def() the following return policy:
return_value_policy
() I simply assumed this was correct. How does Python handle the pointer to this object? Is there some documents in either boost or the python documentation that I can read to understand a little more about how Python handles object ownership when the Python objects really represent C++ pointers? Perhaps someone could give me a brief rundown on the concept.
Thanks in advance.
*bump*
For manage_new_object, how does Python determine when to delete the pointer?
For variables defined in python function scope this seems obvious, it would
be destroyed after the function is done executing. For globals, however, I'm
not sure. You also have to consider what happens when you make copies of the
variable (passing into functions, explicit copies through assignment, etc).
I don't know for sure what will happen in any of these scenarios. Object
copies in Python have always confused me, since there is no concept of
by-value or by-reference. I've found through running simple test apps that
some objects, when passed into parameters of functions, are passed by
reference (in other words, the original object is modified when the function
body mutates the parameter it was passed into) and some are by value (the
original object is NOT modified when the function mutates the parameter it
was passed into). I'm not really sure why Python makes this feel so
inconsistent.
This is a little off-topic from Boost at this point and I do apologize, but
I hope you will bare with me. I brought this up on the Python mailing list
once and I didn't feel like I received any answers to my questions. I'm
still confused about it, at least.
On Tue, Mar 25, 2008 at 6:33 AM, Per Ghosh
As I understand it (been working with boost-python for two weeks) so:
returning a pointer by "return_value_policy
()" and that pointer will not be managed by python (not deleted) returning a pointer by "return_value_policy
()" and that pointer will be managed by python. It will be deleted by python. I order to return a pointer to python, the pointer should point to a known object (declared within the BOOST_PYTHON_MODULE macro)
Robert Dailey skrev:
Hi,
I had a bit of trouble deciding which mailing list to post this question (Python or boost). I decided to post it here since the way I'm extending is boost-specific, however the fundamental question is in regards to how Python internally handles ownership. I do not feel there is a perfect location for this question, so I hope you'll forgive the semi-off topicness of the question.
Right now I'm exposing a function to Python that returns a pointer to a custom object owned by a third party library. In most cases I can manage to use boost::shared_ptr when I'm returning pointers, however since this is a third party library I cannot do this. Since the pointer I'm returning is owned and managed by another object, I gave my def() the following return policy:
return_value_policy
() I simply assumed this was correct. How does Python handle the pointer to this object? Is there some documents in either boost or the python documentation that I can read to understand a little more about how Python handles object ownership when the Python objects really represent C++ pointers? Perhaps someone could give me a brief rundown on the concept.
Thanks in advance.
------------------------------
_______________________________________________ Boost-users mailing listBoost-users@lists.boost.orghttp://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Per Ghosh
-
Robert Dailey