Skip to content

Commit 735a960

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 <[email protected]>
1 parent c5bba85 commit 735a960

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
@@ -3229,13 +3229,13 @@ def test_mbcs_alias(self):
32293229
self.assertEqual(codec.name, 'mbcs')
32303230

32313231
@support.bigmemtest(size=2**31, memuse=7, dry_run=False)
3232-
def test_large_input(self):
3232+
def test_large_input(self, size):
32333233
# Test input longer than INT_MAX.
32343234
# Input should contain undecodable bytes before and after
32353235
# the INT_MAX limit.
3236-
encoded = (b'01234567' * (2**28-1) +
3236+
encoded = (b'01234567' * ((size//8)-1) +
32373237
b'\x85\x86\xea\xeb\xec\xef\xfc\xfd\xfe\xff')
3238-
self.assertEqual(len(encoded), 2**31+2)
3238+
self.assertEqual(len(encoded), size+2)
32393239
decoded = codecs.code_page_decode(932, encoded, 'surrogateescape', True)
32403240
self.assertEqual(decoded[1], len(encoded))
32413241
del encoded
@@ -3246,6 +3246,20 @@ def test_large_input(self):
32463246
'\udc85\udc86\udcea\udceb\udcec'
32473247
'\udcef\udcfc\udcfd\udcfe\udcff')
32483248

3249+
@support.bigmemtest(size=2**31, memuse=6, dry_run=False)
3250+
def test_large_utf8_input(self, size):
3251+
# Test input longer than INT_MAX.
3252+
# Input should contain a decodable multi-byte character
3253+
# surrounding INT_MAX
3254+
encoded = (b'0123456\xed\x84\x80' * (size//8))
3255+
self.assertEqual(len(encoded), size // 8 * 10)
3256+
decoded = codecs.code_page_decode(65001, encoded, 'ignore', True)
3257+
self.assertEqual(decoded[1], len(encoded))
3258+
del encoded
3259+
self.assertEqual(len(decoded[0]), size)
3260+
self.assertEqual(decoded[0][:10], '0123456\ud10001')
3261+
self.assertEqual(decoded[0][-11:], '56\ud1000123456\ud100')
3262+
32493263

32503264
class ASCIITest(unittest.TestCase):
32513265
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
@@ -7085,6 +7085,12 @@ PyUnicode_AsASCIIString(PyObject *unicode)
70857085
#define NEED_RETRY
70867086
#endif
70877087

7088+
/* INT_MAX is the theoretical largest chunk (or INT_MAX / 2 when
7089+
transcoding from UTF-16), but INT_MAX / 4 perfoms better in
7090+
both cases also and avoids partial characters overrunning the
7091+
length limit in MultiByteToWideChar on Windows */
7092+
#define DECODING_CHUNK_SIZE (INT_MAX/4)
7093+
70887094
#ifndef WC_ERR_INVALID_CHARS
70897095
# define WC_ERR_INVALID_CHARS 0x0080
70907096
#endif
@@ -7345,8 +7351,8 @@ decode_code_page_stateful(int code_page,
73457351
do
73467352
{
73477353
#ifdef NEED_RETRY
7348-
if (size > INT_MAX) {
7349-
chunk_size = INT_MAX;
7354+
if (size > DECODING_CHUNK_SIZE) {
7355+
chunk_size = DECODING_CHUNK_SIZE;
73507356
final = 0;
73517357
done = 0;
73527358
}
@@ -7748,10 +7754,8 @@ encode_code_page(int code_page,
77487754
do
77497755
{
77507756
#ifdef NEED_RETRY
7751-
/* UTF-16 encoding may double the size, so use only INT_MAX/2
7752-
chunks. */
7753-
if (len > INT_MAX/2) {
7754-
chunk_len = INT_MAX/2;
7757+
if (len > DECODING_CHUNK_SIZE) {
7758+
chunk_len = DECODING_CHUNK_SIZE;
77557759
done = 0;
77567760
}
77577761
else

0 commit comments

Comments
 (0)