[C++-sig] embedding python in C++: exceptions, a newbie Q

Yuval Nahmany yuval.nahmany at gmail.com
Mon Jun 22 13:06:25 CEST 2009


Hi,
I am trying to use boost.python to wrap some classes in an
application, and have python embedded in my app and make some
instances from the application accessible thru simple script.
I got the impression that with boost python it should be quite simple
for basics, but I cannot have a simple code as bellow run:

1. If I uncomment the  //import("MyModule"); I get a runtime error -
and an application crash. And without it- I get an exception that the
C++ class CMyWrap is not associated with a python class.

2. If I just try to do a simpler thing like //exec("dir(__main__)",
main_namespace); I get "bad argument to an internal function" as an
exception.

I use python26_d.dll/ python26_d.lib and
libboost_python-vc90-mt-1_39.lib (boost 1.39 compiled as static lib).

Can you please help?
Can you refer me to any documentation that may help?
TIA,

Yuv.

class CMyWrap
{
public:
	string Greet()
	{
		return "I'm the dude!?";
	}
};

class CppClass {
public:
	int getNum() {
		return 7;
	}
};

BOOST_PYTHON_MODULE(MyModule)
{
	class_<CMyWrap>("CMyWrap")
		.def("greet", &CMyWrap::Greet);
}


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) {
	int nRetCode = 0;
	CMyWrap myObj;
	try
	{
		PyImport_AppendInittab( "MyModule", &initMyModule);

		Py_Initialize();
		object main_module = import("__main__");

		object main_namespace = main_module.attr("__dict__");
		//import("MyModule");

		//exec("dir(__main__)", main_namespace);
		main_namespace["myObj"] = ptr(&myObj);

	}
	catch( error_already_set )
	{
		PyErr_Print();
	}
	
	Py_Finalize();

	return nRetCode;
}



--
Yuval Nahmany
cell. 054-2020665


More information about the Cplusplus-sig mailing list