[C++-sig] very basic: calling python function from c++

yakumoklesk at yahoo.es yakumoklesk at yahoo.es
Wed Sep 10 17:54:48 CEST 2003

Hi, I am also new in Boost.Python. In fact, I just joined this gropu yesterday and 
downloaded the binaries of libraries. I also compiled them (although I thikn it was not 
necessary). I have not used Boost.Python yet, but I have been using in the last days 
some C/Python API functions to access to python objects.

This is what I did to call a function from a module of mine:

	// This loads my python module
	PyRun_SimpleString("import ModuleLoader"); 
	// This gets the sys.modules dictionary (it shoud have NULL check?)
	PyObject *dicModuleAdm = PyImport_GetModuleDict();
	// This is where I get the my module, no check, I supose I will have to put one
	PyObject *modModule = PyMapping_GetItemString( dicModuleAdm, 
"ModuleLoader" );
	// I get a class instance generated by ModuleLoader when it was imported and 
executed (sorry for the coments in spanish ^_^)
	PyObject* dtTree = PyObject_GetAttrString( modModule, "dtTree" );
	if( !dtTree )
		printf("No encontrada instancia ModuleLoader.dtTree" );
	// I get the function of the instance
	PyObject* funcPrintTree = PyObject_GetAttrString( dtTree, "PrintTree" );
	if( !dtTree )
		printf("No encontrada función dtTree.PrintTree()" );
	// I check if it is callable (don't think is necessary because you defined it)
	if( PyCallable_Check( funcPrintTree ) )
		// I build the arguments. This function needs one integer value
		PyObject* argFuncArgs = Py_BuildValue( "(i)", 1 );
		// I call the object and get the result (PyNone)
		PyObject* resNoneResult = PyEval_CallObject( funcPrintTree, 
argFuncArgs );
		// Check if the funtion call went OK
		if( !resNoneResult )
			printf("Resultados no devueltos, objeto no callable o error" );

I expect this will help you.

And for the rest of programmers, I have a question: I have this code:

	PyObject *modBuiltIn = PyMapping_GetItemString( dicModuleAdm, "__builtin__" 
	PyObject *dicBuiltInDict = PyObject_GetAttrString( modBuiltIn, "__dict__" );
	PyObject *funLocals = PyMapping_GetItemString( dicBuiltInDict, "locals" );
	PyObject* resLocalsResult = PyEval_CallObject( funLocals, NULL );

That is, I get the builtin module, get the dict, get the local() function and call it. But I get 
an ACCESS VIOLATION from C++, inside the C fille bltinmodule.c from the python code 
directories. What it happens is that the line

d = PyEval_GetLocals()

 in bltinmodule.c returns NULL, and inmediately it does:

Py_INCREF(d), producing an acces violation.

Of course, there is another way to get the locals dictionary:
	PyRun_SimpleString("LOCALS = locals");
and then get acces to the LOCALS dictionary variable object, through the 
__builtin__.__dict__, but *I* don't like this way. Why it produces an acces violation? Is a 
bug or what?

Thank you in advance.

David Lucena.

On 10 Sep 2003 at 15:54, Roel Vanhout wrote:

> Hello everybody, I'm only just starting out with boost.python and with 
> python embedding and I have a very basic question. I have found examples 
> of how to get an object into c++ from python and call functions on it, 
> but what I'm trying to do is to get a function pointer to a python 
> function and call that function with two parameters. Basically I have 
> two numbers and users can enter an expression into the gui of the c++ 
> program like this:
> return num1 + num2
> I thought that then in my c++ program, I'd prepend 'function 
> calculate(num1, num2):' to that string, indent the expresson properly 
> and then run the whole thing in python, passing the two numbers that I 
> have in my c++ program to the python function. So I'm looking for a way 
> to get a function pointer that I can pass two numbers and that returns a 
> double. I hope I made myself clear - can anyone help me? Thanks in advance.
> cheers,
> roel
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig

More information about the Cplusplus-sig mailing list