Returning variable length data from C extensions 
Author Message
 Returning variable length data from C extensions

Hi all.

I have a C extension function in which I need to return a variable amount
of data (varying between 5 and 32 bytes).

As far as I can see from pages 16-18 in the 'extending' docs that come with
Python, I can't use Py_BuildValue().

I tried returning the data as a string (see the example below), but that
fails as the data often contains 0x00, which gets interpreted as the string
NULL value, truncating the returned data string.

Does anyone have a suggestion on how I can return variable amounts of data?

Thanks
Graeme

static PyObject *
read_network(PyObject *self, PyObject *args) {

  PyObject *retObj = NULL;      /* NULL = exception */
  int n;                        /* incoming data count */
  char dataIn[BUFSIZE];         /* the incoming data buffer */

  /* there should be no arguments passed */
  if (PyArg_ParseTuple(args, "")) {

    n = rawRead(&dataIn, BUFSIZE);
    retObj = Py_BuildValue("s", dataIn);

  } else {
    PyErr_SetString(PyExc_SystemError, "Error calling read_network");
  }

  return retObj;

Quote:
} /* read_network */

----------------------------------------------------------------------------
Graeme Cross                    Phone: 03 9903 2606

Monash University                 URL: http://www.*-*-*.com/ ~graeme/


Fri, 25 Jun 1999 03:00:00 GMT  
 Returning variable length data from C extensions

How about returning the data as a list of integers?
Or, if you want to return a string, use PyString_FromStringAndSize()

  n = rawRead(&dataIn, BUFSIZE);
  retObj = PyString_FromStringAndSize(dataIn,n);

Anthony

Quote:
>>> Graeme Cross wrote
> I have a C extension function in which I need to return a variable amount
> of data (varying between 5 and 32 bytes).
> [..snip..]
> I tried returning the data as a string (see the example below), but that
> fails as the data often contains 0x00, which gets interpreted as the string
> NULL value, truncating the returned data string.



Fri, 25 Jun 1999 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Processing variable length/variable data files

2. Variable Length Data Types

3. Variable length raw-byte data

4. Attempting to read variable length records as raw data

5. Vstruct module: unpack heterogenous, variable-length binary data

6. Returning a lot of data from a c extension (SWIG,array, Numeric)

7. Getting true length of a variable length record - IBM Mainframe

8. Finding Variable-Length Record Length

9. Variable length variable

10. Char variable of variable length

11. NEED HELP appending two Char variables of variable length

12. LEA allways returning offset from CS

 

 
Powered by phpBB® Forum Software