hopefully fixed reference counting when adding to dictionaries

develop
doomchild 2010-04-08 14:08:58 -05:00
parent e536374aa7
commit b555085e8c
2 changed files with 111 additions and 26 deletions

@ -79,47 +79,86 @@ static PyObject* BuildLike(DFHack::t_like& like)
return Py_BuildValue("OOO", item, BuildMatglossPair(like.material), PyBool_FromLong((int)like.active));
}
//PyDict_SetItem and PyDict_SetItemString don't steal references, so this had to get a bit more complicated...
static PyObject* BuildNote(DFHack::t_note& note)
{
PyObject* noteDict = PyDict_New();
PyObject* temp;
PyDict_SetItemString(noteDict, "symbol", PyString_FromFormat("%c", note.symbol));
PyDict_SetItemString(noteDict, "fore_back", Py_BuildValue("II", note.foreground, note.background));
temp = PyString_FromFormat("%c", note.symbol);
PyDict_SetItemString(noteDict, "symbol", temp);
Py_DECREF(temp);
temp = Py_BuildValue("II", note.foreground, note.background);
PyDict_SetItemString(noteDict, "fore_back", temp);
Py_DECREF(temp);
if(note.name[0])
PyDict_SetItemString(noteDict, "name", PyString_FromString(note.name));
temp = PyString_FromString(note.name);
else
PyDict_SetItemString(noteDict, "name", PyString_FromString(""));
PyString_FromString("");
PyDict_SetItemString(noteDict, "name", temp);
Py_DECREF(temp);
temp = Py_BuildValue("III", note.x, note.y, note.z);
PyDict_SetItemString(noteDict, "position", Py_BuildValue("III", note.x, note.y, note.z));
PyDict_SetItemString(noteDict, "position", temp);
Py_DECREF(temp);
return noteDict;
}
//same here...reference counting is kind of a pain, assuming I'm even doing it right...
static PyObject* BuildName(DFHack::t_name& name)
{
PyObject* nameDict;
PyObject *wordList, *speechList;
PyObject* temp;
int wordCount = 7;
nameDict = PyDict_New();
if(name.first_name[0])
PyDict_SetItemString(nameDict, "first_name", PyString_FromString(name.first_name));
temp = PyString_FromString(name.first_name);
else
PyDict_SetItemString(nameDict, "first_name", PyString_FromString(""));
temp = PyString_FromString("");
PyDict_SetItemString(nameDict, "first_name", temp);
Py_DECREF(temp);
if(name.nickname[0])
PyDict_SetItemString(nameDict, "nickname", PyString_FromString(name.nickname));
temp = PyString_FromString(name.nickname);
else
PyDict_SetItemString(nameDict, "nickname", PyString_FromString(""));
temp = PyString_FromString("");
PyDict_SetItemString(nameDict, "nickname", temp);
Py_DECREF(temp);
temp = PyInt_FromLong(name.language);
PyDict_SetItemString(nameDict, "language", PyInt_FromLong(name.language));
PyDict_SetItemString(nameDict, "has_name", PyBool_FromLong((int)name.has_name));
PyDict_SetItemString(nameDict, "language", temp);
wordList = PyList_New(7);
speechList = PyList_New(7);
Py_DECREF(temp);
for(int i = 0; i < 7; i++)
temp = PyBool_FromLong((int)name.has_name);
PyDict_SetItemString(nameDict, "has_name", temp);
Py_DECREF(temp);
wordList = PyList_New(wordCount);
speechList = PyList_New(wordCount);
for(int i = 0; i < wordCount; i++)
{
PyList_SetItem(wordList, i, PyInt_FromLong(name.words[i]));
PyList_SetItem(wordList, i, PyInt_FromLong(name.parts_of_speech[i]));
@ -128,6 +167,9 @@ static PyObject* BuildName(DFHack::t_name& name)
PyDict_SetItemString(nameDict, "words", wordList);
PyDict_SetItemString(nameDict, "parts_of_speech", speechList);
Py_DECREF(wordList);
Py_DECREF(speechList);
return nameDict;
}
@ -135,18 +177,36 @@ static PyObject* BuildSettlement(DFHack::t_settlement& settlement)
{
PyObject* setDict;
PyObject *local_pos1, *local_pos2;
PyObject* temp;
setDict = PyDict_New();
PyDict_SetItemString(setDict, "origin", PyInt_FromLong(settlement.origin));
PyDict_SetItemString(setDict, "name", BuildName(settlement.name));
PyDict_SetItemString(setDict, "world_pos", Py_BuildValue("ii", settlement.world_x, settlement.world_y));
temp = PyInt_FromLong(settlement.origin);
PyDict_SetItemString(setDict, "origin", temp);
Py_DECREF(temp);
temp = BuildName(settlement.name);
PyDict_SetItemString(setDict, "name", temp);
Py_DECREF(temp);
temp = Py_BuildValue("ii", settlement.world_x, settlement.world_y);
PyDict_SetItemString(setDict, "world_pos", temp);
Py_DECREF(temp);
local_pos1 = Py_BuildValue("ii", settlement.local_x1, settlement.local_y1);
local_pos2 = Py_BuildValue("ii", settlement.local_x2, settlement.local_y2);
PyDict_SetItemString(setDict, "local_pos", Py_BuildValue("OO", local_pos1, local_pos2));
Py_DECREF(local_pos1);
Py_DECREF(local_pos2);
return setDict;
}

@ -42,30 +42,53 @@ struct DF_Material
// Helpers
static PyObject* BuildMatgloss(t_matgloss matgloss)
static PyObject* BuildMatgloss(t_matgloss& matgloss)
{
PyObject* matDict;
PyObject* temp;
matDict = PyDict_New();
if(matgloss.id[0])
PyDict_SetItemString(matDict, "id", PyString_FromString(matgloss.id));
temp = PyString_FromString(matgloss.id);
else
PyDict_SetItemString(matDict, "id", PyString_FromString(""));
temp = PyString_FromString("");
PyDict_SetItemString(matDict, "fore", PyInt_FromLong(matgloss.fore));
PyDict_SetItemString(matDict, "back", PyInt_FromLong(matgloss.back));
PyDict_SetItemString(matDict, "bright", PyInt_FromLong(matgloss.bright));
PyDict_SetItemString(matDict, "id", temp);
Py_DECREF(temp);
temp = PyInt_FromLong(matgloss.fore);
PyDict_SetItemString(matDict, "fore", temp);
Py_DECREF(temp);
temp = PyInt_FromLong(matgloss.back);
PyDict_SetItemString(matDict, "back", temp);
Py_DECREF(temp);
temp = PyInt_FromLong(matgloss.bright);
PyDict_SetItemString(matDict, "bright", temp);
Py_DECREF(temp);
if(matgloss.name[0])
PyDict_SetItemString(matDict, "name", PyString_FromString(matgloss.name));
temp = PyString_FromString(matgloss.name);
else
PyDict_SetItemString(matDict, "name", PyString_FromString(""));
temp = PyString_FromString("");
PyDict_SetItemString(matDict, "name", temp);
Py_DECREF(temp);
return matDict;
}
static PyObject* BuildMatglossPlant(t_matglossPlant matgloss)
static PyObject* BuildMatglossPlant(t_matglossPlant& matgloss)
{
PyObject* matDict;
@ -115,6 +138,8 @@ static PyObject* BuildMatglossList(std::vector<t_matgloss> & matVec)
PyObject* matgloss = BuildMatgloss(*matIter);
PyList_Append(matList, matgloss);
Py_DECREF(matgloss);
}
return matList;