如何在 Windows 上调试 Python 的 C 扩展

2023-08-29Python开发问题
18

本文介绍了如何在 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.

  1. 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)

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

  3. 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.

  4. 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.

  5. 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 needs Use Unicode Character Set.

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

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

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

  7. 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!

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

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

pandas 有从特定日期开始的按月分组的方式吗?
Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)...
2024-08-22 Python开发问题
10

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10