From e43bc0dfc5be1706765106a7f0806f4cb2e8b2c1 Mon Sep 17 00:00:00 2001 From: doomchild Date: Mon, 26 Apr 2010 10:31:29 -0500 Subject: [PATCH 1/3] changed Name to namedtuple --- dfhack/python/pydftypes.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/dfhack/python/pydftypes.py b/dfhack/python/pydftypes.py index 5ff79d7ea..972729bdd 100644 --- a/dfhack/python/pydftypes.py +++ b/dfhack/python/pydftypes.py @@ -17,9 +17,7 @@ Matgloss = namedtuple("Matgloss", "id, fore, back, bright, name") DescriptorColor = namedtuple("DescriptorColor", "id, r, v, b, name") CreatureTypeEx = namedtuple("CreatureTypeEx", "rawname, castes, tile_character, tilecolor") TileColor = namedtuple("TileColor", "fore, back, bright") - -class Name(object): - __slots__ = ["first_name", "nickname", "language", "has_name", "words", "parts_of_speech"] +Name = namedtuple("Name", "first_name, nickname, language, has_name, words, parts_of_speech") class Soul(object): def __init__(self, *args, **kwds): From 3bf53e59f1aa2d44e89d67920abd4a17cf029c85 Mon Sep 17 00:00:00 2001 From: doomchild Date: Mon, 26 Apr 2010 10:31:43 -0500 Subject: [PATCH 2/3] tweaked BuildName --- dfhack/python/DF_Helpers.cpp | 87 ++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 49 deletions(-) diff --git a/dfhack/python/DF_Helpers.cpp b/dfhack/python/DF_Helpers.cpp index 6c4b580cd..aa07471fb 100644 --- a/dfhack/python/DF_Helpers.cpp +++ b/dfhack/python/DF_Helpers.cpp @@ -90,9 +90,19 @@ static PyObject* BuildMatglossPair(DFHack::t_matglossPair& matgloss) static DFHack::t_matglossPair ReverseBuildMatglossPair(PyObject* mObj) { DFHack::t_matglossPair mPair; + PyObject* temp; + + temp = PyTuple_GetItem(mObj, 0); + + mPair.type = (int16_t)PyInt_AsLong(temp); + + Py_DECREF(temp); - mPair.type = (int16_t)PyInt_AsLong(PyTuple_GetItem(mObj, 0)); - mPair.index = (int32_t)PyInt_AsLong(PyTuple_GetItem(mObj, 1)); + temp = PyTuple_GetItem(mObj, 1); + + mPair.index = (int32_t)PyInt_AsLong(temp); + + Py_DECREF(temp); return mPair; } @@ -172,46 +182,28 @@ static PyObject* BuildNote(DFHack::t_note& note) return noteObj; } +static int NAME_WORD_COUNT = 7; + static PyObject* BuildName(DFHack::t_name& name) { PyObject* nameObj; - PyObject *wordList, *speechList; - PyObject* temp; - int wordCount = 7; - - nameObj = PyObject_CallObject(Name_type, NULL); - - if(name.first_name[0]) - temp = PyString_FromString(name.first_name); - else - temp = PyString_FromString(""); - - OBJSET(nameObj, "first_name", temp); - - if(name.nickname[0]) - temp = PyString_FromString(name.nickname); - else - temp = PyString_FromString(""); - - OBJSET(nameObj, "nickname", temp); + PyObject *wordList, *speechList, *args; - temp = PyInt_FromLong(name.language); - OBJSET(nameObj, "language", temp); + wordList = PyList_New(NAME_WORD_COUNT); + speechList = PyList_New(NAME_WORD_COUNT); - temp = PyBool_FromLong((int)name.has_name); - OBJSET(nameObj, "has_name", temp); - - wordList = PyList_New(wordCount); - speechList = PyList_New(wordCount); - - for(int i = 0; i < wordCount; i++) + for(int i = 0; i < NAME_WORD_COUNT; i++) { PyList_SET_ITEM(wordList, i, PyInt_FromLong(name.words[i])); PyList_SET_ITEM(speechList, i, PyInt_FromLong(name.parts_of_speech[i])); } - OBJSET(nameObj, "words", wordList); - OBJSET(nameObj, "parts_of_speech", speechList); + args = Py_BuildValue("ssiOOO", name.first_name, name.nickname, name.language, \ + PyBool_FromLong((int)name.has_name), wordList, speechList); + + nameObj = PyObject_CallObject(Name_type, args); + + Py_DECREF(args); return nameObj; } @@ -219,8 +211,8 @@ static PyObject* BuildName(DFHack::t_name& name) static DFHack::t_name ReverseBuildName(PyObject* nameObj) { PyObject *temp, *listTemp; - int boolTemp, arrLength; - Py_ssize_t listLength, strLength; + int boolTemp; + Py_ssize_t strLength; char* strTemp; DFHack::t_name name; @@ -231,38 +223,33 @@ static DFHack::t_name ReverseBuildName(PyObject* nameObj) boolTemp = (int)PyInt_AsLong(temp); + Py_DECREF(temp); + if(boolTemp != 0) name.has_name = true; else name.has_name = false; - //I seriously doubt the name arrays will change length, but why take chances? listTemp = PyObject_GetAttrString(nameObj, "words"); - arrLength = sizeof(name.words) / sizeof(uint32_t); - listLength = PyList_Size(listTemp); - - if(listLength < arrLength) - arrLength = listLength; - - for(int i = 0; i < arrLength; i++) + for(int i = 0; i < NAME_WORD_COUNT; i++) name.words[i] = (uint32_t)PyInt_AsLong(PyList_GetItem(listTemp, i)); - listTemp = PyObject_GetAttrString(nameObj, "parts_of_speech"); - - arrLength = sizeof(name.parts_of_speech) / sizeof(uint16_t); - listLength = PyList_Size(listTemp); + Py_DECREF(listTemp); - if(listLength < arrLength) - arrLength = listLength; + listTemp = PyObject_GetAttrString(nameObj, "parts_of_speech"); - for(int i = 0; i < arrLength; i++) + for(int i = 0; i < NAME_WORD_COUNT; i++) name.parts_of_speech[i] = (uint16_t)PyInt_AsLong(PyList_GetItem(listTemp, i)); + Py_DECREF(listTemp); + temp = PyObject_GetAttrString(nameObj, "first_name"); strLength = PyString_Size(temp); strTemp = PyString_AsString(temp); + Py_DECREF(temp); + if(strLength > 128) { strncpy(name.first_name, strTemp, 127); @@ -278,6 +265,8 @@ static DFHack::t_name ReverseBuildName(PyObject* nameObj) strLength = PyString_Size(temp); strTemp = PyString_AsString(temp); + Py_DECREF(temp); + if(strLength > 128) { strncpy(name.nickname, strTemp, 127); From 097b65b5bad231f123d34c830b3c19219d73392f Mon Sep 17 00:00:00 2001 From: doomchild Date: Mon, 26 Apr 2010 10:32:06 -0500 Subject: [PATCH 3/3] added error check to TranslateName --- dfhack/python/DF_Translate.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dfhack/python/DF_Translate.cpp b/dfhack/python/DF_Translate.cpp index 1ce635f5c..a72b9c917 100644 --- a/dfhack/python/DF_Translate.cpp +++ b/dfhack/python/DF_Translate.cpp @@ -196,6 +196,12 @@ static PyObject* DF_Translate_TranslateName(DF_Translate* self, PyObject* args) if(PyArg_ParseTuple(args, "O|i", &nameObj, &inEnglish)) return NULL; + if(PyObject_IsInstance(nameObj, Name_type) != 1) + { + PyErr_SetString(PyExc_TypeError, "argument 1 must be a Name object"); + return NULL; + } + name = ReverseBuildName(nameObj); std::string nameStr = self->tran_Ptr->TranslateName(name, (bool)inEnglish);