Georg Brandl added the comment:

Here's a patch that should make unicode.translate() more robust, and
also allows unicode characters to be passed in the mapping.

__________________________________
Tracker <[EMAIL PROTECTED]>
<http://bugs.python.org/issue1071>
__________________________________
Index: Objects/unicodeobject.c
===================================================================
--- Objects/unicodeobject.c     (Revision 57871)
+++ Objects/unicodeobject.c     (Arbeitskopie)
@@ -7763,10 +7763,48 @@
 static PyObject*
 unicode_translate(PyUnicodeObject *self, PyObject *table)
 {
+    PyObject *keys = NULL;
+    Py_ssize_t i;
+
+    if (!PyDict_Check(table)) {
+        PyErr_SetString(PyExc_TypeError, "translate argument must be a dict");
+        return NULL;
+    }
+    /* fixup the table -- allow string keys instead of only int keys */
+    keys = PyDict_Keys(table);
+    if (!keys) return NULL;
+    for (i = 0; i < PyList_GET_SIZE(keys); i++) {
+        PyObject *key = PyList_GET_ITEM(keys, i);
+        if (PyUnicode_Check(key)) {
+            /* convert string keys to integer keys */
+            PyObject *newkey;
+            if (PyUnicode_GET_SIZE(key) != 1) {
+                PyErr_SetString(PyExc_ValueError, "string items in translate "
+                                "table must be 1 element long");
+                goto err;
+            }
+            newkey = PyInt_FromLong(PyUnicode_AS_UNICODE(key)[0]);
+            if (!newkey)
+                goto err;
+            if (PyDict_SetItem(table, newkey, PyDict_GetItem(table, key)) < 0)
+                goto err;
+            if (PyDict_DelItem(table, key) < 0)
+                goto err;
+        } else if (!PyInt_Check(key)) {
+            PyErr_SetString(PyExc_TypeError, "items in translate table must be 
"
+                            "strings or integers");
+            goto err;
+        }
+    }
+    Py_DECREF(keys);        
+
     return PyUnicode_TranslateCharmap(self->str,
                                      self->length,
                                      table,
                                      "ignore");
+  err:
+    Py_XDECREF(keys);
+    return NULL;
 }
 
 PyDoc_STRVAR(upper__doc__,
_______________________________________________
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to