[C++-sig] boost::python on Linux

Jim Treadway jimtreadway at gmail.com
Tue Jun 23 21:17:19 CEST 2009


On Tue, Jun 23, 2009 at 12:06 PM, Stefan Seefeld <seefeld at sympatico.ca>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

>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20090623/e19226f7/attachment.htm>


More information about the Cplusplus-sig mailing list