mirror of https://github.com/python/cpython
Issue #21118: Add unit test for invalid character replacement (code point higher than U+10ffff)
This commit is contained in:
parent
89a76abf20
commit
4ff33af257
|
@ -280,6 +280,14 @@ class UnicodeTest(string_tests.CommonTest,
|
||||||
self.assertEqual("[\xe9]".translate(str.maketrans({'\xe9': None})),
|
self.assertEqual("[\xe9]".translate(str.maketrans({'\xe9': None})),
|
||||||
"[]")
|
"[]")
|
||||||
|
|
||||||
|
# invalid Unicode characters
|
||||||
|
invalid_char = 0x10ffff+1
|
||||||
|
for before in "a\xe9\u20ac\U0010ffff":
|
||||||
|
mapping = str.maketrans({before: invalid_char})
|
||||||
|
text = "[%s]" % before
|
||||||
|
self.assertRaises(ValueError, text.translate, mapping)
|
||||||
|
|
||||||
|
# errors
|
||||||
self.assertRaises(TypeError, self.type2test.maketrans)
|
self.assertRaises(TypeError, self.type2test.maketrans)
|
||||||
self.assertRaises(ValueError, self.type2test.maketrans, 'abc', 'defg')
|
self.assertRaises(ValueError, self.type2test.maketrans, 'abc', 'defg')
|
||||||
self.assertRaises(TypeError, self.type2test.maketrans, 2, 'def')
|
self.assertRaises(TypeError, self.type2test.maketrans, 2, 'def')
|
||||||
|
|
|
@ -8473,10 +8473,10 @@ charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result)
|
||||||
}
|
}
|
||||||
else if (PyLong_Check(x)) {
|
else if (PyLong_Check(x)) {
|
||||||
long value = PyLong_AS_LONG(x);
|
long value = PyLong_AS_LONG(x);
|
||||||
long max = PyUnicode_GetMax();
|
if (value < 0 || value > MAX_UNICODE) {
|
||||||
if (value < 0 || value > max) {
|
PyErr_Format(PyExc_ValueError,
|
||||||
PyErr_Format(PyExc_TypeError,
|
"character mapping must be in range(0x%x)",
|
||||||
"character mapping must be in range(0x%x)", max+1);
|
MAX_UNICODE+1);
|
||||||
Py_DECREF(x);
|
Py_DECREF(x);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -8522,7 +8522,9 @@ charmaptranslate_output(Py_UCS4 ch, PyObject *mapping,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (PyLong_Check(item)) {
|
if (PyLong_Check(item)) {
|
||||||
Py_UCS4 ch = (Py_UCS4)PyLong_AS_LONG(item);
|
long ch = (Py_UCS4)PyLong_AS_LONG(item);
|
||||||
|
/* PyLong_AS_LONG() cannot fail, charmaptranslate_lookup() already
|
||||||
|
used it */
|
||||||
if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0) {
|
if (_PyUnicodeWriter_WriteCharInline(writer, ch) < 0) {
|
||||||
Py_DECREF(item);
|
Py_DECREF(item);
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -8570,11 +8572,9 @@ unicode_fast_translate_lookup(PyObject *mapping, Py_UCS1 ch,
|
||||||
|
|
||||||
if (PyLong_Check(item)) {
|
if (PyLong_Check(item)) {
|
||||||
long replace = (Py_UCS4)PyLong_AS_LONG(item);
|
long replace = (Py_UCS4)PyLong_AS_LONG(item);
|
||||||
if (replace == -1) {
|
/* PyLong_AS_LONG() cannot fail, charmaptranslate_lookup() already
|
||||||
Py_DECREF(item);
|
used it */
|
||||||
return -1;
|
if (127 < replace) {
|
||||||
}
|
|
||||||
if (replace < 0 || 127 < replace) {
|
|
||||||
/* invalid character or character outside ASCII:
|
/* invalid character or character outside ASCII:
|
||||||
skip the fast translate */
|
skip the fast translate */
|
||||||
goto exit;
|
goto exit;
|
||||||
|
|
Loading…
Reference in New Issue