Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Cython和regex.h_Python_Regex_Cython - Fatal编程技术网

Python Cython和regex.h

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

我对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* 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