0

Having some trouble with this code. Trying to return a tuple of tuples (coordinates) from a C++ module Im writing. It looks right to me, the dirty list contains two Coords so len is 2, the x and y values of the items in the list are 0,0 and 0,1 respectively. First time Im attempting this so I might very well have misunderstood the docs or something. Any hints?

PyObject* getDirty()
{
    int len = dirty.size();
    PyObject* tuple = PyTuple_New(len);
    int count = 0;
    for (std::list<Coord>::iterator i = dirty.begin(); i != dirty.end(); ++i)
    {
        PyTuple_SET_ITEM(tuple, count, PyTuple_Pack(2, (*i).x, (*i).y));
        ++count;
    }
    return tuple;
}

Edit: Oh, forgot to mention, the actual crash is on the PyTuple_Set_ITEM line.

Johan Råde
  • 20,480
  • 21
  • 73
  • 110
Mizipzor
  • 51,151
  • 22
  • 97
  • 138

1 Answers1

1

The arguments to PyTuple_Pack, after the first one, must be PyObject pointers.

You might want instead

Py_BuildValue("(ii)", (*i).x, (*i).y)

...assuming the coordinates are actually of type int.

Jason Orendorff
  • 42,793
  • 6
  • 62
  • 96
  • Yes, just found that out through some crazy experimentation. Although, I do it with the format string "(i,i)" is that wrong? And, btw, you dont happen to know the difference? PyTuple_Pack seems to return a PyObject* (which was created) so I cant see why that didnt work. – Mizipzor Nov 23 '09 at 21:34
  • The relevant difference between Py_BuildValue and PyTuple_Pack is that Py_BuildValue converts each individual value to a `PyObject*` for you, whereas PyTuple_Pack expects each of its arguments to already be a `PyObject*`. – Jason Orendorff Nov 24 '09 at 12:07