Working with Python arrays

Python has a builtin array module supporting dynamic 1-dimensional arrays ofprimitive types. It is possible to access the underlying C array of a Pythonarray from within Cython. At the same time they are ordinary Python objectswhich can be stored in lists and serialized between processes when usingmultiprocessing.

Compared to the manual approach with malloc() and free(), thisgives the safe and automatic memory management of Python, and compared to aNumpy array there is no need to install a dependency, as the arraymodule is built into both Python and Cython.

Safe usage with memory views

  1. from cpython cimport array
  2. import array
  3. cdef array.array a = array.array('i', [1, 2, 3])
  4. cdef int[:] ca = a
  5.  
  6. print(ca[0])

NB: the import brings the regular Python array object into the namespacewhile the cimport adds functions accessible from Cython.

A Python array is constructed with a type signature and sequence ofinitial values. For the possible type signatures, refer to the Pythondocumentation for the array module.

Notice that when a Python array is assigned to a variable typed asmemory view, there will be a slight overhead to construct the memoryview. However, from that point on the variable can be passed to otherfunctions without overhead, so long as it is typed:

  1. from cpython cimport array
  2. import array
  3.  
  4. cdef array.array a = array.array('i', [1, 2, 3])
  5. cdef int[:] ca = a
  6.  
  7. cdef int overhead(object a):
  8. cdef int[:] ca = a
  9. return ca[0]
  10.  
  11. cdef int no_overhead(int[:] ca):
  12. return ca[0]
  13.  
  14. print(overhead(a)) # new memory view will be constructed, overhead
  15. print(no_overhead(ca)) # ca is already a memory view, so no overhead

Zero-overhead, unsafe access to raw C pointer

To avoid any overhead and to be able to pass a C pointer to otherfunctions, it is possible to access the underlying contiguous array as apointer. There is no type or bounds checking, so be careful to use theright type and signedness.

  1. from cpython cimport array
  2. import array
  3.  
  4. cdef array.array a = array.array('i', [1, 2, 3])
  5.  
  6. # access underlying pointer:
  7. print(a.data.as_ints[0])
  8.  
  9. from libc.string cimport memset
  10.  
  11. memset(a.data.as_voidptr, 0, len(a) * sizeof(int))

Note that any length-changing operation on the array object may invalidate thepointer.

Cloning, extending arrays

To avoid having to use the array constructor from the Python module,it is possible to create a new array with the same type as a template,and preallocate a given number of elements. The array is initialized tozero when requested.

  1. from cpython cimport array
  2. import array
  3.  
  4. cdef array.array int_array_template = array.array('i', [])
  5. cdef array.array newarray
  6.  
  7. # create an array with 3 elements with same type as template
  8. newarray = array.clone(int_array_template, 3, zero=False)

An array can also be extended and resized; this avoids repeated memoryreallocation which would occur if elements would be appended or removedone by one.

  1. from cpython cimport array
  2. import array
  3.  
  4. cdef array.array a = array.array('i', [1, 2, 3])
  5. cdef array.array b = array.array('i', [4, 5, 6])
  6.  
  7. # extend a with b, resize as needed
  8. array.extend(a, b)
  9. # resize a, leaving just original three elements
  10. array.resize(a, len(a) - len(b))

API reference

Data fields

  1. data.as_voidptr
  2. data.as_chars
  3. data.as_schars
  4. data.as_uchars
  5. data.as_shorts
  6. data.as_ushorts
  7. data.as_ints
  8. data.as_uints
  9. data.as_longs
  10. data.as_ulongs
  11. data.as_longlongs # requires Python >=3
  12. data.as_ulonglongs # requires Python >=3
  13. data.as_floats
  14. data.as_doubles
  15. data.as_pyunicodes

Direct access to the underlying contiguous C array, with given type;e.g., myarray.data.as_ints.

Functions

The following functions are available to Cython from the array module:

  1. int resize(array self, Py_ssize_t n) except -1

Fast resize / realloc. Not suitable for repeated, small increments; resizesunderlying array to exactly the requested amount.

  1. int resize_smart(array self, Py_ssize_t n) except -1

Efficient for small increments; uses growth pattern that deliversamortized linear-time appends.

  1. cdef inline array clone(array template, Py_ssize_t length, bint zero)

Fast creation of a new array, given a template array. Type will be same astemplate. If zero is True, new array will be initialized with zeroes.

  1. cdef inline array copy(array self)

Make a copy of an array.

  1. cdef inline int extend_buffer(array self, char* stuff, Py_ssize_t n) except -1

Efficient appending of new data of same type (e.g. of same array type)n: number of elements (not number of bytes!)

  1. cdef inline int extend(array self, array other) except -1

Extend array with data from another array; types must match.

  1. cdef inline void zero(array self)

Set all elements of array to zero.