On Thu, 1 Jul 2004 20:19:37 +0000 (UTC), Faheem Mitha <faheem@email.unc.edu> wrote:
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()));
I think this should be main_module.ptr() since main_module is an object.
In my example it's a handle<>, so get() is correct. In Dave's (and your) examples, it's an object, so ptr() is correct.
I'm trying with
****************************************************************************** #include <boost/python.hpp> using namespace boost::python;
void foo() { Py_Initialize();
object main_module = object(handle<>(borrowed(PyImport_AddModule("__main__"))));
dict main_namespace = extract<dict>(PyModule_GetDict(main_module.ptr()));
handle<> unused(PyRun_String("hello = file('hello.txt', 'w')\n" "hello.write('Hello world!')\n" "hello.close()", Py_file_input, main_namespace.ptr(),main_namespace.ptr())); Py_Finalize(); }
BOOST_PYTHON_MODULE(embed) { def("foo",foo); } *******************************************************************************
This compiles, but I get the same error as before, namely
NameError: name 'file' is not defined
Does this work correctly for you? I'm wondering if there is some issue with my compiler flags. I'm trying to get Boost.Build to work but there are some linking problems.
First, your extension module should never call Py_Initialize() or Py_Finalize(), as Python will already be initialized by the time you're running methods in it. I think what you want is foo() to be main(), with no extension module stuff needed. Also, Boost.Python and Py_Finalize() don't play nice, so never call it (check the list archive for details, I don't really understand why myself). Second, yes this compiles and runs correctly for me, after removing the Py_Initialize and Py_Finalize calls. Third, you definitely need to use Boost.Build for a few sample programs before you think about switching back to Makefiles. I share your frustration at Boost.Build sometimes, but it does a particular job admirably. I submitted some patches to the docs and the Jamfile rules a while ago that were aimed at making it easier to use Boost.Build for extensions and embedded apps. Dave hasn't gotten back to me yet. (Hint hint) -- -- Graeme graeme.lufkin@gmail.com