Problem with embedding AND extending with BOOST

Emmanuel Astier emmanuel.astier at winwise.fr
Thu Jun 7 11:42:05 EDT 2001


Hi, 

I'm quite new to python, and try to extend/embed it (under
Windows/VC6)

To extend it, I'm using the BOOST python library.

But I have a problem whenever I want to import a class and use it.

Here's my code :

----------------------------------------------
main.c :
----------------------------------------------
namespace hello {

	class world
	{
	public:
		world(int) {}
		~world() {}
		int get() const { return 5; }
	};

	
	size_t length(const int& x) { return x;}
}

#include <boost/python/class_builder.hpp>

// Python requires an exported function called init<module-name> in
every
// extension module. This is where we build the module contents.
extern "C"
#ifdef _WIN32
__declspec(dllexport)
#endif
void inithello()
{
    try
    {
		// create an object representing this extension module
		boost::python::module_builder hello("hello");
		
		// Create the Python type object for our extension class
		boost::python::class_builder<hello::world> world_class(hello,
"world");
		
		// Add the __init__ function
		world_class.def(boost::python::constructor<int>());
		// Add a regular member function
		world_class.def(&hello::world::get, "get");
		
		// Add a regular function to the module
		hello.def(hello::length, "length");
    }
    catch(...)
    {
		boost::python::handle_exception();    // Deal with the exception for
Python
    }
}



void	main()
{
	PyObject*	modname, * mod, *func, *mdict;
	long		Answer;

	Py_Initialize();
	inithello();
	modname = PyString_FromString("Totohello");
	mod = PyImport_Import(modname);
	// Initialisation et verification de Python :
	if (mod) 
	{
		Answer = 1;
		mdict = PyModule_GetDict(mod);
		func = PyDict_GetItemString(mdict, "MyFunction");
		if ( func )
		{
			Answer = 2;
			if ( PyCallable_Check(func) )
			{
				PyObject*	rslt;
				rslt = PyObject_CallFunction(func, "i", 5);
				if (rslt) 
				{
					Answer = PyInt_AsLong(rslt);
					Py_XDECREF(rslt);
				}
				else
				{
					PyErr_Print();
					Answer = 3;
				}
			}
		}
	}
	printf("Answer : %i", Answer );
	Py_XDECREF ( mod );
	Py_XDECREF(modname);
	Py_Finalize();
}

----------------------------------------------
Totohello.py :
----------------------------------------------
def ChangeFov(a):
    from hello import *
    MyInst = world(2)
    #print MyInst.get()			<- Crash when uncommented !
    val = length(5)
    return 14 + val



The linker report 2 errors, one of those is :
:\dev\boost\boost_1_22_0\boost\python\detail\extension_class.hpp(225)
: warning C4541: 'dynamic_cast' used on polymorphic type 'class
boost::python::detail::instance_holder_base' with /GR-; unpredictable
behavior may result
        d:\dev\boost\boost_1_22_0\boost\python\detail\extension_class.hpp(218)
: while compiling class-template member function 'class hello::world
*__cdecl boost::python::python_extension_class_converters<class
hello::world,class boost::python::det
ail::held_instance<class hello::world> >::non_null_from_python(struct
_object *,struct boost::python::type<class hello::world *>)'

and effectively, it crashes in the "non_null_from_python" function...


As someone suceeded in extended AND embedding python using BOOST ?

Thanks for any help, 

Emmanuel



More information about the Python-list mailing list