Python Cython和regex.h
我对Cython比较陌生,所以如果这个问题看起来很基本,我深表歉意 有一个可并行化的regex匹配块,我想用Cython和Python Cython和regex.h,python,regex,cython,Python,Regex,Cython,我对Cython比较陌生,所以如果这个问题看起来很基本,我深表歉意 有一个可并行化的regex匹配块,我想用Cython和nogil运行它。为了避免使用Python对象,我的计划是导入regex.h 编译以下导入段: cdef extern from "regex.h" nogil: ctypedef struct regoff_t ctypedef struct regex_t ctypedef struct regmatch_t int regcomp(regex_t
nogil
运行它。为了避免使用Python对象,我的计划是导入regex.h
编译以下导入段:
cdef extern from "regex.h" nogil:
ctypedef struct regoff_t
ctypedef struct regex_t
ctypedef struct regmatch_t
int regcomp(regex_t* preg, const char* regex, int cflags)
int regexec(const regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags)
def matchPatterns(str pageContent, str regex):
cdef set matchingPatterns = set()
return matchingPatterns
但只要我使用regex\u t
或它的任何函数,我就会得到错误:contentMatchPatternCython.pyx:10:16:变量类型“regex\u t”不完整
如果删除空的ctypedef
s,代码不会编译为regex\u t
未定义。显然,我认为/希望在不重复Cython中的结构定义的情况下有一条前进的道路
我使用的是Python 2.7.2和Cython 0.22。任何指点都会受到感激。
要直接引用文档,请执行以下操作:
如果头文件声明了一个大结构,而您只想使用
少数成员,您只需声明您感兴趣的成员
在里面忽略其余部分不会造成任何伤害,因为C编译器
将使用头文件中的完整定义
在某些情况下,您可能不需要任何结构的成员
在这种情况下,您可以将pass放在结构体中
声明,例如:
使用哪一个取决于您是否匹配读取
struct A{}
或typedef struct{}B
的C代码是的,我知道文档的这一部分。但是,regex\u t
不是我直接使用的,而是我导入的API使用的,因此没有限制数据成员列表的选项。显然,可以选择手动复制结构的完整定义,但这看起来很麻烦且无法维护。Cython难道没有办法自动导入包含所有成员的结构定义吗?您只需添加pass
语句,它就可以工作了-您自己不使用成员,因此Cython不需要了解它们。
cdef extern from "foo.h":
struct A:
pass
# or (I've added this bit - not in the documentation directly...)
ctypedef struct B:
pass