[C++-sig] Re: How to access a custom module from embedded Python?

Hanz Meizer steam at nurfuerspam.de
Fri Apr 16 12:54:40 CEST 2004


Pierre Barbier de Reuille wrote:
> Hello !
> 
> I had the same problem and found that if you call :
> 
>    PyImport_AppendInittab(mname, initmodule);
> 
> before the Py_Initialize(), with mname your module name and initmodule
> the initialisation function, it just works fine.
> 
> Pierre

Hi,

I currently have to embed python inside an application as well. This 
embedded python needs to be able to use a library I've wrapped with 
boost (it's a shared object called pymoses.so). Unfortunately I get a 
segmentation fault when trying to import this library inside of my 
embedded python. Anyone knows what's going wrong here? Code follows:

embed.cpp----------------------------------------------------------------
#include <Python.h>
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <fcntl.h>
#include <boost/python/handle_fwd.hpp>
#include <boost/python/handle.hpp>
#include <boost/python.hpp>

// maybe the error is here?
extern void (*initpymoses)();

using namespace boost::python;

char* read_file(char *fname) {
   ...
}

int main(int argc, char *argv[])
{
   if (argc <= 1) return 0;
   auto_ptr<char> data(read_file(argv[1]));

   // add pymoses.so to inittab
   if (PyImport_AppendInittab("pymoses", initpymoses) < 0)
   {
     cerr << "can't PyImport_AppendInittab" << endl;
     exit(1);
   }

   Py_Initialize();

   handle<> h_main_module( borrowed( PyImport_AddModule("__main__") ) );
   handle<> h_main_namespace( borrowed( PyModule_GetDict( 
h_main_module.get() ) ) );

   handle<> tmp=handle<>( allow_null( PyRun_String(data.get(), 
Py_file_input, h_main_namespace.get(), h_main_namespace.get()) ) );

   if (!tmp)
   {
     cerr << "Error while running input file" << endl;
     PyErr_Print();
     exit(1);
   }

   Py_Finalize();

   return 0;
}
--------------------------------------------------------------------------

Compilation:
g++ -I. -Wl,-E -I/usr/include/python2.2 -L/usr/lib/python2.2 
-L/usr/lib/python2.2/config -I/usr/local/include/boost-1_31 
-L/usr/local/lib pymoses.so -lboost_python-gcc -lpython2.2 -ldl 
-lpthread -lutil embed.cpp -o embed

Running this example works with simple python stuff:
test1.py------------------------------------------------------------------
def fib(n):
         if n < 2: return 1
         return fib(n-2)+fib(n-1)

print fib(10)
--------------------------------------------------------------------------
 > ./embed test1.py
89

Using a script that import pymoses gives me a segfault, tho:
test2.py------------------------------------------------------------------
import pymoses

print dir()
print dir(pymoses)
--------------------------------------------------------------------------

Can anyone give me some advice as to what am I doing wrong here? Maybe I 
can't simply link the shared object pymoses.so into my executable? Do I 
have to use dlopen to add it to my program? Debugging advices? Any help 
is welcome :)

Greetings,
H.






More information about the Cplusplus-sig mailing list