[Pyrex] Namespaces, modules and includes - oh my!

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Jan 28 10:55:21 CET 2005


Gavin Baker wrote:
> In a nutshell, the problem is that types included via "include" in two
> separate modules are not regarded as the same when one module calls
> cdef methods that use these included types in the other.

You shouldn't be using include for that. You should be
defining the shared types in a .pxd file and using
cimport to import them.

The include statement is a pure textual inclusion --
the result is the same as if you'd written the
declarations out twice. Since Pyrex compares types by
identity (unlike C), the result is two different types.

> The implication of this seems to be that if you want to use C code in
> Pyrex when sharing between multiple extension modules, you need to
> "nominate" one Pyrex module to "own" the C interface, and only include
> the declarations in that one module.

This is more or less right, except that the owning
module doesn't need to be a real module. It's possible
to have a .pxd file serving purely as a namespace for
declarations, without any corresponding .pyx file.

In your example, all you need to do is rename 'stdio.pxi'
to 'stdio.pxd', and cimport FILE from that in each .pyx
file where you need it.

--
Greg






More information about the Pyrex mailing list