Cython 和 regex.h

Cython and regex.h

我对 Cython 比较陌生,如果这个问题看起来很基础,我深表歉意。

有一个可并行的正则表达式匹配块,我想 运行 它与 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_t 或其任何功能,我就会收到错误消息:contentMatchPatternCython.pyx:10:16: Variable type 'regex_t' is incomplete

如果我删除空的 ctypedefs,代码不会编译,因为 regex_t 是未定义的。显然,我 think/hope 有一种方法可以在不复制 Cython 中的结构定义的情况下前进。

我正在使用 Python 2.7.2 和 Cython 0.22。如有任何指点,我们将不胜感激。

http://docs.cython.org/src/userguide/external_C_code.html

直接引用文档:

If the header file declares a big struct and you only want to use a few members, you only need to declare the members you’re interested in. Leaving the rest out doesn’t do any harm, because the C compiler will use the full definition from the header file.

In some cases, you might not need any of the struct’s members, in which case you can just put pass in the body of the struct declaration, e.g.:

cdef extern from "foo.h":
    struct A:
        pass
    # or (I've added this bit - not in the documentation directly...)
    ctypedef struct B:
        pass

使用哪一个取决于您匹配的 C 代码是 struct A {} 还是 typedef struct {} B