cpython: edf029fc9591 (original) (raw)

Mercurial > cpython

changeset 83433:edf029fc9591

Close #17694: Add minimum length to _PyUnicodeWriter * Add also min_char attribute to _PyUnicodeWriter structure (currently unused) * _PyUnicodeWriter_Init() has no more argument (except the writer itself): min_length and overallocate must be set explicitly * In error handlers, only enable overallocation if the replacement string is longer than 1 character * CJK decoders don't use overallocation anymore * Set min_length, instead of preallocating memory using _PyUnicodeWriter_Prepare(), in many decoders * _PyUnicode_DecodeUnicodeInternal() checks for integer overflow [#17694]

Victor Stinner victor.stinner@gmail.com
date Wed, 17 Apr 2013 23:02:17 +0200
parents 5755ee168bd7
children 7eb52460c999
files Include/unicodeobject.h Modules/cjkcodecs/multibytecodec.c Objects/complexobject.c Objects/floatobject.c Objects/longobject.c Objects/stringlib/unicode_format.h Objects/unicodeobject.c
diffstat 7 files changed, 85 insertions(+), 75 deletions(-)[+] [-] Include/unicodeobject.h 20 Modules/cjkcodecs/multibytecodec.c 9 Objects/complexobject.c 2 Objects/floatobject.c 2 Objects/longobject.c 2 Objects/stringlib/unicode_format.h 6 Objects/unicodeobject.c 119

line wrap: on

line diff

--- a/Include/unicodeobject.h +++ b/Include/unicodeobject.h @@ -898,22 +898,28 @@ typedef struct { Py_UCS4 maxchar; Py_ssize_t size; Py_ssize_t pos;

+

+

--- a/Modules/cjkcodecs/multibytecodec.c +++ b/Modules/cjkcodecs/multibytecodec.c @@ -633,7 +633,8 @@ MultibyteCodec_Decode(MultibyteCodecObje return make_tuple(PyUnicode_New(0, 0), 0); }

@@ -1037,7 +1038,7 @@ mbidecoder_decode(MultibyteIncrementalDe data = pdata.buf; size = pdata.len;

--- a/Objects/complexobject.c +++ b/Objects/complexobject.c @@ -705,7 +705,7 @@ complex__format__(PyObject* self, PyObje if (!PyArg_ParseTuple(args, "U:format", &format_spec)) return NULL;

--- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -1711,7 +1711,7 @@ float__format__(PyObject *self, PyObject if (!PyArg_ParseTuple(args, "U:format", &format_spec)) return NULL;

--- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -4379,7 +4379,7 @@ long__format__(PyObject *self, PyObject if (!PyArg_ParseTuple(args, "U:format", &format_spec)) return NULL;

--- a/Objects/stringlib/unicode_format.h +++ b/Objects/stringlib/unicode_format.h @@ -906,7 +906,6 @@ build_string(SubString *input, PyObject int recursion_depth, AutoNumber *auto_number) { _PyUnicodeWriter writer;

/* check the recursion level */ if (recursion_depth <= 0) { @@ -915,8 +914,9 @@ build_string(SubString *input, PyObject return NULL; }

if (!do_markup(input, args, kwargs, &writer, recursion_depth, auto_number)) {

--- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -2665,7 +2665,9 @@ PyUnicode_FromFormatV(const char *format const char *f; _PyUnicodeWriter writer;

/* va_list may be an array (of 1 item) on some platforms (ex: AMD64). Copy it to be able to pass a reference to a subfunction. */ @@ -4117,7 +4119,10 @@ unicode_decode_call_errorhandler_writer( goto onError; }

@@ -4256,9 +4261,8 @@ PyUnicode_DecodeUTF7Stateful(const char } /* Start off assuming it's all ASCII. Widen later as necessary. */

shiftOutStart = 0; e = s + size; @@ -4655,7 +4659,7 @@ PyUnicode_DecodeUTF8Stateful(const char return get_latin1_char((unsigned char)s[0]); }

@@ -4910,7 +4914,7 @@ PyUnicode_DecodeUTF32Stateful(const char le = bo <= 0; #endif

@@ -5149,7 +5153,7 @@ PyUnicode_DecodeUTF16Stateful(const char /* Note: size will always be longer than the resulting Unicode character count */

@@ -5420,11 +5424,9 @@ PyUnicode_DecodeUnicodeEscape(const char and we determined it's exact size (common case) or it contains \x, \u, ... escape sequences. then we create a legacy wchar string and resize it at the end of this function. */

@@ -5432,8 +5434,7 @@ PyUnicode_DecodeUnicodeEscape(const char length after conversion to the true value. (but if the error callback returns a long replacement string we'll have to allocate more space) */

- switch (c) { /* \x escapes */ @@ -5787,9 +5784,8 @@ PyUnicode_DecodeRawUnicodeEscape(const c Unicode string, so we start with size here and then reduce the length after conversion to the true value. (But decoding error handler might have to resize the string) */

end = s + size; while (s < end) { @@ -5982,12 +5978,14 @@ PyObject * if (size == 0) _Py_RETURN_UNICODE_EMPTY();

+ end = s + size; - while (s < end) { Py_UNICODE uch; Py_UCS4 ch; @@ -6429,9 +6427,9 @@ PyUnicode_DecodeASCII(const char *s, if (size == 1 && (unsigned char)s[0] < 128) return get_latin1_char((unsigned char)s[0]);

e = s + size; data = writer.data; @@ -7280,7 +7278,7 @@ PyUnicode_DecodeCharmap(const char *s, if (size == 0) _Py_RETURN_UNICODE_EMPTY();

@@ -7312,7 +7310,7 @@ PyUnicode_DecodeCharmap(const char *s, ch = *s; x = mapdata_ucs1[ch]; if (x > maxchar) {

@@ -12841,21 +12839,27 @@ unicode_endswith(PyObject *self, Py_LOCAL_INLINE(void) _PyUnicodeWriter_Update(_PyUnicodeWriter *writer) {

void -_PyUnicodeWriter_Init(_PyUnicodeWriter *writer, Py_ssize_t min_length) +_PyUnicodeWriter_Init(_PyUnicodeWriter *writer) { memset(writer, 0, sizeof(writer)); #ifdef Py_DEBUG writer->kind = 5; / invalid kind */ #endif

} int @@ -12873,29 +12877,28 @@ int } newlen = writer->pos + length;

+ if (writer->buffer == NULL) {

+ writer->buffer = PyUnicode_New(newlen, maxchar); if (writer->buffer == NULL) return -1;

-

if (maxchar > writer->maxchar || writer->readonly) { /* resize + widen */ @@ -12913,7 +12916,6 @@ int return -1; } writer->buffer = newbuffer;

@@ -12924,8 +12926,8 @@ int writer->buffer, 0, writer->pos); Py_DECREF(writer->buffer); writer->buffer = newbuffer;

@@ -12959,11 +12961,10 @@ int maxchar = PyUnicode_MAX_CHAR_VALUE(str); if (maxchar > writer->maxchar || len > writer->size - writer->pos) { if (writer->buffer == NULL && !writer->overallocate) {

@@ -13080,7 +13081,7 @@ unicode__format__(PyObject* self, PyObje if (PyUnicode_READY(self) == -1) return NULL;

@@ -14164,7 +14165,9 @@ PyUnicode_Format(PyObject *format, PyObj ctx.fmtcnt = PyUnicode_GET_LENGTH(ctx.fmtstr); ctx.fmtpos = 0;

if (PyTuple_Check(args)) { ctx.arglen = PyTuple_Size(args);