returning a list of tuples -- C API

John Hunter jdhunter at ace.bsd.uchicago.edu
Thu Dec 4 11:47:53 EST 2003


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[i], totals[i]);
    PyList_SetItem(list, i, tup);
    totals[i] = 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 





More information about the Python-list mailing list