[C++-sig] Re: compile errors with embedding example from tutorial
Graeme Lufkin
graeme.lufkin at gmail.com
Thu Jul 1 18:05:32 CEST 2004
I've had similar problems, and have slogged my way to figuring
them out. The tutorial is wrong, at least on gcc 3.2.3.
Here's what you want: After calling Py_Initialize(), you should get
the Python main namespace object by doing:
handle<> main_module(borrowed( PyImport_AddModule("__main__") ));
dict main_namespace = extract<dict>(PyModule_GetDict(main_module.get()));
The first line gets a pointer to the '__main__' module object and
stores it as a borrowed reference in a handle. The second line gets
the dictionary of the '__main__' module and puts it in a dict C++
object. You should now use this dictionary as the globals and locals
of PyRun_* functions that you call. For example:
handle<> unused(PyRun_String("import yourModuleNameHere",
Py_file_input, main_namespace.ptr(), main_namespace.ptr()));
handle<> unused2(PyRun_String("f = file('test_output', 'w')",
Py_file_input, main_namespace.ptr(), main_namespace.ptr()));
You need to name the handle<> objects, even though you don't use them.
If not, while you may think you're creating an anonymous temporary
that will immediately get destructed, the compiler thinks you're
declaring a function and gets very confused.
Hope this helps. As usual, I invoke the great spirit of Dave Abrahams
to correct/clarify anything I've said that's not quite right or
misleading. Good luck
--
-- Graeme
graeme.lufkin at gmail.com
More information about the Cplusplus-sig
mailing list