J
John Hunter
I am writing a python extension module and have a reference counting
question
My function looks like
static PyObject *
pokereval_seven_cards(PyObject *self, PyObject *args)
{
int i;
PyObject * tup;
PyObject * list;
// ... snip ...
list = PyList_New(HandType_LAST+1);
for (i = HandType_FIRST; i <= HandType_LAST; i++) {
tup = Py_BuildValue("(s,h)", handTypeNamesPadded, totals);
PyList_SetItem(list, i, tup);
totals = 0;
}
Py_INCREF(list);
return list;
}
Should I be incrementing the ref of tup each time I call
Py_BuildValue? Is it correct to increment the ref of list before I
return it? Does it make a difference vis-a-vis ref counting if I
create the tuple with Py_BuildValue or PyTuple_New?
Thanks,
John Hunter
question
My function looks like
static PyObject *
pokereval_seven_cards(PyObject *self, PyObject *args)
{
int i;
PyObject * tup;
PyObject * list;
// ... snip ...
list = PyList_New(HandType_LAST+1);
for (i = HandType_FIRST; i <= HandType_LAST; i++) {
tup = Py_BuildValue("(s,h)", handTypeNamesPadded, totals);
PyList_SetItem(list, i, tup);
totals = 0;
}
Py_INCREF(list);
return list;
}
Should I be incrementing the ref of tup each time I call
Py_BuildValue? Is it correct to increment the ref of list before I
return it? Does it make a difference vis-a-vis ref counting if I
create the tuple with Py_BuildValue or PyTuple_New?
Thanks,
John Hunter