python - cythoning 类时出现空声明符错误?

标签 python compiler-construction compiler-errors cython

我正在尝试使用 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 文件,但这个示例似乎不起作用。有人可以告诉我我缺少什么吗?

最佳答案

在您的__init__ 方法中,您有一个名为char 的变量。如果你 Cythonize 一个 .py 模块,这很好。但是,在 Cython .pyx 文件中,即使是 Python def 函数也可以有 C type declarations in the parameters .

关于python - cythoning 类时出现空声明符错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8089412/

相关文章:

c# - Invoke调用中的匿名方法

python - 如何在 Windows 上打开包含选定项目的文件夹

python - Pandas bool 值 .any() .all()

python - 如何在python中读取unicode文件(不是UTF-8)

python - 为什么我的解析器无法正确理解 “numb=”?

C 错误 : expression must have a constant value

python - 如何为自己的数据实现tensorflow的next_batch

java - 无法访问的代码编译器错误

c - 有没有办法让 gcc 或 clang 对显式强制转换发出警告?

c++ - 奇怪的编译错误 C++ "undefined reference"& "relocation truncated"