[Pyrex] Use of ctypdef MyClass [object, type]

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Oct 12 00:53:56 CEST 2007


Matthieu Brucher wrote:

> The reason I'm asking this is I'm writing a bok and a part 
> of it is on Pyrex, and I'd like to be as precise as possible.

Okay, here's some more explanation. There are about three
different things going on in that statement.

1) The "public" declaration. This causes Pyrex to generate
a .h file containing declarations for all the types, variables
and functions that you have declared as "public". You can
include this file in other C code that is to be linked with
your extension module and wants to use those declarations.

2) The "type" and "object" names. In C, there are two parts
to an extension type -- the C struct that gets allocated for
objects of that type, and the PyTypeObject instance
corresponding to it. Normally Pyrex uses mangled names for
these, to disambiguate between things of the same name in
different modules. However, this isn't very friendly for
external C code to interface with, so these clauses allow
you to specify the names to be used in the generated C code.

3) The "ctypedef". You can declare an extension type using
either "cdef" or "ctypedef". If you use "ctypedef", Pyrex
generates a C typedef for the object struct, so that you can
refer to it simply by the type name in C code. If you use
"cdef", no typedef is generated, so you have to prefix the
type name with "struct" to refer to its object struct type
in C code.

Hope that helps,
Greg



More information about the Pyrex mailing list