[C++-sig] Evaluating multiple statements

Ingo Luetkebohle ingo at fargonauten.de
Tue Jul 6 21:45:06 CEST 2004

Hi Murray,

you're using Py_CompileString already and that method can compile whole
modules with Py_file_input.

Say you have
	def fun():
		return 1	

Where 'fun' could of course be arbitrarily complex.  Then you'd do
something like (all error handling and reference counting omitted):

	PyObject* c = Py_CompileString(text, "null", Py_file_input);
	PyObject* module = PyImport_ExecModule("module name", c);
	PyObject* dict = PyModule_GetDict(module);
	PyObject* f = PyDict_GetItemString(dict, "fun");

And you get yourself a callable for "fun" for use with

Hope that helps,


Soll doch jeder bleiben, wie er gerne wäre.

More information about the Cplusplus-sig mailing list