Cython和regex.h

2024-04-24 02:58:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我对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_t或它的任何函数,我就会得到一个错误:contentMatchPatternCython.pyx:10:16: Variable type 'regex_t' is incomplete

如果我删除空的ctypedef,代码不会编译为regex_t未定义。显然,我认为/希望有一种方法可以避免重复Cython中的结构定义。在

我使用的是python2.7.2和cython0.22。如有任何建议,我们将不胜感激。在


Tags: structcythonregexintsetcharcdefstr
1条回答
网友
1楼 · 发布于 2024-04-24 02:58:33

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

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

相关问题 更多 >