如何调试C扩展Windows上的Python [英] How to debug C extensions for Python on Windows

查看:178
本文介绍了如何调试C扩展Windows上的Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个<一个href=\"http://stackoverflow.com/questions/11657958/catching-a-segfault-in-python-pyodbc-cursor-fetch-causes-python-to-crash\">problem在pyodbc段错误并想调试它在Windows XP x86的。然而,网上的资料似乎主要以Linux为中心。什么是去这个问题的最好方法是什么?

I have a problem with a segfault in pyodbc and would like to debug it in Windows XP x86. However, the information online seems primarily Linux-centric. What is the best way to go about this?

推荐答案

所以,我能够通过使用Visual Studio 2008年成功地解决我的问题,我跟着松散这里列出的步骤 -

So I was able to successfully resolve my issue by using Visual Studio 2008. I loosely followed the steps listed here -

http://www.velocityreviews.com/forums/t329214-debugging-python-extensions.html

和解决方法上的一些技巧在这里 -

And some tips on workarounds here -

<一个href=\"http://stackoverflow.com/questions/1236060/compiling-python-modules-whith-debug-defined-on-msvc\">Compiling Python模块蒙山上MSVC定义DEBUG

下面是我的版本为别人的步骤谁可能遇到此问题。

Here is my version of the steps for anyone else who may encounter this problem.


  1. 如果你还没有准备好,一定要设置在VS Python的头和库目录

  1. In case you haven't already, be sure to setup the Python header and libs directories in VS

一个。转到工具>选项>项目和解决方案> VC ++目录即可。一定要加include和,分别为库路径和库文件的路径。 (例如 C:\\ Python27 \\包含 C:\\ Python27 \\库

a. Go to Tools > Options > Projects and Solutions > VC++ Directories. Be sure to add your include and libs path to the Include and Library files' path, respectively. (e.g. C:\Python27\include, C:\Python27\libs)

转到你的Python包含文件夹(再次,如 C:\\ Python27 \\包含),并修改 pyconfig.h 。注释掉行#定义Py_DEBUG 和保存。转到您的libs文件夹(例如 C:\\ Python27 \\库),并 python27.lib 的副本。命名副本 python27_d.lib

Go to your Python include folder (once again, e.g. C:\Python27\include) and edit pyconfig.h. Comment out the line # define Py_DEBUG and save. Go to your libs folder (e.g. C:\Python27\libs) and make a copy of python27.lib. Name the copy python27_d.lib.

创建一个新项目。选择 Win32项目并命名为模块名(在我的情况 pyodbc 。点击的下一步的然后选择 DLL 对于应用类型并检查空项目

Create a new project. Choose Win32 Project and name it the module name (in my case pyodbc. Click Next then choose DLL for Application type and check Empty Project.

在Solution Explorer中,头文件右键单击,然后选择添加>现有的项目。选择所有您需要的头文件。执行相同的源文件。

In the Solution Explorer, right-click on Header Files and choose Add > Existing Item. Select all of the header files that you need. Do the same for Source Files.

转到项目>属性,然后在配置属性 -

Go to Project > Properties, then under Configuration Properties -

一个。 常规 - 确保你使用的是正确的字符集即可。对我来说,使用多字节字符集。 Python 3里大概需要采用UNI code字符集

a. General - ensure that you are using the correct Character Set. For me it was Use Multi-Byte Character Set. Python 3 probably needs Use Unicode Character Set.

乙。 调试 - 在命令字段中输入路径到Python。 (例如 C:\\ Python27 \\ python.exe )。然后将连接

b. Debugging - enter the path to Python in the Command field. (e.g. C:\Python27\python.exe). Then set Attach to Yes.

℃。 链接>常规 - 更改输出文件 .pyd 而不是 .DLL

c. Linker > General - change the Output File to end in .pyd instead of .dll.

确保您的配置设置为调试。转到生成>生成解决方案

Ensure that your configuration is set to Debug. Go to Build > Build Solution.

打开CMD和 CD 到您的 PYD 文件编译的目录。从cmd窗口启动蟒蛇。要附加在这个运行过程蟒调试,回到Visual Studio和点击绿色的播放按钮开始调试。您也可以使用调试 - >附加到进程......现在回到Python和导入模块。播放,测试,并试图打破它!

Open a cmd and cd into the directory where your pyd file was compiled. Start python from the cmd window. To attach debugger on this running python process, go back to Visual Studio and click the green play button to start debugging. You can also use Debugging -> Attach to Process... Now go back to Python and import your module. Play, test, and try to break it!

这篇关于如何调试C扩展Windows上的Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆