mirror of
https://github.com/python/cpython.git
synced 2025-10-17 12:18:23 +00:00
Issue #15379: Fix passing of non-BMP characters as integers for the charmap decoder (already working as unicode strings).
Patch by Serhiy Storchaka.
This commit is contained in:
parent
20b8d992b0
commit
6f80f5d444
4 changed files with 135 additions and 3 deletions
|
@ -744,7 +744,7 @@ class CodecCallbackTest(unittest.TestCase):
|
||||||
raise ValueError
|
raise ValueError
|
||||||
self.assertRaises(UnicodeError, codecs.charmap_decode, b"\xff", "strict", {0xff: None})
|
self.assertRaises(UnicodeError, codecs.charmap_decode, b"\xff", "strict", {0xff: None})
|
||||||
self.assertRaises(ValueError, codecs.charmap_decode, b"\xff", "strict", D())
|
self.assertRaises(ValueError, codecs.charmap_decode, b"\xff", "strict", D())
|
||||||
self.assertRaises(TypeError, codecs.charmap_decode, b"\xff", "strict", {0xff: sys.maxunicode+1})
|
self.assertRaises(TypeError, codecs.charmap_decode, b"\xff", "strict", {0xff: 0x110000})
|
||||||
|
|
||||||
def test_encodehelper(self):
|
def test_encodehelper(self):
|
||||||
# enhance coverage of:
|
# enhance coverage of:
|
||||||
|
|
|
@ -1546,6 +1546,10 @@ class CharmapTest(unittest.TestCase):
|
||||||
("abc", 3)
|
("abc", 3)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
self.assertRaises(UnicodeDecodeError,
|
||||||
|
codecs.charmap_decode, b"\x00\x01\x02", "strict", "ab"
|
||||||
|
)
|
||||||
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
codecs.charmap_decode(b"\x00\x01\x02", "replace", "ab"),
|
codecs.charmap_decode(b"\x00\x01\x02", "replace", "ab"),
|
||||||
("ab\ufffd", 3)
|
("ab\ufffd", 3)
|
||||||
|
@ -1572,6 +1576,107 @@ class CharmapTest(unittest.TestCase):
|
||||||
("", len(allbytes))
|
("", len(allbytes))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_decode_with_int2str_map(self):
|
||||||
|
self.assertEqual(
|
||||||
|
codecs.charmap_decode(b"\x00\x01\x02", "strict",
|
||||||
|
{0: 'a', 1: 'b', 2: 'c'}),
|
||||||
|
("abc", 3)
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
codecs.charmap_decode(b"\x00\x01\x02", "strict",
|
||||||
|
{0: 'Aa', 1: 'Bb', 2: 'Cc'}),
|
||||||
|
("AaBbCc", 3)
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
codecs.charmap_decode(b"\x00\x01\x02", "strict",
|
||||||
|
{0: '\U0010FFFF', 1: 'b', 2: 'c'}),
|
||||||
|
("\U0010FFFFbc", 3)
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
codecs.charmap_decode(b"\x00\x01\x02", "strict",
|
||||||
|
{0: 'a', 1: 'b', 2: ''}),
|
||||||
|
("ab", 3)
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertRaises(UnicodeDecodeError,
|
||||||
|
codecs.charmap_decode, b"\x00\x01\x02", "strict",
|
||||||
|
{0: 'a', 1: 'b'}
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
codecs.charmap_decode(b"\x00\x01\x02", "replace",
|
||||||
|
{0: 'a', 1: 'b'}),
|
||||||
|
("ab\ufffd", 3)
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
codecs.charmap_decode(b"\x00\x01\x02", "replace",
|
||||||
|
{0: 'a', 1: 'b', 2: None}),
|
||||||
|
("ab\ufffd", 3)
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
codecs.charmap_decode(b"\x00\x01\x02", "ignore",
|
||||||
|
{0: 'a', 1: 'b'}),
|
||||||
|
("ab", 3)
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
codecs.charmap_decode(b"\x00\x01\x02", "ignore",
|
||||||
|
{0: 'a', 1: 'b', 2: None}),
|
||||||
|
("ab", 3)
|
||||||
|
)
|
||||||
|
|
||||||
|
allbytes = bytes(range(256))
|
||||||
|
self.assertEqual(
|
||||||
|
codecs.charmap_decode(allbytes, "ignore", {}),
|
||||||
|
("", len(allbytes))
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_decode_with_int2int_map(self):
|
||||||
|
a = ord('a')
|
||||||
|
b = ord('b')
|
||||||
|
c = ord('c')
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
codecs.charmap_decode(b"\x00\x01\x02", "strict",
|
||||||
|
{0: a, 1: b, 2: c}),
|
||||||
|
("abc", 3)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Issue #15379
|
||||||
|
self.assertEqual(
|
||||||
|
codecs.charmap_decode(b"\x00\x01\x02", "strict",
|
||||||
|
{0: 0x10FFFF, 1: b, 2: c}),
|
||||||
|
("\U0010FFFFbc", 3)
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertRaises(TypeError,
|
||||||
|
codecs.charmap_decode, b"\x00\x01\x02", "strict",
|
||||||
|
{0: 0x110000, 1: b, 2: c}
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertRaises(UnicodeDecodeError,
|
||||||
|
codecs.charmap_decode, b"\x00\x01\x02", "strict",
|
||||||
|
{0: a, 1: b},
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
codecs.charmap_decode(b"\x00\x01\x02", "replace",
|
||||||
|
{0: a, 1: b}),
|
||||||
|
("ab\ufffd", 3)
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
codecs.charmap_decode(b"\x00\x01\x02", "ignore",
|
||||||
|
{0: a, 1: b}),
|
||||||
|
("ab", 3)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class WithStmtTest(unittest.TestCase):
|
class WithStmtTest(unittest.TestCase):
|
||||||
def test_encodedfile(self):
|
def test_encodedfile(self):
|
||||||
f = io.BytesIO(b"\xc3\xbc")
|
f = io.BytesIO(b"\xc3\xbc")
|
||||||
|
|
|
@ -10,6 +10,9 @@ What's New in Python 3.2.4
|
||||||
Core and Builtins
|
Core and Builtins
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
- Issue #15379: Fix passing of non-BMP characters as integers for the charmap
|
||||||
|
decoder (already working as unicode strings). Patch by Serhiy Storchaka.
|
||||||
|
|
||||||
- Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
|
- Issue #13992: The trashcan mechanism is now thread-safe. This eliminates
|
||||||
sporadic crashes in multi-thread programs when several long deallocator
|
sporadic crashes in multi-thread programs when several long deallocator
|
||||||
chains ran concurrently and involved subclasses of built-in container
|
chains ran concurrently and involved subclasses of built-in container
|
||||||
|
|
|
@ -5250,12 +5250,36 @@ PyObject *PyUnicode_DecodeCharmap(const char *s,
|
||||||
/* Apply mapping */
|
/* Apply mapping */
|
||||||
if (PyLong_Check(x)) {
|
if (PyLong_Check(x)) {
|
||||||
long value = PyLong_AS_LONG(x);
|
long value = PyLong_AS_LONG(x);
|
||||||
if (value < 0 || value > 65535) {
|
if (value < 0 || value > 0x10FFFF) {
|
||||||
PyErr_SetString(PyExc_TypeError,
|
PyErr_SetString(PyExc_TypeError,
|
||||||
"character mapping must be in range(65536)");
|
"character mapping must be in range(0x110000)");
|
||||||
Py_DECREF(x);
|
Py_DECREF(x);
|
||||||
goto onError;
|
goto onError;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef Py_UNICODE_WIDE
|
||||||
|
if (value > 0xFFFF) {
|
||||||
|
/* see the code for 1-n mapping below */
|
||||||
|
if (extrachars < 2) {
|
||||||
|
/* resize first */
|
||||||
|
Py_ssize_t oldpos = p - PyUnicode_AS_UNICODE(v);
|
||||||
|
Py_ssize_t needed = 10 - extrachars;
|
||||||
|
extrachars += needed;
|
||||||
|
/* XXX overflow detection missing */
|
||||||
|
if (_PyUnicode_Resize(&v,
|
||||||
|
PyUnicode_GET_SIZE(v) + needed) < 0) {
|
||||||
|
Py_DECREF(x);
|
||||||
|
goto onError;
|
||||||
|
}
|
||||||
|
p = PyUnicode_AS_UNICODE(v) + oldpos;
|
||||||
|
}
|
||||||
|
value -= 0x10000;
|
||||||
|
*p++ = 0xD800 | (value >> 10);
|
||||||
|
*p++ = 0xDC00 | (value & 0x3FF);
|
||||||
|
extrachars -= 2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
#endif
|
||||||
*p++ = (Py_UNICODE)value;
|
*p++ = (Py_UNICODE)value;
|
||||||
}
|
}
|
||||||
else if (x == Py_None) {
|
else if (x == Py_None) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue