views:

265

answers:

1

Hi,

Python is 'hanging' when I try to import a c++ shared library into the windows version of python 2.5 and I have no clue why.

On Linux, everything works fine. We can compile all of our C++ code, generate swig wrapper classes. They compile and can be imported and used in either python 2.5 or 2.6. Now, we are trying to port the code to Windows using Cygwin.

We are able to compile each of the C++ libraries to shared dlls using -mno-cygwin, which removes the dependency on cygwin1.dll. Essentially this causes the gcc target to be MinGW instead of Cygwin, enabling the resulting binaries to be run in Windows without any dependency on Cygwin. Moreover, each of these shared libraries can be linked into c++ binaries and run successfully.

Once this one done, we used swig to generate wrappers for each of the shared libraries. These wrappers are generated, compiled, and linked without problem.

The next step then, was to import the generated python wrapper into python. We are able to import all but two of our libraries. For the two that do not work, when we try to import either the .py or .pyd files into Windows python (the version compiled with Visual C++), python hangs. We cannot kill python with ctrl+c or ctrl+d, the only recourse is to kill it via task manager. If we attach gdb to the python process and print a stack trace, we mostly get garbage, nothing useful.

Next, we tried ifdef'ing out blocks of code in the *.i files and recreating the swig wrappers. This process at least allowed me to import the libraries into Windows python, but the problem is we had to comment out too many functions which are necessary for the software to run. In general, there were three types of functions that had to be commented out: static functions, virtual const functions, and regular public functions that were NOT declared as const. This is reproducible too, if we uncomment any one of these functions, then the import hangs again.

Next, we tried extracting the functions into a simple hello world program, generate a swig wrapper and import them into python. This worked. We copied functions exactly from the header files. They work in the very small test program, but not in the larger shared library. We are building them the exact same way.

So, any ideas about why this is happening or even just better debugging techniques would be very helpful.

These work fine on Linux with gcc 3 and 4 and python 2.5 and 2.6. On Windows, this is the software I am using: gcc 3.4.4 swig 1.39 (Windows binaries from swig.org) python 2.5.4 (Windows binaries and includes/libs from python.org)

These are the commands I am using for building the simple hello world program (the full library uses the same options, it's just a lot longer because of extra -I, -L, and -l options)

swig -c++ -python -o test_wrap.cc test.i

gcc -c -mno-cygwin test.cc

gcc -c -mno-cygwin test_wrap.cc -I/usr/python25/include

dlltool --export-all --output-def _test.def test.o

gcc -mno-cygwin -shared -s test_wrap.o test.o -L/usr/python25/libs -lpython25 -lstdc++ -o _TestModule.pyd

Thanks, AJ

+1  A: 

A technique I've used is to insert a "hard" breakpoint (__asm int 3) in the module init function. Then either run it through a debugger or just run it and let the windows debugger pop when the interrupt is called.

You can download a nice windows debugger from Microsoft here.

lazy1