简体   繁体   English

导入Swig生成的包装器时Python挂起

[英]Python Hangs When Importing Swig Generated Wrapper

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. 当我尝试将c ++共享库导入到python 2.5的windows版本时,Python正在“悬挂”,我不知道为什么。

On Linux, everything works fine. 在Linux上,一切正常。 We can compile all of our C++ code, generate swig wrapper classes. 我们可以编译所有的C ++代码,生成swig包装类。 They compile and can be imported and used in either python 2.5 or 2.6. 它们编译并可以在python 2.5或2.6中导入和使用。 Now, we are trying to port the code to Windows using Cygwin. 现在,我们正在尝试使用Cygwin将代码移植到Windows。

We are able to compile each of the C++ libraries to shared dlls using -mno-cygwin, which removes the dependency on cygwin1.dll. 我们可以使用-mno-cygwin将每个C ++库编译为共享dll,从而消除对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. 从本质上讲,这会导致gcc目标为MinGW而不是Cygwin,从而使得生成的二进制文件可以在Windows中运行而不依赖于Cygwin。 Moreover, each of these shared libraries can be linked into c++ binaries and run successfully. 而且,每个共享库都可以链接到c ++二进制文件并成功运行。

Once this one done, we used swig to generate wrappers for each of the shared libraries. 完成此操作后,我们使用swig为每个共享库生成包装器。 These wrappers are generated, compiled, and linked without problem. 生成,编译和链接这些包装器没有问题。

The next step then, was to import the generated python wrapper into python. 接下来的步骤是将生成的python包装器导入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. 对于两个不起作用的,当我们尝试将.py或.pyd文件导入Windows python(使用Visual C ++编译的版本)时,python会挂起。 We cannot kill python with ctrl+c or ctrl+d, the only recourse is to kill it via task manager. 我们不能用ctrl + c或ctrl + d杀死python,唯一的办法是通过任务管理器杀死它。 If we attach gdb to the python process and print a stack trace, we mostly get garbage, nothing useful. 如果我们将gdb附加到python进程并打印堆栈跟踪,我们通常会得到垃圾,没什么用处。

Next, we tried ifdef'ing out blocks of code in the *.i files and recreating the swig wrappers. 接下来,我们尝试ifdef出* .i文件中的代码块并重新创建swig包装器。 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. 这个过程至少允许我将库导入到Windows python中,但问题是我们必须注释掉运行软件所需的太多函数。 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. 通常,有三种类型的函数必须被注释掉:静态函数,虚拟const函数和未声明为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. 接下来,我们尝试将函数提取到一个简单的hello world程序中,生成一个swig包装器并将它们导入到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. 这些在使用gcc 3和4以及python 2.5和2.6的Linux上运行良好。 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) 在Windows上,这是我正在使用的软件:gcc 3.4.4 swig 1.39(来自swig.org的Windows二进制文件)python 2.5.4(Windows二进制文件和来自python.org的/ libs)

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) 这些是我用来构建简单的hello world程序的命令(完整的库使用相同的选项,因为额外的-I,-L和-l选项,它只是更长的时间)

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

gcc -c -mno-cygwin test.cc gcc -c -mno-cygwin test.cc

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

dlltool --export-all --output-def _test.def test.o 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 gcc -mno-cygwin -shared -s test_wrap.o test.o -L / usr / python25 / libs -lpython25 -lstdc ++ -o _TestModule.pyd

Thanks, AJ 谢谢,AJ

A technique I've used is to insert a "hard" breakpoint ( __asm int 3 ) in the module init function. 我使用的一种技术是在模块init函数中插入一个“硬”断点( __asm int 3 )。 Then either run it through a debugger or just run it and let the windows debugger pop when the interrupt is called. 然后通过调试器运行它或者只运行它,并在调用中断时弹出Windows调试器。

You can download a nice windows debugger from Microsoft here . 你可以在这里下载一个很好的Windows调试器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM