The C structures and functions that we wrap in Pyrex behave
like C structures and functions, not like Python objects and
Therefor we write wrappers for the C functions so they behave
The last part of out CUPS module that we
have to write is a wrapper for the
We start it in the same way we would start any Python
Next we declare all the local C-variables.
The first is the return value for call to
cupsGetDests, which is going to supply an array of
The second is a variable that we are going to use in the
cdef cups_dest_t *dests cdef cups_dest_t currDest
Now we make the call to the C function
numDests = cupsGetDests(&dests)
numDests was not declared because it
was a normal Python integer.
The final job of our Python procedure is to unpack the C array and put the names into a Python list.
retval =  for i in range(numDests): currDest = dests[i] retval.append(currDest.name) return retval
The loop starts by unpacking each element of the array
dests into a local variable.
name element of the
cups_dest_t structure is appended to a list,
before the list of names is returned.
There are three parts to a Pyrex extension-class:
Writing a Python class to wrap a C structure begins by
declaring the local variables.
The variable declarations are the same for classes as they are
For the XOSD wrapper-class we declare the local
xosdWin immediately after the
class declaration, as follows.
cdef class XOSD: cdef xosd *xosdWin
Note: You cannot declare the C variables inside
the constructor (
The constructor has to be written next.
__new__ method to create the constructor
— instead of using the traditional
__new__ is called before the
object is created.
Initialise all the C variables, and libraries inside the
constructor; for XOSD, the
xosd_init function is called.
def __new__(self, font, colour, timeout, pos): self.xosdWin = xosd_init(font, colour, timeout, pos, 0, 1)
To wrap the destructor, override the
xosd_uninit function is called inside the
destructor to remove the XOSD window.
def __dealloc__(self): xosd_uninit(self.xosdWin)
Alert readers would have noticed that the function
xosd_uninit was not declared
The declaration is left as an exercise to the reader.
The final task is to write the standard methods that will be
called by code outside the module.
These methods are written in a similar way to standard Python
For our example, we will wrap up the
function, creating a
display_text method that
will display the text on the screen.
def display_text(self, text):
xosd_display took a variable number
Because of this we have to convert
text to the
correct C-type ourselves, otherwise Pyrex would not know what
type to make
To do this we create a temporary C-variable,
cText, that is passed to
cdef char *cText cText = text xosd_display(self.xosdWin, 1, XOSD_string, cText)