[Pyrex] Best practice for wrapping struct

Edward C. Jones edcjones at erols.com
Tue Oct 26 16:45:46 CEST 2004


I would like to use a ".pxd" file here instead of a "C name 
specification". Here is my current code:

--------------------------------------------------
example.h:

typedef struct {
    int iVal;
} Example;

Example* create(int i);
void destroy(Example* t);
int mult(Example* t, int scalar, int shift);

---------------------------------------------------
pyExample.pyx:

cdef extern from "example.h":
    ctypedef struct c_Example "Example":
        int iVal

    c_Example* create(int i)
    void destroy(c_Example* t)
    int c_mult "mult" (c_Example *t, int scalar, int shift)

cdef class Example:
    cdef c_Example* t

    def __new__(self, i):
        self.t = create(i)
        if self.t == NULL:
            raise MemoryError

    def __dealloc__(self):
        destroy(self.t)

    def mult(self, pair):
        return c_mult(self.t, pair[0], pair[1])

    def __getattr__(self, name):
        if name == 'iVal':
            return self.t.iVal
        return object.__getattribute__(self, name)

    def __setattr__(self, name, val):
        if name == 'iVal':
            self.t.iVal = val
        else:
            object.__setattr__(self, name, val)
----------------------------------------------------

Victor Ng wrote:

>cdef extern from "example.h":
>    cdef struct Example:
>        int iVal
>    Example* create(int)
>    void destroy(Example* t)
>    int mult(Example* t, int scalar, int shift)
>
>vic
>
>On Mon, 25 Oct 2004 22:03:33 -0400, Edward C. Jones <edcjones at erols.com> wrote:
>  
>
>>Here is the file "example.h":
>>
>>typedef struct {
>>    int iVal;
>>} Example;
>>
>>Example* create(int i);
>>void destroy(Example* t);
>>int mult(Example* t, int scalar, int shift);
>>
>>What is currently the best way to wrap this as a Python class "Example"?
>>
>>_______________________________________________
>>Pyrex mailing list
>>Pyrex at lists.copyleft.no
>>http://lists.copyleft.no/mailman/listinfo/pyrex
>>
>>    
>>
>
>_______________________________________________
>Pyrex mailing list
>Pyrex at lists.copyleft.no
>http://lists.copyleft.no/mailman/listinfo/pyrex
>
>  
>




More information about the Pyrex mailing list