user124114
user124114

Reputation: 8721

How to call a C-API function such as PyUnicode_READ_CHAR from Cython?

I'm using Cython to speed up a function that operates over a string (unicode, CPython 3.6).

How do I call CPython's Py_UCS4 val = PyUnicode_READ_CHAR(my_string, my_index) from my Cython .pyx code? I cannot figure out the correct way to import and use the C-API from Cython.

Thanks!

Upvotes: 2

Views: 238

Answers (1)

ead
ead

Reputation: 34377

Cython doesn't wrap the whole CPython's C-API in cpython.*-headers, only the most important part. If a function is not wrapped, it can be always done on the fly, e.g.:

#instead of from cpython.unicode cimport PyUnicode_READ_CHAR
cdef extern from *:
    Py_UCS4 PyUnicode_READ_CHAR(object o, Py_ssize_t index)

After that the function can be used in Cython.

Upvotes: 2

Related Questions