|
Boost : |
From: David Abrahams (abrahams_at_[hidden])
Date: 2001-02-05 18:50:51
As Alex Martelli recently discovered, BPL will convert const reference
return types to_python by /copying/ the referent into an object managed by a
python reference count.
BPL refuses to automatically convert non-const reference return types
to_python because there is no way to give Python control over the lifetime
of the referent and still maintain the property that the original object is
modified through mutating operations.
It is similar to the problem described in
www.boost.org/libs/python/doc/pointers.html for non-const pointer return
types. You can employ the same (dangerous) workaround described under "If
you can't (afford to) copy the referent, or the pointer is non-const" if
neccessary:
BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE // this is a gcc 2.95.2 bug
workaround
PyObject* to_python(Foo& p)
{
return boost::python::PyExtensionClassConverters::ptr_to_python(&p);
}
BOOST_PYTHON_END_CONVERSION_NAMESPACE
To what is the reference referring? If it refers to an object owned by one
of the arguments to the function you're wrapping (including "this", if it's
a member function) I could give you a way to return the non-const reference
safely; it would work by managing a reference count on the owning object. I
have offered this to several people in the past, but demand has not been
great so far.
-Dave
----- Original Message -----
From: <l.dunn_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Monday, February 05, 2001 6:11 PM
Subject: [boost] BPL: How do I return a non-const reference?
> My code has about 20 functions that return non-const references to
> wrapped classes. I'm getting lots of "from_python" errors.
>
> Apologies if this is a stupid question- I've been coding with boost
> for about 3 weeks.
>
> Thanks in advance!
>
>
>
>
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk