Skip to content

Commit 7c722e3

Browse files
[3.9] bpo-45461: Fix IncrementalDecoder and StreamReader in the "unicode-escape" codec (GH-28939) (GH-28945)
They support now splitting escape sequences between input chunks. Add the third parameter "final" in codecs.unicode_escape_decode(). It is True by default to match the former behavior. (cherry picked from commit c96d154) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 38fadbc commit 7c722e3

10 files changed

Lines changed: 14377 additions & 9431 deletions

File tree

‎Doc/data/python3.9.abi‎

Lines changed: 14253 additions & 9401 deletions
Large diffs are not rendered by default.

‎Include/cpython/unicodeobject.h‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,12 +857,20 @@ PyAPI_FUNC(PyObject*) _PyUnicode_EncodeUTF16(
857857

858858
/* --- Unicode-Escape Codecs ---------------------------------------------- */
859859

860+
/* Variant of PyUnicode_DecodeUnicodeEscape that supports partial decoding. */
861+
PyAPI_FUNC(PyObject*) _PyUnicode_DecodeUnicodeEscapeStateful(
862+
const char *string, /* Unicode-Escape encoded string */
863+
Py_ssize_t length, /* size of string */
864+
const char *errors, /* error handling */
865+
Py_ssize_t *consumed /* bytes consumed */
866+
);
860867
/* Helper for PyUnicode_DecodeUnicodeEscape that detects invalid escape
861868
chars. */
862-
PyAPI_FUNC(PyObject*) _PyUnicode_DecodeUnicodeEscape(
869+
PyAPI_FUNC(PyObject*) _PyUnicode_DecodeUnicodeEscapeInternal(
863870
const char *string, /* Unicode-Escape encoded string */
864871
Py_ssize_t length, /* size of string */
865872
const char *errors, /* error handling */
873+
Py_ssize_t *consumed, /* bytes consumed */
866874
const char **first_invalid_escape /* on return, points to first
867875
invalid escaped char in
868876
string. */

‎Lib/encodings/unicode_escape.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ class IncrementalEncoder(codecs.IncrementalEncoder):
2121
def encode(self, input, final=False):
2222
return codecs.unicode_escape_encode(input, self.errors)[0]
2323

24-
class IncrementalDecoder(codecs.IncrementalDecoder):
25-
def decode(self, input, final=False):
26-
return codecs.unicode_escape_decode(input, self.errors)[0]
24+
class IncrementalDecoder(codecs.BufferedIncrementalDecoder):
25+
def _buffer_decode(self, input, errors, final):
26+
return codecs.unicode_escape_decode(input, errors, final)
2727

2828
class StreamWriter(Codec,codecs.StreamWriter):
2929
pass
3030

3131
class StreamReader(Codec,codecs.StreamReader):
32-
pass
32+
def decode(self, input, errors='strict'):
33+
return codecs.unicode_escape_decode(input, errors, False)
3334

3435
### encodings module API
3536

‎Lib/test/test_codecs.py‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2327,7 +2327,11 @@ def test_unicode_escape(self):
23272327
(r"\x5c\x55\x30\x30\x31\x31\x30\x30\x30\x30", 10))
23282328

23292329

2330-
class UnicodeEscapeTest(unittest.TestCase):
2330+
class UnicodeEscapeTest(ReadTest, unittest.TestCase):
2331+
encoding = "unicode-escape"
2332+
2333+
test_lone_surrogates = None
2334+
23312335
def test_empty(self):
23322336
self.assertEqual(codecs.unicode_escape_encode(""), (b"", 0))
23332337
self.assertEqual(codecs.unicode_escape_decode(b""), ("", 0))
@@ -2414,6 +2418,44 @@ def test_decode_errors(self):
24142418
self.assertEqual(decode(br"\U00110000", "ignore"), ("", 10))
24152419
self.assertEqual(decode(br"\U00110000", "replace"), ("\ufffd", 10))
24162420

2421+
def test_partial(self):
2422+
self.check_partial(
2423+
"\x00\t\n\r\\\xff\uffff\U00010000",
2424+
[
2425+
'',
2426+
'',
2427+
'',
2428+
'\x00',
2429+
'\x00',
2430+
'\x00\t',
2431+
'\x00\t',
2432+
'\x00\t\n',
2433+
'\x00\t\n',
2434+
'\x00\t\n\r',
2435+
'\x00\t\n\r',
2436+
'\x00\t\n\r\\',
2437+
'\x00\t\n\r\\',
2438+
'\x00\t\n\r\\',
2439+
'\x00\t\n\r\\',
2440+
'\x00\t\n\r\\\xff',
2441+
'\x00\t\n\r\\\xff',
2442+
'\x00\t\n\r\\\xff',
2443+
'\x00\t\n\r\\\xff',
2444+
'\x00\t\n\r\\\xff',
2445+
'\x00\t\n\r\\\xff',
2446+
'\x00\t\n\r\\\xff\uffff',
2447+
'\x00\t\n\r\\\xff\uffff',
2448+
'\x00\t\n\r\\\xff\uffff',
2449+
'\x00\t\n\r\\\xff\uffff',
2450+
'\x00\t\n\r\\\xff\uffff',
2451+
'\x00\t\n\r\\\xff\uffff',
2452+
'\x00\t\n\r\\\xff\uffff',
2453+
'\x00\t\n\r\\\xff\uffff',
2454+
'\x00\t\n\r\\\xff\uffff',
2455+
'\x00\t\n\r\\\xff\uffff',
2456+
'\x00\t\n\r\\\xff\uffff\U00010000',
2457+
]
2458+
)
24172459

24182460
class RawUnicodeEscapeTest(unittest.TestCase):
24192461
def test_empty(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix incremental decoder and stream reader in the "unicode-escape" codec.
2+
Previously they failed if the escape sequence was split.

‎Modules/_codecsmodule.c‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -487,17 +487,20 @@ _codecs_utf_32_ex_decode_impl(PyObject *module, Py_buffer *data,
487487
_codecs.unicode_escape_decode
488488
data: Py_buffer(accept={str, buffer})
489489
errors: str(accept={str, NoneType}) = None
490+
final: bool(accept={int}) = True
490491
/
491492
[clinic start generated code]*/
492493

493494
static PyObject *
494495
_codecs_unicode_escape_decode_impl(PyObject *module, Py_buffer *data,
495-
const char *errors)
496-
/*[clinic end generated code: output=3ca3c917176b82ab input=8328081a3a569bd6]*/
496+
const char *errors, int final)
497+
/*[clinic end generated code: output=b284f97b12c635ee input=6154f039a9f7c639]*/
497498
{
498-
PyObject *decoded = PyUnicode_DecodeUnicodeEscape(data->buf, data->len,
499-
errors);
500-
return codec_tuple(decoded, data->len);
499+
Py_ssize_t consumed = data->len;
500+
PyObject *decoded = _PyUnicode_DecodeUnicodeEscapeStateful(data->buf, data->len,
501+
errors,
502+
final ? NULL : &consumed);
503+
return codec_tuple(decoded, consumed);
501504
}
502505

503506
/*[clinic input]

‎Modules/clinic/_codecsmodule.c.h‎

Lines changed: 18 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Objects/unicodeobject.c‎

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6271,9 +6271,10 @@ PyUnicode_AsUTF16String(PyObject *unicode)
62716271
static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL;
62726272

62736273
PyObject *
6274-
_PyUnicode_DecodeUnicodeEscape(const char *s,
6274+
_PyUnicode_DecodeUnicodeEscapeInternal(const char *s,
62756275
Py_ssize_t size,
62766276
const char *errors,
6277+
Py_ssize_t *consumed,
62776278
const char **first_invalid_escape)
62786279
{
62796280
const char *starts = s;
@@ -6286,6 +6287,9 @@ _PyUnicode_DecodeUnicodeEscape(const char *s,
62866287
*first_invalid_escape = NULL;
62876288

62886289
if (size == 0) {
6290+
if (consumed) {
6291+
*consumed = 0;
6292+
}
62896293
_Py_RETURN_UNICODE_EMPTY();
62906294
}
62916295
/* Escaped strings will always be longer than the resulting
@@ -6336,7 +6340,7 @@ _PyUnicode_DecodeUnicodeEscape(const char *s,
63366340
/* \ - Escapes */
63376341
if (s >= end) {
63386342
message = "\\ at end of string";
6339-
goto error;
6343+
goto incomplete;
63406344
}
63416345
c = (unsigned char) *s++;
63426346

@@ -6390,7 +6394,10 @@ _PyUnicode_DecodeUnicodeEscape(const char *s,
63906394
count = 8;
63916395
message = "truncated \\UXXXXXXXX escape";
63926396
hexescape:
6393-
for (ch = 0; count && s < end; ++s, --count) {
6397+
for (ch = 0; count; ++s, --count) {
6398+
if (s >= end) {
6399+
goto incomplete;
6400+
}
63946401
c = (unsigned char)*s;
63956402
ch <<= 4;
63966403
if (c >= '0' && c <= '9') {
@@ -6403,12 +6410,9 @@ _PyUnicode_DecodeUnicodeEscape(const char *s,
64036410
ch += c - ('A' - 10);
64046411
}
64056412
else {
6406-
break;
6413+
goto error;
64076414
}
64086415
}
6409-
if (count) {
6410-
goto error;
6411-
}
64126416

64136417
/* when we get here, ch is a 32-bit unicode character */
64146418
if (ch > MAX_UNICODE) {
@@ -6435,14 +6439,20 @@ _PyUnicode_DecodeUnicodeEscape(const char *s,
64356439
}
64366440

64376441
message = "malformed \\N character escape";
6438-
if (s < end && *s == '{') {
6442+
if (s >= end) {
6443+
goto incomplete;
6444+
}
6445+
if (*s == '{') {
64396446
const char *start = ++s;
64406447
size_t namelen;
64416448
/* look for the closing brace */
64426449
while (s < end && *s != '}')
64436450
s++;
6451+
if (s >= end) {
6452+
goto incomplete;
6453+
}
64446454
namelen = s - start;
6445-
if (namelen && s < end) {
6455+
if (namelen) {
64466456
/* found a name. look it up in the unicode database */
64476457
s++;
64486458
ch = 0xffffffff; /* in case 'getcode' messes up */
@@ -6468,6 +6478,11 @@ _PyUnicode_DecodeUnicodeEscape(const char *s,
64686478
continue;
64696479
}
64706480

6481+
incomplete:
6482+
if (consumed) {
6483+
*consumed = startinpos;
6484+
break;
6485+
}
64716486
error:
64726487
endinpos = s-starts;
64736488
writer.min_length = end - s + writer.pos;
@@ -6496,12 +6511,14 @@ _PyUnicode_DecodeUnicodeEscape(const char *s,
64966511
}
64976512

64986513
PyObject *
6499-
PyUnicode_DecodeUnicodeEscape(const char *s,
6514+
_PyUnicode_DecodeUnicodeEscapeStateful(const char *s,
65006515
Py_ssize_t size,
6501-
const char *errors)
6516+
const char *errors,
6517+
Py_ssize_t *consumed)
65026518
{
65036519
const char *first_invalid_escape;
6504-
PyObject *result = _PyUnicode_DecodeUnicodeEscape(s, size, errors,
6520+
PyObject *result = _PyUnicode_DecodeUnicodeEscapeInternal(s, size, errors,
6521+
consumed,
65056522
&first_invalid_escape);
65066523
if (result == NULL)
65076524
return NULL;
@@ -6516,6 +6533,14 @@ PyUnicode_DecodeUnicodeEscape(const char *s,
65166533
return result;
65176534
}
65186535

6536+
PyObject *
6537+
PyUnicode_DecodeUnicodeEscape(const char *s,
6538+
Py_ssize_t size,
6539+
const char *errors)
6540+
{
6541+
return _PyUnicode_DecodeUnicodeEscapeStateful(s, size, errors, NULL);
6542+
}
6543+
65196544
/* Return a Unicode-Escape string version of the Unicode object. */
65206545

65216546
PyObject *

‎Parser/pegen/parse_string.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ decode_unicode_with_escapes(Parser *parser, const char *s, size_t len, Token *t)
120120
s = buf;
121121

122122
const char *first_invalid_escape;
123-
v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
123+
v = _PyUnicode_DecodeUnicodeEscapeInternal(s, len, NULL, NULL, &first_invalid_escape);
124124

125125
if (v != NULL && first_invalid_escape != NULL) {
126126
if (warn_invalid_escape_sequence(parser, *first_invalid_escape, t) < 0) {

‎Python/ast.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4640,7 +4640,7 @@ decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
46404640
s = buf;
46414641

46424642
const char *first_invalid_escape;
4643-
v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4643+
v = _PyUnicode_DecodeUnicodeEscapeInternal(s, len, NULL, NULL, &first_invalid_escape);
46444644

46454645
if (v != NULL && first_invalid_escape != NULL) {
46464646
if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {

0 commit comments

Comments
 (0)