cintegercythonportabilityfixed-width

Are exact-width integers in Cython actually platform dependent?


In Cython one can use exact-width integral types by importing them from stdint, e.g.

from libc.stdint cimport int32_t

Looking through stdint.pxd, we see that int32_t is defined as

cdef extern from "<stdint.h>" nogil:
    ...
    ctypedef signed int int32_t

Does this mean that if I use int32_t in my Cython code, this type is just an alias for signed int (int), which might in fact be only 16 bits wide?

The issue is the same for all the other integral types.


Solution

  • They should be fine.

    The typedefs that are really used come from the C stdint.h header, which are almost certainly right.

    The Cython typedef

    ctypedef signed int int32_t
    

    Is really just so that Cython understands that the type is an integer and that it's signed. It isn't what's actually used in the generated C code. Since it's in a cdef extern block it's telling Cython "a typedef like this exists" rather than acting as the real definition.