Wrapping Structures

Structures are similar to Python classes, except they do not contain any methods. They are the next element that you convert from C to Pyrex. To convert the structure:

  1. Replace the typedef struct with cdef struct,
  2. Move the name of the structure from after the closing bracket (}) to after the struct,
  3. Remove ; from the end of each line, and
  4. Replace the {} blocking with a : and indenting.
C
U
P
S

Structures will look something like the following (from the CUPS cups.h file).

typedef struct
{
  char *name;
  char *value;
} cups_option_t;

This is a structure, called cups_option_t, that holds two elements: name and value. The Pyrex version looks similar, including the types!

cdef struct cups_option_t:
   char *name
   char *value
struct
tells Pyrex that you are declaring a structure.
cups_option_t
is the name of the structure.

Structures can also hold other structure, just like C. For example:

cdef struct cups_dest_t:
    char          *name
    char          *instance
    int           is_default
    int           num_options
    cups_option_t *options

The options value of the above structure holds a pointer to the cups_option_t structure we declared earlier.

You may want to try and compile the two structures above, linking against the cups library.

X
O
S
D

External Structures

Some structures are external to the C header-file. To declare these structures you use the extern keyword. Consider the following example, taken from xosd.h.

typedef struct xosd xosd;

In Pyrex it would look like the following.

cdef extern struct xosd
Previous Contents Next

Michael JasonSmith
Last modified: Mon Jun 24 11:54:51 NZST 2002