[C++-sig] Re: Calling a python function from C++
David Abrahams
dave at boost-consulting.com
Wed Jun 30 02:49:13 CEST 2004
Jeff Holle <jeff.holle at verizon.net> writes:
> 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.
Oh, it should; GCC bug it looks like.
object
getPythonFunction(string moduleName,string functionName)
{
object _module = object(
handle<>(
PyImport_ImportModule(const_cast<char*>(moduleName.c_str()))
)
);
return _module.attr(functionName.c_str());
}
or
object
getPythonFunction(string moduleName,string functionName)
{
object _module((
handle<>(
PyImport_ImportModule(const_cast<char*>(moduleName.c_str()))
)
));
return _module.attr(functionName.c_str());
}
works.
> 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.
What makes you say that?
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com
More information about the Cplusplus-sig
mailing list