2012-09-10 43 views
6

我正在用C编写一个python模块。 这个模块需要为Python版本2.4,2.5,2.6和2.7进行编译。如何在编译时检查python API的版本?

现在我遇到了python 2.5的问题,他们为列表的大小定义了Py_ssize_t,但是在2.4中他们只是使用了int

所以我的问题是: 有没有一种简单的方法来检查我是否在编译时使用2.4或2.5的API,所以我可以写一个小宏?

e.g:

#if PY_MINOR < 5 
typedef int Py_ssize_t; 
#endif 

回答

6

是,patchlevel.h在Python包括一行目录定义了什么您正在查找:

#define PY_MAJOR_VERSION 2 
#define PY_MINOR_VERSION 5 
#define PY_MICRO_VERSION 2 
-1

只是做一些类似的。

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

我想你需要的是PY_VERSION_HEX

有C代码通过用Cython产生

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