PyDateTime_IMPORT 宏未初始化 PyDateTimeAPI 变量
我在 Windows 上使用 Visual Studio 2008 使用 Python C API。当我尝试使用 PyDate_Check
宏和其他相关宏时,它们会导致访问冲突,因为静态变量 PyDateTimeAPI 为 null。该变量使用 PyDateTime_IMPORT
宏进行初始化,在使用任何日期时间宏之前需要调用该宏。我在单独的线程上创建新的 Python 子解释器时执行此操作。
几个问题:
- 为什么PyDateTime_IMPORT
宏中的PyCObject_Import
函数返回null。我知道返回空值是因为找不到模块。但是datetime模块怎么会找不到呢?是否是因为子解释器中的 sys.path 不正确? - 另外,我是否在正确的位置调用 PyDateTime_IMPORT
宏,应该是在子解释器初始化之后,还是在 Python 解释器初始化时调用?
PyDateTime_IMPORT
定义:
#define PyDateTime_IMPORT \
PyDateTimeAPI = (PyDateTime_CAPI*) PyCObject_Import("datetime", \
"datetime_CAPI")`
I'm using the Python C API on Windows using Visual Studio 2008. When I attempt to use the PyDate_Check
macro, and other related macros, they cause an access violation because the static variable PyDateTimeAPI is null. This variable is initialized using the PyDateTime_IMPORT
macro which needs calling before using any date time macros. I do this when creating a new Python sub-interpreter on a separate thread.
Couple of questions:
- Why is the the PyCObject_Import
function in the PyDateTime_IMPORT
macro returning null. I understand a null return value is because the module cannot be found. But how can the datetime module not being found? Could it be because of an incorrect sys.path in the sub-interpreter?
- Also, am I calling PyDateTime_IMPORT
macro in the correct place, should it be just after the sub-interpreter is initialized, or when the Python interpreter is initialized?
PyDateTime_IMPORT
definition:
#define PyDateTime_IMPORT \
PyDateTimeAPI = (PyDateTime_CAPI*) PyCObject_Import("datetime", \
"datetime_CAPI")`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用 G++ 和 Python 3.2 遇到了同样的问题。由于 PyDateTimeAPI 在标头中声明,因此包含该标头的每个文件都会获得自己的变量版本。
I ran into this same problem using G++ and Python 3.2. It has something to do that since PyDateTimeAPI is declared in the header, each file that includes that header gets its own version of the variable.
PyCObject 在 2.7 中已弃用,并在 3.x 中被删除。应使用 PyCapsule_Import() 而不是 PyCObject_Import()
PyCObject is deprecated in 2.7 and removed in 3.x. PyCapsule_Import() should be used instead of PyCObject_Import()