Skip to content

Commit f93c15a

Browse files
bpo-36311: Fixes decoding multibyte characters around chunk boundaries and improves decoding performance (GH-15083)
(cherry picked from commit 7ebdda0) Co-authored-by: Steve Dower <steve.dower@python.org>
1 parent 9eb3d54 commit f93c15a

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

Lib/test/test_codecs.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3075,13 +3075,13 @@ def test_mbcs_alias(self):
30753075
self.assertEqual(codec.name, 'mbcs')
30763076

30773077
@support.bigmemtest(size=2**31, memuse=7, dry_run=False)
3078-
def test_large_input(self):
3078+
def test_large_input(self, size):
30793079
# Test input longer than INT_MAX.
30803080
# Input should contain undecodable bytes before and after
30813081
# the INT_MAX limit.
3082-
encoded = (b'01234567' * (2**28-1) +
3082+
encoded = (b'01234567' * ((size//8)-1) +
30833083
b'\x85\x86\xea\xeb\xec\xef\xfc\xfd\xfe\xff')
3084-
self.assertEqual(len(encoded), 2**31+2)
3084+
self.assertEqual(len(encoded), size+2)
30853085
decoded = codecs.code_page_decode(932, encoded, 'surrogateescape', True)
30863086
self.assertEqual(decoded[1], len(encoded))
30873087
del encoded
@@ -3092,6 +3092,20 @@ def test_large_input(self):
30923092
'\udc85\udc86\udcea\udceb\udcec'
30933093
'\udcef\udcfc\udcfd\udcfe\udcff')
30943094

3095+
@support.bigmemtest(size=2**31, memuse=6, dry_run=False)
3096+
def test_large_utf8_input(self, size):
3097+
# Test input longer than INT_MAX.
3098+
# Input should contain a decodable multi-byte character
3099+
# surrounding INT_MAX
3100+
encoded = (b'0123456\xed\x84\x80' * (size//8))
3101+
self.assertEqual(len(encoded), size // 8 * 10)
3102+
decoded = codecs.code_page_decode(65001, encoded, 'ignore', True)
3103+
self.assertEqual(decoded[1], len(encoded))
3104+
del encoded
3105+
self.assertEqual(len(decoded[0]), size)
3106+
self.assertEqual(decoded[0][:10], '0123456\ud10001')
3107+
self.assertEqual(decoded[0][-11:], '56\ud1000123456\ud100')
3108+
30953109

30963110
class ASCIITest(unittest.TestCase):
30973111
def test_encode(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Decoding bytes objects larger than 2GiB is faster and no longer fails when a
2+
multibyte characters spans a chunk boundary.

Objects/unicodeobject.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7118,6 +7118,12 @@ PyUnicode_AsASCIIString(PyObject *unicode)
71187118
#define NEED_RETRY
71197119
#endif
71207120

7121+
/* INT_MAX is the theoretical largest chunk (or INT_MAX / 2 when
7122+
transcoding from UTF-16), but INT_MAX / 4 perfoms better in
7123+
both cases also and avoids partial characters overrunning the
7124+
length limit in MultiByteToWideChar on Windows */
7125+
#define DECODING_CHUNK_SIZE (INT_MAX/4)
7126+
71217127
#ifndef WC_ERR_INVALID_CHARS
71227128
# define WC_ERR_INVALID_CHARS 0x0080
71237129
#endif
@@ -7354,8 +7360,8 @@ decode_code_page_stateful(int code_page,
73547360
do
73557361
{
73567362
#ifdef NEED_RETRY
7357-
if (size > INT_MAX) {
7358-
chunk_size = INT_MAX;
7363+
if (size > DECODING_CHUNK_SIZE) {
7364+
chunk_size = DECODING_CHUNK_SIZE;
73597365
final = 0;
73607366
done = 0;
73617367
}
@@ -7759,10 +7765,8 @@ encode_code_page(int code_page,
77597765
do
77607766
{
77617767
#ifdef NEED_RETRY
7762-
/* UTF-16 encoding may double the size, so use only INT_MAX/2
7763-
chunks. */
7764-
if (len > INT_MAX/2) {
7765-
chunk_len = INT_MAX/2;
7768+
if (len > DECODING_CHUNK_SIZE) {
7769+
chunk_len = DECODING_CHUNK_SIZE;
77667770
done = 0;
77677771
}
77687772
else

0 commit comments

Comments
 (0)