mirror of
https://github.com/python/cpython.git
synced 2025-10-16 03:40:58 +00:00
Issue #26464: Fix unicode_fast_translate() again
Initialize i variable if the string is non-ASCII.
This commit is contained in:
parent
6c9aa8f2bf
commit
337986740f
2 changed files with 22 additions and 13 deletions
|
@ -341,16 +341,22 @@ class UnicodeTest(string_tests.CommonTest,
|
||||||
"[XXX]")
|
"[XXX]")
|
||||||
self.assertEqual("[a]".translate(str.maketrans({'a': '\xe9'})),
|
self.assertEqual("[a]".translate(str.maketrans({'a': '\xe9'})),
|
||||||
"[\xe9]")
|
"[\xe9]")
|
||||||
|
self.assertEqual('axb'.translate(str.maketrans({'a': None, 'b': '123'})),
|
||||||
|
"x123")
|
||||||
|
self.assertEqual('axb'.translate(str.maketrans({'a': None, 'b': '\xe9'})),
|
||||||
|
"x\xe9")
|
||||||
|
|
||||||
|
# test non-ASCII (don't take the fast-path)
|
||||||
self.assertEqual("[a]".translate(str.maketrans({'a': '<\xe9>'})),
|
self.assertEqual("[a]".translate(str.maketrans({'a': '<\xe9>'})),
|
||||||
"[<\xe9>]")
|
"[<\xe9>]")
|
||||||
self.assertEqual("[\xe9]".translate(str.maketrans({'\xe9': 'a'})),
|
self.assertEqual("[\xe9]".translate(str.maketrans({'\xe9': 'a'})),
|
||||||
"[a]")
|
"[a]")
|
||||||
self.assertEqual("[\xe9]".translate(str.maketrans({'\xe9': None})),
|
self.assertEqual("[\xe9]".translate(str.maketrans({'\xe9': None})),
|
||||||
"[]")
|
"[]")
|
||||||
self.assertEqual('axb'.translate(str.maketrans({'a': None, 'b': '123'})),
|
self.assertEqual("[\xe9]".translate(str.maketrans({'\xe9': '123'})),
|
||||||
"x123")
|
"[123]")
|
||||||
self.assertEqual('axb'.translate(str.maketrans({'a': None, 'b': '\xe9'})),
|
self.assertEqual("[a\xe9]".translate(str.maketrans({'a': '<\u20ac>'})),
|
||||||
"x\xe9")
|
"[<\u20ac>\xe9]")
|
||||||
|
|
||||||
# invalid Unicode characters
|
# invalid Unicode characters
|
||||||
invalid_char = 0x10ffff+1
|
invalid_char = 0x10ffff+1
|
||||||
|
|
|
@ -8582,10 +8582,6 @@ unicode_fast_translate(PyObject *input, PyObject *mapping,
|
||||||
Py_UCS1 *in, *end, *out;
|
Py_UCS1 *in, *end, *out;
|
||||||
int res = 0;
|
int res = 0;
|
||||||
|
|
||||||
if (PyUnicode_READY(input) == -1)
|
|
||||||
return -1;
|
|
||||||
if (!PyUnicode_IS_ASCII(input))
|
|
||||||
return 0;
|
|
||||||
len = PyUnicode_GET_LENGTH(input);
|
len = PyUnicode_GET_LENGTH(input);
|
||||||
|
|
||||||
memset(ascii_table, 0xff, 128);
|
memset(ascii_table, 0xff, 128);
|
||||||
|
@ -8668,13 +8664,20 @@ _PyUnicode_TranslateCharmap(PyObject *input,
|
||||||
|
|
||||||
ignore = (errors != NULL && strcmp(errors, "ignore") == 0);
|
ignore = (errors != NULL && strcmp(errors, "ignore") == 0);
|
||||||
|
|
||||||
res = unicode_fast_translate(input, mapping, &writer, ignore, &i);
|
if (PyUnicode_READY(input) == -1)
|
||||||
if (res < 0) {
|
|
||||||
_PyUnicodeWriter_Dealloc(&writer);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
if (PyUnicode_IS_ASCII(input)) {
|
||||||
|
res = unicode_fast_translate(input, mapping, &writer, ignore, &i);
|
||||||
|
if (res < 0) {
|
||||||
|
_PyUnicodeWriter_Dealloc(&writer);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (res == 1)
|
||||||
|
return _PyUnicodeWriter_Finish(&writer);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
i = 0;
|
||||||
}
|
}
|
||||||
if (res == 1)
|
|
||||||
return _PyUnicodeWriter_Finish(&writer);
|
|
||||||
|
|
||||||
while (i<size) {
|
while (i<size) {
|
||||||
/* try to encode it */
|
/* try to encode it */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue