gpt4 book ai didi

python - 使用调试版本时,处理 C 扩展的 "_d"后缀的最佳方法是什么?

转载 作者:太空狗 更新时间:2023-10-29 17:50:34 26 4
gpt4 key购买 nike

我正在尝试为 Python 2.7 调试我的 C 扩展。我使用 python2.7 调试版本。我用 setuptools 构建我的项目,我的 setup.py 有这样几行:

ext_modules=[Extension("my.extension",
["my/_extension.c"])]

当我出于某种原因调用 python setup.py install 时,扩展编译为带有 _d 后缀的文件,之后,在 Python 中,我无法执行 import my.extension,我只能做import my.extension_d。我得到的是:

Traceback (most recent call last):
File "<string>", line 1, in <module>
File "build/bdist.linux-x86_64/egg/my/extension_d.py", line 7, in <module>
File "build/bdist.linux-x86_64/egg/my/extension_d.py", line 6, in __bootstrap__
ImportError: dynamic module does not define init function (initextension_d)

当然我的扩展没有inittextension_d,它只有inittextension功能。

这很不方便,因为我必须更改一些代码并将此 _d 后缀添加到导入和其他内容中。

是否可以关闭此后缀的前置?或者如何以其他方式处理该问题?也许有一些“官方”方式?

更新 #0

我使用 Ubuntu Linux。

最佳答案

构建 C 扩展时只需禁用 Debug模式。或者,如果您想保留调试信息,请暂时禁用 _DEBUG 宏:

#ifdef _DEBUG
# ifndef BOOST_DEBUG_PYTHON
# ifdef _MSC_VER
// VC8.0 will complain if system headers are #included both with
// and without _DEBUG defined, so we have to #include all the
// system headers used by pyconfig.h right here.
# include <stddef.h>
# include <stdarg.h>
# include <stdio.h>
# include <stdlib.h>
# include <assert.h>
# include <errno.h>
# include <ctype.h>
# include <wchar.h>
# include <basetsd.h>
# include <io.h>
# include <limits.h>
# include <float.h>
# include <string.h>
# include <math.h>
# include <time.h>
# endif
# undef _DEBUG // Don't let Python force the debug library just because we're debugging.
# define DEBUG_WRAP_PYTHON_H
# endif
#endif

#include <Python.h>

#ifdef DEBUG_WRAP_PYTHON_H
# define _DEBUG
#endif

有关完整代码示例,您可以查看 full version boost.python 如何包含 python.h

关于python - 使用调试版本时,处理 C 扩展的 "_d"后缀的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29554266/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com