Hi,<br>&nbsp; I'd like to wrap the C implementation of a 'class' in Python.<br>In C, I have a struct containing all the information about the<br>object, and functions that take pointers to such structs as<br>arguments. In Python, I'd like a class that holds an instance
<br>variable, and functions in that class merely pass this variable<br>to their C counterparts. This works fairly well with:<br><br>------------<br>cdef class myclass:<br>&nbsp; cdef void *my_struct<br><br>&nbsp; def __init__(self, input):
<br>&nbsp;&nbsp;&nbsp; if type(input) is str:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.my_struct = &lt;void *&gt; struct_from_string(input)<br>------------<br><br>The main problem comes when I get another object of the<br>same class, e.g. for comparison or arithmetic operations.
<br>I can access self.my_struct but not other.my_struct, as<br>Pyrex expects the latter to be a Python object - not a C<br>pointer. I can't make Pyrex recognize a class function as<br>returning other than a Python object, and I can't seem to
<br>get a non-class function to take a Python object and<br>return a pointer to a struct, perhaps because the non-class<br>function has the same problem in that it believes that<br>var.my_struct is a Python object.<br>Is there any way around this?
<br><br>Another problem arises because I'd like the initializer to<br>be able to take a list and convert it to a C array of ints or<br>longs before passing it to a similar struct_from_array()<br>function. The best I have so far is:
<br><br>cdef extern from &quot;stdlib.h&quot;<br>&nbsp; ctypedef int size_t<br>&nbsp; cdef size_t sizeof(void *)<br>&nbsp; cdef void *malloc(size_t)<br><br>...in __init__...<br>&nbsp; if type(input) is list:<br>&nbsp;&nbsp;&nbsp; cdef int *c_array<br>&nbsp;&nbsp;&nbsp; cdef size_t c_size
<br>&nbsp;&nbsp;&nbsp; c_size = sizeof(int) * len(input)<br>&nbsp;&nbsp;&nbsp; c_array = &lt;int *&gt; malloc(c_size)<br>&nbsp;&nbsp;&nbsp; ................<br><br>Now I can't find where size_t and sizeof are actually defined.<br>sizeof is a macro I think, so won't be prototyped. This may
<br>have something to do with the fact that I see malloc defined<br>in the generated C code as taking an 'int' despite the note in<br>the pyrex changelog (0.9.3) saying that typedefs are referred<br>to as their original typedef name. This code needs to work on
<br>a variety of systems, and size_t can be 32, 48, or 64 bits long.<br><br>In the spirit of &quot;batteries included&quot; couldn't Pyrex just parse<br>&nbsp; cdef int *c_array[len(input)]<br>and do its own memory allocation and garbage collection in the
<br>generated C code? If not, is there a safe and portable way to<br>do mallocs in Pyrex code?<br><br>Thanks<br><br>