From: Martin v. Löwis Date: Thu, 12 Dec 2002 20:03:19 +0000 (+0000) Subject: Patch #650653: Raise always value error if the table is not 256 bytes long. X-Git-Tag: v2.3c1~3042 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=00b6127097f10d7fb03ab5abcd213141b878a8fd;p=python Patch #650653: Raise always value error if the table is not 256 bytes long. --- diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py index 3eb8785374..b64917b449 100644 --- a/Lib/test/string_tests.py +++ b/Lib/test/string_tests.py @@ -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', '!', '') diff --git a/Objects/stringobject.c b/Objects/stringobject.c index 3a69aa9efa..7937b46a3d 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -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;