extending with C

Jesper Olsen jolsen at mailme.dk
Mon Jan 14 15:45:27 EST 2002


Thanks.

Now, I see that the address of the array data can be obtained
with the array method "buffer_info".

The address can be passed back and forth between python and C
as an integer...I guess this is shady also, but quite effective,
and easier than defining a new Python type for the pointer

Jesper


"Jason Orendorff" <jason at jorendorff.com> wrote in message news:<mailman.1010769257.22923.python-list at python.org>...
> > I am not super enthusiastic about the python array type
> > - I have not cheked the implementation, but given the operations
> > on it, it can not map well to c-arrays.
> 
> Actually, the implementation is a C array.
> 
> I think you can get at the data this way, although I haven't tried:
> 
>   int size;
>   void *pRawData;
>   int *pIntData;
> 
>   /* First, make sure it's a buffer object. */
>   if (!PyObject_CheckReadBuffer(obj)) {
>       PyErr_SetString(...);
>       return NULL;
>   }
> 
>   /* Get the data buffer, for reading. */
>   size = obj->ob_type->tp_as_buffer->bf_getreadbuffer(obj, 0, &pRawData);
>   if (size == -1)
>       return NULL;
> 
>   /* Cast the pointers to whatever type you're actually expecting. */
>   size /= sizeof(int);
>   pIntData = (int *) pRawData;
> 
> Now you have a pointer to an int array, and 'size' is the number of
> elements in the array.
> 
> This is slightly shady, because there's no way to make sure
> you're actually dealing with an integer array object.
> But it should work.
> 
> ## Jason Orendorff    http://www.jorendorff.com/



More information about the Python-list mailing list