Need help with C extension module

Robert Kern rkern at ucsd.edu
Thu Sep 8 02:05:50 CEST 2005


chris wrote:
> Any tips on what the pyrex should look like for my example?

# Untested and off the top of my head; please read the Pyrex
# documentation and correct my mistakes before using any of this!
# Notably, I'm pretty sure the __init__ and __dealloc__ bits are
# slightly wrong.

cdef extern from "Python.h":
    void *PyMem_Malloc(unsigned int size)
    void PyMem_Free(void *buf)

ctypedef struct In:
    int x
    char *s
    # ...

ctypedef struct Out:
    int y
    char *s
    # ...

cdef Out *func(Int *x, Out *y)

cdef class Output:
    cdef Out *output

    def __init__(self):
        self.output = <Out*>PyMem_Malloc(sizeof(Out))

    def __dealloc__(self):
        PyMem_Free(self.output)

    property y:
        def __get__(self):
            return self.output.y

    # ...

cdef class Input:
    cdef In *input

    def __init__(self):
        self.input = <In*>PyMem_Malloc(sizeof(In))

    def __dealloc__(self):
        PyMem_Free(self.input)

    property x:
        def __get__(self):
            return self.input.x

        def __set__(self, value):
            self.input.x = value

    # ...

    def func(self):
        output = Output()
        func(self.input, output.output)
        return output

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter




More information about the Python-list mailing list