[Zope-Checkins] CVS: Zope/lib/python/Products/PluginIndexes/TextIndexNG/src - normalizer.c:1.1.2.4

Andreas Jung andreas@digicool.com
Sat, 9 Feb 2002 17:37:17 -0500


Update of /cvs-repository/Zope/lib/python/Products/PluginIndexes/TextIndexNG/src
In directory cvs.zope.org:/tmp/cvs-serv12500/src

Modified Files:
      Tag: ajung-textindexng-branch
	normalizer.c 
Log Message:
yet another rewrite - 
- added local copy of the translation table (all 2-tuples are converted
to unicode).
-  all tests pass, (hopefully) leak free


=== Zope/lib/python/Products/PluginIndexes/TextIndexNG/src/normalizer.c 1.1.2.3 => 1.1.2.4 ===
 static PyObject *NormalizeWord(normalizer *self,PyObject *word)
 {
-
     int i;
     PyObject *temp;
 
@@ -39,18 +38,13 @@
         key   = PyTuple_GetItem(item,0);
         value = PyTuple_GetItem(item,1);
 
-        if (PyString_Check(key))
-            key = PyUnicode_FromEncodedObject(key, self->encoding,"strict");
-
-        if (PyString_Check(value))
-            value = PyUnicode_FromEncodedObject(value, self->encoding,"strict");
-
         if (! (s = PyUnicode_Replace( temp, key, value, -1)))
             return NULL;
 
         Py_DECREF(temp);
 
         temp = s;
+
     }
 
     return temp;
@@ -77,16 +71,14 @@
 
             word = NormalizeWord(self, item);
 
-            PyList_Append(list, (PyObject *) word);
+            PyList_Append(list, word);
         }
 
-        Py_DECREF(data);
-
         return list;
 
     } else if (PyUnicode_Check(data) || PyString_Check(data) ) {
 
-        PyObject *word;
+        PyObject *word=NULL;
 
         if (! (word = NormalizeWord(self,data)))
             return NULL;
@@ -106,6 +98,7 @@
     int i;
     PyObject *item,*key,*value;
 
+
     if ( !( PyList_Check(o) || PyTuple_Check(o) )) {
         PyErr_SetString(PyExc_TypeError, "argument must be list or tuple of 2-tuples of strings");
         return 0;
@@ -139,7 +132,6 @@
 
         Py_DECREF(item);
     }
-
     return 1;
 
 err:
@@ -192,26 +184,60 @@
 
 static char *normalizer_args[]={"translation","encoding",NULL};
 
+void CopyTranslationTable(normalizer *self, PyObject *table) {
+    
+    int i;
+
+    self->table = PyList_New(0);
+
+    for (i=0; i<PyList_GET_SIZE(table); i++) {
+        PyObject *item, *key, *value, *tuple;
+
+        item = PyList_GetItem(table, i);
+
+        key   = PyTuple_GetItem(item,0);
+        value = PyTuple_GetItem(item,1);
+
+        if (PyString_Check(key))
+            key = PyUnicode_FromEncodedObject(key, self->encoding,"strict");
+
+        if (PyString_Check(value))
+            value = PyUnicode_FromEncodedObject(value, self->encoding,"strict");
+
+        tuple = PyTuple_New(2);
+
+        PyTuple_SetItem(tuple, 0, key);
+        PyTuple_SetItem(tuple, 1, value);
+
+        PyList_Append(self->table, tuple);
+
+        Py_DECREF(tuple);
+    }
+
+//    Py_DECREF(table);
+}
+
+
+
 static PyObject *
 newnormalizer(PyObject *modinfo, PyObject *args, PyObject *keywds)
 {
     normalizer *self=NULL;
     PyObject *table;
-    static char * encoding = "latin1";
+    char * encoding = "latin1";
 
     if (! (PyArg_ParseTupleAndKeywords(args,keywds,"O|s",normalizer_args,&table,&encoding)))
         return NULL;
 
-    if (! checkList(table))
+    if (! (self = PyObject_NEW(normalizer, &normalizerType)))
         return NULL;
 
-    if (! (self = PyObject_NEW(normalizer, &normalizerType)))
+    if (! checkList(table))
         return NULL;
 
-    self->table = table;
     self->encoding = encoding;
 
-    Py_INCREF(self->table);
+    CopyTranslationTable(self,table);
 
     return (PyObject*)self;
 }
@@ -219,7 +245,7 @@
 static struct PyMethodDef normalizer_module_methods[] =
     {
         { "Normalizer", (PyCFunction)newnormalizer, METH_VARARGS|METH_KEYWORDS,
-            "Normalizer(list) " "-- Normalizer module - takes a list of 2-tuples of strings/unicode strings"
+            "Normalizer(list[,encoding='latin1') " "-- Normalizer module - takes a list of 2-tuples of strings/unicode strings"
         },
         { NULL, NULL }
     };