[Pyrex] access to Modules/*.c or ignoring void**/char** casts

Josiah Carlson jcarlson at uci.edu
Fri Mar 25 06:16:01 CET 2005


cookedm at physics.mcmaster.ca (David M. Cooke) wrote:
> 
> Josiah Carlson <jcarlson at uci.edu> writes:
[snip]
> > Question 1:
> > How does one gain access to functions defined in mmapmodule.c, or any
> > function defined in any of the Python source Modules/*.c files?
> 
> You can't. The functions in mmap, for instance, are declared static,
> so they can't be called from outside the file.

Ick.  Fairly unfortunate that the rest of the objects/functions defined
in those files are also not usable.  I guess C/Python API it is.


> Now, a hacky way to get access to the mmap object would be to define
> the mmap_object from the mmapmodule.c file in your own header file,
> and cast a Python object you're sure is an mmap object to that struct.
> Then you can access the fields (like the data etc).

That is quite hackish.


> > Problem 2:
> > For my problem, there is always the buffer interface: get a read-write
> > buffer, then get the char* from the buffer.  Great, except for one
> > problem, in the buffer object definition...
> >     int get_buf(PyBufferObject, void **, int *)
> > ...takes a void** where it assigns the char* to the data.  When I try to
> > use this, I get Pyrex errors of the form:
> >     Cannot assign type 'char (*(*))' to 'void (*(*))'
> >
> > Question 2:
> > How would one tell Pyrex "don't worry about char**/void** casting", or
> > its equivalent?
> 
> Where's get_buf from? For what you'd want, I'd probably do something like

From bufferobject.h.


> cdef extern from "Python.h":
>     int PyObject_AsWriteBuffer(object obj, void **buffer, int *buffer_len)
> 
> cdef int get_buffer(obj, char **data, int *length) except -1:
>     cdef int result
>     cdef void *vd
>     # use a void *
>     result = PyObject_AsWriteBuffer(obj, &vd, length)
>     data[0] = <char *>vd
>     return result
> 
> Note we call PyObject_AsWriteBuffer with a void *, then cast it back
> to char *.

The above solution worked well, thank you.
 - Josiah




More information about the Pyrex mailing list