embedding; return array

Fredrik Lundh fredrik at pythonware.com
Fri Jul 2 04:02:06 EDT 1999


Randy Heiland wrote:
> I'd welcome any pointers into being able to return an (unsigned char)
> array from an embedded module.

how about:

    return Py_BuildValue("s#", buffer, buffer_size);

(gives you a string; you can use the array module to
process it further).

if you really want to return it as a python list, you
have to build the list yourself.  something like:

    PyObject* list;

    list = PyList_New(buffer_size);
    if (list)
        for (i = 0; i < buffer_size; i++) {
            PyObject* item;
            item = PyInt_FromLong(buffer[i]);
            if (item == NULL) {
                Py_DECREF(list);
                list = NULL;
                break;
            }
            PyList_SetItem(list, i, item);
        }
    }

    return list;

should do the trick.

</F>





More information about the Python-list mailing list