[C++-sig] Boost::Signals

John Reid johnbaronreid at netscape.net
Tue Jun 6 23:33:24 CEST 2006


John Reid wrote:
> Hi,
> 
> I'm trying to call a python function from C++. I pass the function to 
> C++ as a boost::python::object. I can call functions with no arguments, 
> when I try to pass a C++ created object as an argument I get the 
> following error:
> 
> Traceback (most recent call last):
>    File "python/test_python.py", line 8, in ?
>      d.connect_new_deal_slot_py_2( f )
> TypeError: No to_python (by-value) converter found for C++ type: class 
> module::classname
> 
> Now the boost.python section (http://tinyurl.com/n3cpv) in the python 
> wiki answers the following question:
> "Is it is possible to convert pointers to existing classes to PyObjects* 
> and then be able to pass an existing instance of an object directly to 
> and from python?"
> 
> where the last paragraph says:
> 
> "If you really want to pass pointers around, it's certainly possible to 
> tell the library to build a Python object around the pointer, but then 
> you need to make sure the lifetime of the C++ object being referenced by 
> the pointer extends past the lifetime of all Python references to the 
> object or your program will crash."
> 
> This is what I would like to do but cannot find this elsewhere in the 
> documentation. Can anyone let me know how to do this?
> 
> Thanks in advance,
> John.

Maybe it is better if I post a small example of what I'd like to do. 
Here is the C++...


#pragma warning( push )
# pragma warning( disable : 4099 )
# pragma warning( disable : 4267 )
# include <boost/python.hpp>
#pragma warning( pop )
#include <boost/signals.hpp>

using namespace boost::python;
using namespace boost;

struct test
	: noncopyable
{
	signal< void ( object ) > _signal;
	void connect_slot_py( object const & slot )
	{
		_signal.connect( slot );

		//call just for testing
		_signal( object( this ) );
	}
};

BOOST_PYTHON_MODULE( test_bp )
{
	class_< test, noncopyable >( "test" )
         .def( "connect_slot", &test::connect_slot_py )
	    ;
	//register_ptr_to_python< test * >();
}

  and here is the python...

import test_bp

def f( arg ):
     print 'In "f"', arg

t = test_bp.test()
t.connect_slot( f )



but I still get this error...

Traceback (most recent call last):
   File "<stdin>", line 1, in ?
TypeError: No to_python (by-value) converter found for C++ type: struct test



Any help appreciated,
John.




More information about the Cplusplus-sig mailing list