ArchiveOrangemail archive

Development of Python/C++ integration


cplusplus-sig.python.org
(List home) (Recent threads) (90 other Python lists)

Subscription Options

  • RSS or Atom: Read-only subscription using a browser or aggregator. This is the recommended way if you don't need to send messages to the list. You can learn more about feed syndication and clients here.
  • Conventional: All messages are delivered to your mail address, and you can reply. To subscribe, send an email to the list's subscribe address with "subscribe" in the subject line, or visit the list's homepage here.
  • Low traffic list: less than 3 messages per day
  • This list contains about 16,566 messages, beginning Mar 1998
  • 3 messages added yesterday
Report the Spam
This button sends a spam report to the moderator. Please use it sparingly. For other removal requests, read this.
Are you sure? yes no

boost::python on Linux

Ad
Jim Treadway 1245782711Tue, 23 Jun 2009 18:45:11 +0000 (UTC)
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 --
Stefan Seefeld 1245783960Tue, 23 Jun 2009 19:06:00 +0000 (UTC)
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
Jim Treadway 1245784639Tue, 23 Jun 2009 19:17:19 +0000 (UTC)
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

>
Home | About | Privacy