extending with C

Jason Orendorff jason at jorendorff.com
Fri Jan 11 12:09:16 EST 2002


> 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