mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
[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 c96d1546b1
)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
38fadbc5b9
commit
7c722e32bf
10 changed files with 9836 additions and 4890 deletions
|
@ -487,17 +487,20 @@ _codecs_utf_32_ex_decode_impl(PyObject *module, Py_buffer *data,
|
|||
_codecs.unicode_escape_decode
|
||||
data: Py_buffer(accept={str, buffer})
|
||||
errors: str(accept={str, NoneType}) = None
|
||||
final: bool(accept={int}) = True
|
||||
/
|
||||
[clinic start generated code]*/
|
||||
|
||||
static PyObject *
|
||||
_codecs_unicode_escape_decode_impl(PyObject *module, Py_buffer *data,
|
||||
const char *errors)
|
||||
/*[clinic end generated code: output=3ca3c917176b82ab input=8328081a3a569bd6]*/
|
||||
const char *errors, int final)
|
||||
/*[clinic end generated code: output=b284f97b12c635ee input=6154f039a9f7c639]*/
|
||||
{
|
||||
PyObject *decoded = PyUnicode_DecodeUnicodeEscape(data->buf, data->len,
|
||||
errors);
|
||||
return codec_tuple(decoded, data->len);
|
||||
Py_ssize_t consumed = data->len;
|
||||
PyObject *decoded = _PyUnicode_DecodeUnicodeEscapeStateful(data->buf, data->len,
|
||||
errors,
|
||||
final ? NULL : &consumed);
|
||||
return codec_tuple(decoded, consumed);
|
||||
}
|
||||
|
||||
/*[clinic input]
|
||||
|
|
23
Modules/clinic/_codecsmodule.c.h
generated
23
Modules/clinic/_codecsmodule.c.h
generated
|
@ -1149,7 +1149,7 @@ exit:
|
|||
}
|
||||
|
||||
PyDoc_STRVAR(_codecs_unicode_escape_decode__doc__,
|
||||
"unicode_escape_decode($module, data, errors=None, /)\n"
|
||||
"unicode_escape_decode($module, data, errors=None, final=True, /)\n"
|
||||
"--\n"
|
||||
"\n");
|
||||
|
||||
|
@ -1158,7 +1158,7 @@ PyDoc_STRVAR(_codecs_unicode_escape_decode__doc__,
|
|||
|
||||
static PyObject *
|
||||
_codecs_unicode_escape_decode_impl(PyObject *module, Py_buffer *data,
|
||||
const char *errors);
|
||||
const char *errors, int final);
|
||||
|
||||
static PyObject *
|
||||
_codecs_unicode_escape_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
|
||||
|
@ -1166,8 +1166,9 @@ _codecs_unicode_escape_decode(PyObject *module, PyObject *const *args, Py_ssize_
|
|||
PyObject *return_value = NULL;
|
||||
Py_buffer data = {NULL, NULL};
|
||||
const char *errors = NULL;
|
||||
int final = 1;
|
||||
|
||||
if (!_PyArg_CheckPositional("unicode_escape_decode", nargs, 1, 2)) {
|
||||
if (!_PyArg_CheckPositional("unicode_escape_decode", nargs, 1, 3)) {
|
||||
goto exit;
|
||||
}
|
||||
if (PyUnicode_Check(args[0])) {
|
||||
|
@ -1208,8 +1209,20 @@ _codecs_unicode_escape_decode(PyObject *module, PyObject *const *args, Py_ssize_
|
|||
_PyArg_BadArgument("unicode_escape_decode", "argument 2", "str or None", args[1]);
|
||||
goto exit;
|
||||
}
|
||||
if (nargs < 3) {
|
||||
goto skip_optional;
|
||||
}
|
||||
if (PyFloat_Check(args[2])) {
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"integer argument expected, got float" );
|
||||
goto exit;
|
||||
}
|
||||
final = _PyLong_AsInt(args[2]);
|
||||
if (final == -1 && PyErr_Occurred()) {
|
||||
goto exit;
|
||||
}
|
||||
skip_optional:
|
||||
return_value = _codecs_unicode_escape_decode_impl(module, &data, errors);
|
||||
return_value = _codecs_unicode_escape_decode_impl(module, &data, errors, final);
|
||||
|
||||
exit:
|
||||
/* Cleanup for data */
|
||||
|
@ -2922,4 +2935,4 @@ exit:
|
|||
#ifndef _CODECS_CODE_PAGE_ENCODE_METHODDEF
|
||||
#define _CODECS_CODE_PAGE_ENCODE_METHODDEF
|
||||
#endif /* !defined(_CODECS_CODE_PAGE_ENCODE_METHODDEF) */
|
||||
/*[clinic end generated code: output=51b42d170889524c input=a9049054013a1b77]*/
|
||||
/*[clinic end generated code: output=d4b696fe54cfee8f input=a9049054013a1b77]*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue