Cython: (Why / When) Is it preferable to use Py_ssize_t for indexing?

embert picture embert · Jan 8, 2014 · Viewed 13.9k times · Source

This is a follow-up to this question.

(Why / When) Is it preferable to use Py_ssize_t for indexing? In the docs I just found

# Purists could use "Py_ssize_t" which is the proper Python type for
# array indices.

-> Does that mean always when indexing NumPy/Cython - array(s)/-views one should use Py_ssize_t?

-> Is Py_ssize_t e. g. an unsigned int so that I can't used @cython.boundscheck(False)

Answer

Warren Weckesser picture Warren Weckesser · Jan 8, 2014

Py_ssize_t is signed. See PEP 353, where it says "A new type Py_ssize_t is introduced, which has the same size as the compiler's size_t type, but is signed. It will be a typedef for ssize_t where available."

You should use Py_ssize_t for indexing. I didn't find a definitive statement of this in the Cython docs, but Stefan Behnel, a Cython developer, said as much in an email (https://groups.google.com/forum/#!topic/cython-users/brENF_M9zxM):

As a general remark, you are using ints as indices. You should use the Py_ssize_t type instead (or size_t, if you prefer an unsigned type) to properly accommodate for 64 bit architectures.