[C++-sig] boost-python: Passing an object held in a boost::shared_ptr from C++ to python to C++

2012-08-30 Thread Philip Dunstan
Hi

I have some code that creates an object in a shared_ptr in C++. The object
is then used within python and passed from Python to another C++ function
that tries to keep hold of it. It appears that when the object goes from
C++ to Python it is converted from a shared_ptr into a python object.
Then when it goes back into C++ it is converted from the python object into
a new shared_ptr, however this shared_ptr is unrelated to the initial
holding shared_ptr.

Is it possible to set up the boost-python bindings so that the conversion
from python object to shared_ptr references the original shared_ptr
internals?

Below is the abridged code I have used to show the problem. I have attached
the full reproducible to this email. In this example an object is initially
held in a shared_ptr named s_iniital. It is grabbed from within python
through the getSharedPtr function, then pushed back into C++ through the
putSharedPtr function. Inside putSharedPtr it is copied into the weak_ptr
s_copied. Inspecting the pointers in the debugger shows that the shared_ptr
used in putSharedPtr does not have the same reference counting internals as
s_initial. The final assert will fire because the weak pointer s_copied was
only related to a single strong pointer (the pointer used in putSharedPtr)
and that pointer was destructed once putSharedPtr was finished.


static shared_ptr s_initial;
static weak_ptr s_copied;

class UseSharedPtr
{
public:
shared_ptr getSharedPtr()
{
return s_initial;
}

void putSharedPtr(shared_ptr ptr)
{
s_copied = ptr;
}
};


BOOST_PYTHON_MODULE(test)
{
class_, boost::noncopyable>("Captured",
no_init);
class_("UseSharedPtr", init<>())
.def("getSharedPtr", &UseSharedPtr::getSharedPtr)
.def("putSharedPtr", &UseSharedPtr::putSharedPtr)
;
}



s_initial = make_shared();

const char* chunk = "\
from test import UseSharedPtr \n\
x = UseSharedPtr() \n\
ptr = x.getSharedPtr() \n\
x.putSharedPtr(ptr)\n\
del x \n\
del ptr \n\
";
object result = exec(chunk, mainNamespace, mainNamespace);

assert(s_copied.lock());



thanks,
Phil
--
Philip Dunstan
p...@philipdunstan.com
www.philipdunstan.com
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using std::string;
using boost::shared_ptr;
using boost::weak_ptr;
using boost::make_shared;
using namespace boost::python;


class Captured
{
public:
Captured() {}
~Captured() {}
};

static shared_ptr s_initial;
static weak_ptr s_copied;

class UseSharedPtr
{
public:
shared_ptr getSharedPtr()
{
return s_initial;
}

void putSharedPtr(shared_ptr ptr)
{
s_copied = ptr;
}
};


#define PY_MODULE_NAME "test"

BOOST_PYTHON_MODULE(test)
{
class_, boost::noncopyable>("Captured", no_init);
class_("UseSharedPtr", init<>())
.def("getSharedPtr", &UseSharedPtr::getSharedPtr)
.def("putSharedPtr", &UseSharedPtr::putSharedPtr)
;
}


int main()
{
try
{
PyImport_AppendInittab(PY_MODULE_NAME , &inittest);

Py_Initialize();
object mainModule((handle<>(borrowed(PyImport_AddModule("__main__");
object mainNamespace = mainModule.attr("__dict__");

object test_module(handle<>(PyImport_ImportModule(PY_MODULE_NAME)));
mainNamespace[PY_MODULE_NAME] = test_module;

s_initial = make_shared();

const char* chunk = "\
from test import UseSharedPtr \n\
x = UseSharedPtr() \n\
ptr = x.getSharedPtr() \n\
x.putSharedPtr(ptr)\n\
del x \n\
del ptr \n\
";
object result = exec(chunk, mainNamespace, mainNamespace);

assert(s_copied.lock());
}
catch (const error_already_set&)
{
return 1;
}

return 0;
};___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

Re: [C++-sig] building 32 Bit and 64 Bit extensions on the same machine

2012-08-30 Thread Lars Viklund
On Mon, Aug 27, 2012 at 04:23:14PM +0200, Ronny Herzog wrote:
> Thank you,
>
> but this does not seem to work. If I do this with
> "bjam --address-model=64 -n"

Note that the previous poster did not say "--address-model=64".
He said "address-model=64". It's a feature, not an option.
Features do not have a leading dashdash.
You may of course have to point out a suitable Python path anyway, but
it helps if you actually instruct the build to be 64-bit in the first
place.

-- 
Lars Viklund | z...@acc.umu.se
___
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig