]> granicus.if.org Git - python/commitdiff
Patch #650653: Raise always value error if the table is not 256 bytes long.
authorMartin v. Löwis <martin@v.loewis.de>
Thu, 12 Dec 2002 20:03:19 +0000 (20:03 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Thu, 12 Dec 2002 20:03:19 +0000 (20:03 +0000)
Lib/test/string_tests.py
Objects/stringobject.c

index 3eb8785374fdae9ff24329fac1a23c6961f963bf..b64917b4492a24f1e3c9c4d0357c955bab2d60c3 100644 (file)
@@ -192,6 +192,8 @@ def run_method_tests(test):
     table = string.maketrans('a', 'A')
     test('translate', 'abc', 'Abc', table)
     test('translate', 'xyz', 'xyz', table)
+    test('translate', 'xyz', ValueError, 'too short', 'strip')
+    test('translate', 'xyz', ValueError, 'too short')
 
     test('replace', 'one!two!three!', 'one@two!three!', '!', '@', 1)
     test('replace', 'one!two!three!', 'onetwothree', '!', '')
index 3a69aa9efa591262d6f81a416473596ff9b03781..7937b46a3d9454a45e9363fd71c9c3823c690672 100644 (file)
@@ -2070,6 +2070,12 @@ string_translate(PyStringObject *self, PyObject *args)
        else if (PyObject_AsCharBuffer(tableobj, &table1, &tablen))
                return NULL;
 
+       if (tablen != 256) {
+               PyErr_SetString(PyExc_ValueError,
+                 "translation table must be 256 characters long");
+               return NULL;
+       }
+
        if (delobj != NULL) {
                if (PyString_Check(delobj)) {
                        del_table = PyString_AS_STRING(delobj);
@@ -2084,12 +2090,6 @@ string_translate(PyStringObject *self, PyObject *args)
 #endif
                else if (PyObject_AsCharBuffer(delobj, &del_table, &dellen))
                        return NULL;
-
-               if (tablen != 256) {
-                       PyErr_SetString(PyExc_ValueError,
-                         "translation table must be 256 characters long");
-                       return NULL;
-               }
        }
        else {
                del_table = NULL;