[Cython] (bug?) "cdef public" and Python attribute access

Stefan Behnel stefan_ml at behnel.de
Tue Mar 20 09:21:57 CET 2012


note that the cython-users mailing list would be a more appropriate place
to discuss this. Please reply over there.

David Warde-Farley, 20.03.2012 06:44:
> All of this refers to Cython 0.15.1:
> I have a "cdef public void" function which takes several ndarrays, that
> segfaults when I call it from other C code. Inside I use the ".size"
> attribute, triggering a PyObject_GetAttr, which segfaults. Running in
> gdb and looking at the generated C code, I see it is because
> __pyx_n_s__size is NULL, presumably owing to __Pyx_InitStrings() never
> being called.

I take it you are embedding Cython code in a C program?

Note that the module must be imported in order to work properly. See here:


> Am I to presume this is intended behaviour?


> i.e. code that generates
> Python C API (or more specifically GetAttr) calls simply doesn't
> work/isn't intended to work from "public" functions called from C code?

It does work, but it requires two things: a properly imported module and
(unless declared "nogil") the GIL.

> I don't see mention of it in the documentation, so it's kind of a nasty
> surprise :)

Yes, this can use some better documentation.

> (The "api" keyword mentions the import_modulename()
> business but no signature for that is generated in my header file.)

It has been discussed before that the concepts "api" and "public" are not
very clearly separated and should eventually be merged into one. Hasn't
been done yet.

Basically, the concepts are somewhat more focussed towards being used from
inside of CPython than towards embedding CPython in C code. Both work, but
embedding requires a little more manual setup.


More information about the cython-devel mailing list