Conditional `ctypedef` with Cython

I need access to the uint64_t typedef from stdint.h in some shell code that I am writing and I cannot figure out how to do this. The problem is that from what I can say from the docs, my ctypedef should take the form:

 ctypedef unsigned long uint64_t 

or

 ctypedef unsigned long long uint64_t 

depending on if WORDSIZE from bits/wordsize.h is 64 or 32. I was not able to figure out how to access this preprocessor definition from Cython, and if I could, Cython did not seem to like ctypedef in if , and when I try to put the if in the cdef block, it seems to confuse it with the declaration. Any ideas? Hope I just missed something really basic.

+6
c python cython
source share
2 answers
 cdef extern from "stdint.h": ctypedef unsigned long long uint64_t 

Any ctypedef that extern 'd will not have the typedef generated in the .c file. Cython will include stdint.h , and your C compiler will use the actual typedef from there.

The only thing that matters to the type is when cython generates code that automatically converts C types and Python types. Using unsigned long long means that Cython will use PyLong_FromUnsignedLongLong and PyLong_AsLongLongAndOverflow . That way, you hopefully don't get any truncation when converting.

+9
source share

Cython already contains these definitions in the libc.stdint module:

 from libc cimport stdint ctypedef stdint.uint64_t foo 
+5
source share

All Articles