How to debug C extensions for Python on Windows(如何在 Windows 上调试 Python 的 C 扩展)
问题描述
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?
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 -
Compiling python modules whith DEBUG defined on MSVC
Here is my version of the steps for anyone else who may encounter this problem.
In case you haven't already, be sure to setup the Python header and libs directories in VS
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:Python27include
,C:Python27libs
)Go to your Python include folder (once again, e.g.
C:Python27include
) and editpyconfig.h
. Comment out the line# define Py_DEBUG
and save. Go to your libs folder (e.g.C:Python27libs
) and make a copy ofpython27.lib
. Name the copypython27_d.lib
.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.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 -
a. General - ensure that you are using the correct Character Set. For me it was
Use Multi-Byte Character Set
. Python 3 probably needsUse Unicode Character Set
.b. Debugging - enter the path to Python in the Command field. (e.g.
C:Python27python.exe
). Then set Attach toYes
.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.
Open a cmd and
cd
into the directory where yourpyd
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!
这篇关于如何在 Windows 上调试 Python 的 C 扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Windows 上调试 Python 的 C 扩展


基础教程推荐
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 筛选NumPy数组 2022-01-01