[C++-sig] Re: Calling a python function from C++

Jeff Holle jeff.holle at verizon.net
Tue Jun 29 04:30:50 CEST 2004


Dave wrote:

Why struggle?
This should be:

object    /* Returns a borrowed reference */
getPythonFunction(string moduleName,string functionName)
{
    object _module(handle<>( PyImport_ImportModule(const_cast<char*>(moduleName.c_str())))); <<-- line 63 in Main.cpp

    return _module.attr(functionName.c_str());
}

------------------------------------------------------------------------

Always a good question.
The code doesn't compile as presented.
I added #include <boost/python/object.hpp> to my module, have completely opened the boost::python namespace.

Get the error:

    Main.cpp:63: error: variable declaration is not allowed here


If I just instanciate an empty object with its default constructor, the module compiles.

An underlining concern I have is error handling.  When PyImport_ImportModule fails, a NULL pointer is returned.
Seems like the underlining machinery in "handle<>" is intolerant of this.







More information about the Cplusplus-sig mailing list