diff --git a/spidev_module.c b/spidev_module.c index 4502664..162b7cb 100644 --- a/spidev_module.c +++ b/spidev_module.c @@ -267,8 +267,8 @@ SpiDev_readbytes(SpiDevObject *self, PyObject *args) list = PyList_New(len); for (ii = 0; ii < len; ii++) { - PyObject *val = Py_BuildValue("l", (long)rxbuf[ii]); - PyList_SET_ITEM(list, ii, val); + PyObject *val = PyLong_FromLong((long)rxbuf[ii]); + PyList_SET_ITEM(list, ii, val); // Steals reference, no need to Py_DECREF(val) } return list; @@ -586,8 +586,9 @@ SpiDev_xfer(SpiDevObject *self, PyObject *args) #endif for (ii = 0; ii < len; ii++) { - PyObject *val = Py_BuildValue("l", (long)rxbuf[ii]); + PyObject *val = PyLong_FromLong((long)rxbuf[ii]); PySequence_SetItem(seq, ii, val); + Py_DECREF(val); // PySequence_SetItem does not steal reference, must Py_DECREF(val) } // WA: @@ -696,8 +697,9 @@ SpiDev_xfer2(SpiDevObject *self, PyObject *args) } for (ii = 0; ii < len; ii++) { - PyObject *val = Py_BuildValue("l", (long)rxbuf[ii]); + PyObject *val = PyLong_FromLong((long)rxbuf[ii]); PySequence_SetItem(seq, ii, val); + Py_DECREF(val); // PySequence_SetItem does not steal reference, must Py_DECREF(val) } // WA: // in CS_HIGH mode CS isnt pulled to low after transfer @@ -844,8 +846,8 @@ SpiDev_xfer3(SpiDevObject *self, PyObject *args) return NULL; } for (ii = 0, jj = block_start; ii < block_size; ii++, jj++) { - PyObject *val = Py_BuildValue("l", (long)rxbuf[ii]); - PyTuple_SetItem(rx_tuple, jj, val); + PyObject *val = PyLong_FromLong((long)rxbuf[ii]); + PyTuple_SetItem(rx_tuple, jj, val); // Steals reference, no need to Py_DECREF(val) } block_start += block_size;