I'm having trouble getting a simple boost::python sample program to
work properly on Linux. It compiles and seems to link properly, but the Python
interpreter is unable to call my C++ function. On Mac OS X the
program works as expected.
Any help would be appreciated, hopefully I'm missing something obvious.
The program output is:
-- BEGIN --
Traceback (most recent call last):
File "<string>", line 1, in <module>
Boost.Python.ArgumentError: Python argument types in
Test.test(int)
did not match C++ signature:
test(int)
-- END --
Here is the code. It's compiled and linked using "g++
-I/usr/include/python2.5 -lpython2.5 -lboost_python -Wall -o test
test.cpp". On the Linux box I'm using boost-1.37.0 and python-2.5.2.
-- BEGIN --
#include <stdlib.h>
#include <string>
#include <boost/python.hpp>
using namespace boost::python;
/* g++ -I/usr/include/python2.5 -lpython2.5 -lboost_python -Wall -o
test test.cpp */
int test(int i)
{
fprintf(stderr, "%s:\n", __FUNCTION__);
return i * 5;
}
BOOST_PYTHON_MODULE(Test)
{
using namespace boost::python;
def("test", test);
}
int main(int argc, char *argv[])
{
Py_Initialize();
try {
initTest();
PyRun_SimpleString("import Test");
PyRun_SimpleString("print 'calling test function'");
PyRun_SimpleString("print Test.test(5)");
} catch (boost::python::error_already_set) {
PyErr_Print();
}
Py_Finalize();
return 0;
}
-- END --
On 06/23/2009 02:45 PM, Jim Treadway wrote: > I'm having trouble getting a simple boost::python sample program to > work properly on Linux. It compiles and seems to link properly, but the Python > interpreter is unable to call my C++ function. On Mac OS X the > program works as expected. > > Any help would be appreciated, hopefully I'm missing something obvious. >Let's see: I expect the problem to be that the Python interpreter doesn't 'see' the 'Test' module. While normally this would just result in an import error, here it may just happen to find a different 'Test' module, which, as it doesn't match what you expect, raises an ArgumentError.> -- BEGIN -- > #include<stdlib.h> > #include<string> > #include<boost/python.hpp> > > using namespace boost::python; > > /* g++ -I/usr/include/python2.5 -lpython2.5 -lboost_python -Wall -o > test test.cpp */ > > int test(int i) > { > fprintf(stderr, "%s:\n", __FUNCTION__); > return i * 5; > } > > BOOST_PYTHON_MODULE(Test) > { > using namespace boost::python; > def("test", test); > } > > int main(int argc, char *argv[]) > { > Py_Initialize(); > > try { > initTest(); > >I'm actually not sure what the effect of calling 'initTest()' is. I believe you should call: PyImport_AppendInittab(const_cast<char*>("Test"), initTest);> PyRun_SimpleString("import Test"); > PyRun_SimpleString("print 'calling test function'"); > PyRun_SimpleString("print Test.test(5)"); > } catch (boost::python::error_already_set) { > PyErr_Print(); > } > > Py_Finalize(); > return 0; > } > -- END -- >HTH, Stefan
On Tue, Jun 23, 2009 at 12:06 PM, Stefan Seefeld wrote: > On 06/23/2009 02:45 PM, Jim Treadway wrote: > >> I'm having trouble getting a simple boost::python sample program to >> work properly on Linux. It compiles and seems to link properly, but the >> Python >> interpreter is unable to call my C++ function. On Mac OS X the >> program works as expected. >> >> Any help would be appreciated, hopefully I'm missing something obvious. >> >> > > > Let's see: > > I expect the problem to be that the Python interpreter doesn't 'see' the > 'Test' module. While normally this would just result in an import error, > here it may just happen to find a different 'Test' module, which, as it > doesn't match what you expect, raises an ArgumentError. >Renaming everything from test to "MyUniqueTest" produces the same effective results. -- BEGIN --> #include<stdlib.h> > #include<string> > #include<boost/python.hpp> > > using namespace boost::python; > > /* g++ -I/usr/include/python2.5 -lpython2.5 -lboost_python -Wall -o > test test.cpp */ > > int test(int i) > { > fprintf(stderr, "%s:\n", __FUNCTION__); > return i * 5; > } > > BOOST_PYTHON_MODULE(Test) > { > using namespace boost::python; > def("test", test); > } > > int main(int argc, char *argv[]) > { > Py_Initialize(); > > try { > initTest(); >I'm actually not sure what the effect of calling 'initTest()' is.> I believe you should call: > > PyImport_AppendInittab(const_cast<char*>("Test"), initTest);My understanding is that PyImport_AppendInittab makes it so that a Python "import" statement will call your initialization function so that you do not have to call "initTest" (for example) from the C++ code. Nevertheless, using PyImport_AppendInittab instead, in both the location you recommend as well as before the call to Py_Initialize() produces no change in the results. PyRun_SimpleString("import Test");> PyRun_SimpleString("print 'calling test function'"); > PyRun_SimpleString("print Test.test(5)"); > } catch (boost::python::error_already_set) { > PyErr_Print(); > } > > Py_Finalize(); > return 0; > } > -- END --I've also tried: PyRun_SimpleString("import sys, dl"); PyRun_SimpleString("sys.setdlopenflags(dl.RTLD_NOW | dl.RTLD_GLOBAL); before the "import Test" part, but that doesn't change the result either. Jim >