Cython 正确性
Cython 生成的代码是否始终与生成它的 Python 代码一样正确?
它可能会帮助其他读者解决 Cython 静态类型声明和其他 Cython 功能(如果有)的使用,尽管我只对通过将 Python 模块重命名为 *.pyx 来创建 Cython 文件的情况感兴趣。
我只关心 Cython 涵盖的 Python 子集。
Is code produced by Cython always just as correct as the Python code it was produced from?
It may help other readers to address the use of Cython static type declarations and other Cython features (if any), though I am only interested in the case of creating Cython files by renaming the Python modules to *.pyx.
I only care about the subset of Python covered by Cython.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说,是的。当然,还有bug(尽管许多都围绕扩展支持的Python子集,但实际上这些bug使生成的 C 代码不正确的情况相对较少),并且有一些必要的警告 (尽管看起来这个简短列表中只有一项偏离了 Python 语义)。
当您在纯 Python 模式下添加静态类型时,在某些情况下,无论是编译为 Cython 还是作为 Python 代码运行,都会有所不同。我想到的是无符号整数(例如:如上面的一个链接中所述,如果
n
是无符号且range(-n, n)<,则
-n
将环绕。 /code> 因此为空),以及整数溢出(Python 将int
s(Clong
s)提升为long
s(abritary-精度整数))。Generally, yes. Of course there are bugs (many revolve around expanding the supported Python subset though, bugs that actually make generated C code incorrect are relatively rare), and there are a few necessary caveats (although it seems that only one item on that short list diverts from Python semantics).
When you add static types in pure Python mode, there are circumstances where it makes a difference whether it's compiled as Cython or run as Python code. Unsigned integers come to mind (example: as mentioned in one link above,
-n
will wrap around ifn
is unsigned andrange(-n, n)
is hence empty), as well as integer overflow (Python promotesint
s (Clong
s) tolong
s (abritary-precision integers)).