Overview
Teaching: 20 min
Exercises: 0 minQuestions
What are the basic C types supported by Cython?
How are types, variables, and functions declared?
Objectives
Understand the structure of Cython programs.
Learn which C data types are supported.
The C language provides a number of basic data types, and many of these are available in Cython. As C is designed for system-level programming, many of the data types mape closely to the format of the data that is represented by the hardware. There are two fundamental C data types:
The following table shows the statements for introducing different integer and floating point types:
Type | Description |
---|---|
char | 8-bit signed integer |
short | 16-bit signed integer |
int | 32-bit signed integer |
long | 64-bit signed integer |
long long | 64-bit signed integer |
float | 32-bit floating point |
double | 64-bit floating point |
long double | 80-bit floating point |
C also provides a number of structured data types that can be used to build on these fundamental types. These include:
size
elements, each of type type
, is:type name[size]
type
is:type *name
struct name { declaration }
union name { declaration }
enum name { declaration }
The cdef
statement is used to declare C variables, either local or module-level:
cdef int i, j, k
cdef float f, g[42], *h
In C, types can be given names using the typedef
statement. The equivalent in Cython is ctypedef
:
ctypedef int * intPtr
Cython also supports C struct, union, or enum types:
C code | Cython code |
---|---|
|
|
|
|
|
|
|
|
There are two kinds of function definition in Cython:
def
statement, as in Python. They take Python objects as parameters and return Python objects.cdef
statement. They take either Python objects or C values as parameters, and can return either Python objects or C values.Within a Cython module, Python functions and C functions can call each other freely, but only Python functions can be called from outside the
module by interpreted Python code. So, any functions that you want to “export” from your Cython module must be declared as Python functions
using def
. There is also a hybrid function, called cpdef
, that can be called from anywhere, but uses the faster C calling conventions when
being called from other Cython code. A cpdef
can also be overridden by a Python method on a subclass or an instance attribute, even when
called from Cython. If this happens, most performance gains are of course lost and even if it does not, there is a tiny overhead in calling
a cpdef
method from Cython compared to calling a cdef
method.
Parameters of either type of function can be declared to have C data types, using normal C declaration syntax. For example:
def spam(int i, char *s):
...
cdef int eggs(unsigned long l, float f):
...
Automatic conversion is currently only possible for numeric types, string types and structs
(composed recursively of any of these types);
attempting to use any other type for the parameter of a Python function will result in a compile-time error. Care must be taken with strings
to ensure a reference if the pointer is to be used after the call. Structs can be obtained from Python mappings, and again care must be
taken with string attributes if they are to be used after the function returns.
C functions, on the other hand, can have parameters of any type, since they’re passed in directly using a normal C function call.
Functions declared using cdef
, like Python functions, will return a False
value when execution leaves the function body without an explicit
return value. This is in contrast to C/C++, which leaves the return value undefined.
In most situations, automatic conversions will be performed for the basic numeric and string types when a Python object is used in a context requiring a C value, or vice versa. The following table summarises the conversion possibilities.
C types | From Python types | To Python types |
---|---|---|
char, short, int, long | int, long | int |
int, long, long long | int, long | long |
float, double, long double | int, long, float | float |
char* | str | str |
struct, union | dict |
Control structures and expressions follow Python syntax for the most part. When applied to Python objects, they have the same semantics as in Python (unless otherwise noted). Most of the Python operators can also be applied to C values, with the obvious semantics.
If Python objects and C values are mixed in an expression, conversions are performed automatically between Python objects and C numeric or string types.
Key Points
Cython supports a range of basic C data types.
Variables must be declared with C data types to get a performance boost.
Normal Python functions can be mixed with Cython functions.
Normal Python syntax is used in most places.