簡體   English   中英

如何在編譯時從C擴展模塊檢查python API的版本?

[英]How to check the version of the python API at compile time from a C extension module?

我在C中編寫了一個python模塊。需要為python版本2.4,2.5,2.6和2.7編譯模塊。

現在我遇到了問題,在python 2.5中他們為列表的大小定義了Py_ssize_t ,但是在2.4中他們只使用了int

所以我的問題是:有沒有一種簡單的方法來檢查我是否在編譯時使用版本2.4或2.5的API,所以我可以編寫一個小宏?

例如:

#if PY_MINOR < 5
typedef int Py_ssize_t;
#endif

是的,Python中的patchlevel.h包括dir定義了你要找的東西:

#define PY_MAJOR_VERSION    2
#define PY_MINOR_VERSION    5
#define PY_MICRO_VERSION    2

我認為你需要的是PY_VERSION_HEX

cython生成的c代碼中有一行

PY_VERSION_HEX < 0x02040000

#ifndef Py_PYTHON_H
  #error Python headers needed to compile C extensions, please install development version of Python.
#elif PY_VERSION_HEX < 0x02040000
  #error Cython requires Python 2.4+.
#else

做一些與此類似的事情。

Import sys
if sys.version_info < (2, 4): //do something, typedef what you need
else // so on

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM