gpt4 book ai didi

python - Cython 错误 : declaration does not declare anything

转载 作者:行者123 更新时间:2023-11-28 02:37:04 27 4
gpt4 key购买 nike

我正在编写一些 cython 代码,但遇到了一个奇怪的问题。当我尝试将对象作为结构直接从 python 传递到 C 时,cython 生成的代码很好,但 gcc 不喜欢代码输出并给我以下错误:error: declaration does not declare anything。这是我的测试代码:

// cake.h
using Cake = struct CakeStruct {
int a, b, c;
};

void bake(Cake batter);

和赛通:

# test.pyx

cdef extern from "cake.h":
void bake(Cake batter)
ctypedef struct Cake:
int a
int b
int c

def make_one(batter):
cdef Cake more_batter;
more_batter.a = 5
more_batter.b = 10
print(more_batter.a + more_batter.b)

bake(more_batter)
bake(batter) # <- this line generates bad code

如果您查看生成的代码,错误行如下所示:

...

Cake; // this is the error
static Cake __pyx_convert__from_py_Cake(PyObject *);

...

我使用的是直接来自 Anaconda 的 cython 0.21 和 Ubuntu 14.04 附带的 gcc 4.8.2。 Cython 代码是使用 cython --cplus test.pyx 生成的,语法检查由:

gcc -std=c++11 -fsyntax-only -I`...python include dir...` test.cpp

--

任何人都可以告诉我我在 .pyx 文件中做错了什么吗?或者这是我绊倒的 cython 错误?

最佳答案

是的,我认为你是对的,这是 Cython 0.21 中的错误。首先,使用 Cython 0.20 进行测试(因为这是我的 linux 发行版发布的),它给了我

cake.pyx:16:15: Cannot convert Python object to 'Cake'

我想这是因为转换功能在 0.20 中缺失或不完整,尽管我在发行说明中找不到任何关于此的信息 (https://github.com/cython/cython/blob/master/CHANGES.rst)。

接下来,我使用 Github 存储库 (https://github.com/cython/cython) 中的主分支进行了测试,这非常有效,您提供的 cythongcc 命令没有出现任何错误。使用 0.21 版本,我可以重现您看到的错误。

运行 git bisect 显示错误似乎已在提交 fd56551 中修复.这是在 0.21.1(目前最新版本)之后,所以升级到这个版本不会修复这个错误。看来您将不得不使用开发分支或等待下一个 Cython 版本。

关于python - Cython 错误 : declaration does not declare anything,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27088460/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com