Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with uint8_t on a Python Extension?

I would like to pass as argument of a function in my C module an array of uint8_t's.

I couldn't find a method to directly parse this array, so I'm parsing it to a PyObject_t and then iterating as a PyTuple_t object. This way, I need to cast each element PyObject_t of this tuple to uint8_t.

How can I do that, once that there is no PyInt_FromUINT8_t function or anything like it?

like image 354
Pedro Alves Avatar asked Apr 26 '16 15:04

Pedro Alves


1 Answers

You can usually just get away with B using unsigned char. According to Parsing Arguments you should just be able to do:

uint8_t b;
if (!PyArg_ParseTuple("b", &b)) {
    return NULL;
}

If not directly using arguments (e.g. you are dealing with a PyObject, simply use one of the PyInt_*, PyLong_* or PyNumber_* functions (https://docs.python.org/3/c-api/number.html?highlight=pynumber#c.PyNumber_AsSsize_t).

Converting from a uin8_t to a PyObject is simple as well, you can use PyInt_FromLong or PyLong_FromLong

like image 73
Mark Nunberg Avatar answered Oct 02 '22 09:10

Mark Nunberg