在将类用Cython编译时出现空声明错误?

3

我正在尝试使用Cython编译以下.pyx文件:

import collections

nil = object()  # used to distinguish from None

class TrieNode(object):
    __slots__ = ['char', 'output', 'fail', 'children']
    def __init__(self, char):
        self.char = char
        self.output = nil
        self.fail = nil
        self.children = {}

    def __repr__(self):
        if self.output is not nil:
            return "<TrieNode '%s' '%s'>" % (self.char, self.output)
        else:
            return "<TrieNode '%s'>" % self.char

而 Cython 抛出了这个错误:

running build_ext
cythoning TrieNode.pyx to TrieNode.c

Error compiling Cython file:
------------------------------------------------------------
...

nil = object()  # used to distinguish from None

class TrieNode(object):
        __slots__ = ['char', 'output', 'fail', 'children']
        def __init__(self, char):
                       ^
------------------------------------------------------------

TrieNode.pyx:7:24: Empty declarator

building 'TrieNode' extension
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\BIN\amd64\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -IE:\Python26\include -IE:\Python26\PC /Tc
TrieNode.c /Fobuild\temp.win-amd64-2.6\Release\TrieNode.obj
TrieNode.c
TrieNode.c(1) : fatal error C1189: #error :  Do not use this file, it is the result of a failed Cython compilation.
error: command 'cl.exe' failed with exit status 2

我的 setup.py 目前看起来是这样的:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

setup(
    cmdclass = {'build_ext': build_ext},
    ext_modules = [Extension("TrieNode", ["TrieNode.pyx"])]
)

我看过一个例子,其中一个Python类被编译成了Cython文件而没有出现问题,但是这个似乎不起作用。有人可以告诉我我错过了什么吗?


@eryksun:很奇怪,我正在使用相同的版本。:( 你也是在使用64位机器吗? - Legend
1
@eryksun:牛逼!我把“char”改成了“_char”,然后它就成功编译了。你能否把这个作为答案发布,这样我就可以接受它了吗? - Legend
1个回答

8
在你的__init__方法中,有一个名为char的变量。如果你将一个.py模块进行Cython编译,这是可以的。然而,在Cython .pyx文件中,即使是Pythondef函数也可以在参数中有C类型声明。请参考Cython官方文档

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接