[Pyrex] newby guidance

Greg Ewing greg at cosc.canterbury.ac.nz
Sun Jun 29 08:19:23 CEST 2003


Robin Becker <robin at reportlab.com>:

> Can we declare members sparsely, or do I have to repeat the whole thing
> to get the layout the same? If the latter it seems likely to cause

No. The actual layout will be determined by the contents of
the header file. You only need to declare the members that
you want to access from Pyrex, and it doesn't matter what
order you declare them in.

> so can I do
>
> cdef class AStruct:
>     cdef extern from "myheader.h":
>         struct MyStruct

No, but you can do

  cdef extern from "myheader.h":

    struct MyStruct:
      int i
      # etc...

  cdef class MyClass:

    cdef MyStruct mystruct

    def __new__(self):
      self.mystruct.i = 42
      # etc...

> From what I understand there's no actual parsing/understanding of the
> structs from the header files. So the from clauses are there purely to
> get the includes done for the generated C code.

Yes, that's right.

> If that's the case, is there any way I can get a pyrex definition to
> create a header that's compatible with the layout defined in the pyrex
> struct?

Yes, you can declare things "public", e.g.

  cdef public struct MyStruct:
    ...

Pyrex will then generate a .h file containing C declarations
for everything in the module that was declared public. See
the section on "Public Declarations" in the docs for more.

Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg at cosc.canterbury.ac.nz	   +--------------------------------------+




More information about the Pyrex mailing list