Question

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.

Was it helpful?

Solution

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)).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top