Cython correctness
Asked Answered
L

1

7

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.

Lindon answered 21/3, 2011 at 18:34 Comment(0)
D
10

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 if n is unsigned and range(-n, n) is hence empty), as well as integer overflow (Python promotes ints (C longs) to longs (abritary-precision integers)).

Disservice answered 21/3, 2011 at 20:45 Comment(3)
Thanks for your answer. It would be most reassuring to hear from an expert; do you work on the Cython project? BTW, I enjoy the Cython project; it provides functionality for me that no other tool I've found does.Lindon
@Brian: No, I'm nowhere awesome enough to be involved in Cython ;) I'm just another user and student of the documentation. If you want to ask the devs, at least some of them participate in the cython-users mailing list.Disservice
I'm a Cython dev, and can say that any regression from Python would be considered a serious bug, though the caveats linked (most notably using explicitly C typed integers) should be kept in mind.Togetherness

© 2022 - 2024 — McMap. All rights reserved.