diff options
author | Charles-François Natali <cf.natali@gmail.com> | 2014-06-19 21:45:09 (GMT) |
---|---|---|
committer | Charles-François Natali <cf.natali@gmail.com> | 2014-06-19 21:45:09 (GMT) |
commit | e4cda6ce21dc268e308f0ee95e097f667f680d43 (patch) | |
tree | 083b7fbe6e01e752a0c367729b513e9dcc5c9bfd /Objects | |
parent | cee4f034385902659ec1ade666e17537eb2bc399 (diff) | |
parent | 077c9564b7f0c09352d1d918f38d2cb75fe61881 (diff) | |
download | cpython-e4cda6ce21dc268e308f0ee95e097f667f680d43.zip cpython-e4cda6ce21dc268e308f0ee95e097f667f680d43.tar.gz cpython-e4cda6ce21dc268e308f0ee95e097f667f680d43.tar.bz2 |
Merge.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/bytearrayobject.c | 6 | ||||
-rw-r--r-- | Objects/exceptions.c | 12 | ||||
-rw-r--r-- | Objects/fileobject.c | 14 | ||||
-rw-r--r-- | Objects/moduleobject.c | 6 | ||||
-rw-r--r-- | Objects/setobject.c | 8 | ||||
-rw-r--r-- | Objects/stringlib/formatter.h | 18 | ||||
-rw-r--r-- | Objects/stringlib/transmogrify.h | 38 | ||||
-rw-r--r-- | Objects/stringobject.c | 72 | ||||
-rw-r--r-- | Objects/tupleobject.c | 6 | ||||
-rw-r--r-- | Objects/unicodeobject.c | 8 |
10 files changed, 96 insertions, 92 deletions
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 21c58ce..fd0ce7c 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -994,10 +994,8 @@ bytearray_repr(PyByteArrayObject *self) *p++ = *quote_postfix++; } *p = '\0'; - if (_PyString_Resize(&v, (p - PyString_AS_STRING(v)))) { - Py_DECREF(v); - return NULL; - } + /* v is cleared on error */ + (void)_PyString_Resize(&v, (p - PyString_AS_STRING(v))); return v; } } diff --git a/Objects/exceptions.c b/Objects/exceptions.c index 0f86cfb..e165528 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -1648,6 +1648,10 @@ UnicodeEncodeError_str(PyObject *self) PyObject *reason_str = NULL; PyObject *encoding_str = NULL; + if (!uself->object) + /* Not properly initialized. */ + return PyUnicode_FromString(""); + /* Get reason and encoding as strings, which they might not be if they've been modified after we were contructed. */ reason_str = PyObject_Str(uself->reason); @@ -1733,6 +1737,10 @@ UnicodeDecodeError_str(PyObject *self) PyObject *reason_str = NULL; PyObject *encoding_str = NULL; + if (!uself->object) + /* Not properly initialized. */ + return PyUnicode_FromString(""); + /* Get reason and encoding as strings, which they might not be if they've been modified after we were contructed. */ reason_str = PyObject_Str(uself->reason); @@ -1830,6 +1838,10 @@ UnicodeTranslateError_str(PyObject *self) PyObject *result = NULL; PyObject *reason_str = NULL; + if (!uself->object) + /* Not properly initialized. */ + return PyUnicode_FromString(""); + /* Get reason as a string, which it might not be if it's been modified after we were contructed. */ reason_str = PyObject_Str(uself->reason); diff --git a/Objects/fileobject.c b/Objects/fileobject.c index c5e0961..5594058 100644 --- a/Objects/fileobject.c +++ b/Objects/fileobject.c @@ -1941,13 +1941,13 @@ file_writelines(PyFileObject *f, PyObject *seq) PyObject *v = PyList_GET_ITEM(list, i); if (!PyString_Check(v)) { const char *buffer; - if (((f->f_binary && - PyObject_AsReadBuffer(v, - (const void**)&buffer, - &len)) || - PyObject_AsCharBuffer(v, - &buffer, - &len))) { + int res; + if (f->f_binary) { + res = PyObject_AsReadBuffer(v, (const void**)&buffer, &len); + } else { + res = PyObject_AsCharBuffer(v, &buffer, &len); + } + if (res) { PyErr_SetString(PyExc_TypeError, "writelines() argument must be a sequence of strings"); goto error; diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c index 08e9740..f2fed30 100644 --- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -122,7 +122,8 @@ _PyModule_Clear(PyObject *m) if (s[0] == '_' && s[1] != '_') { if (Py_VerboseFlag > 1) PySys_WriteStderr("# clear[1] %s\n", s); - PyDict_SetItem(d, key, Py_None); + if (PyDict_SetItem(d, key, Py_None) != 0) + PyErr_Clear(); } } } @@ -135,7 +136,8 @@ _PyModule_Clear(PyObject *m) if (s[0] != '_' || strcmp(s, "__builtins__") != 0) { if (Py_VerboseFlag > 1) PySys_WriteStderr("# clear[2] %s\n", s); - PyDict_SetItem(d, key, Py_None); + if (PyDict_SetItem(d, key, Py_None) != 0) + PyErr_Clear(); } } } diff --git a/Objects/setobject.c b/Objects/setobject.c index db5cee2..b4b1178 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -1796,12 +1796,8 @@ set_richcompare(PySetObject *v, PyObject *w, int op) PyObject *r1, *r2; if(!PyAnySet_Check(w)) { - if (op == Py_EQ) - Py_RETURN_FALSE; - if (op == Py_NE) - Py_RETURN_TRUE; - PyErr_SetString(PyExc_TypeError, "can only compare to a set"); - return NULL; + Py_INCREF(Py_NotImplemented); + return Py_NotImplemented; } switch (op) { case Py_EQ: diff --git a/Objects/stringlib/formatter.h b/Objects/stringlib/formatter.h index fd22751..122abe6 100644 --- a/Objects/stringlib/formatter.h +++ b/Objects/stringlib/formatter.h @@ -180,8 +180,9 @@ parse_internal_render_format_spec(STRINGLIB_CHAR *format_spec, Py_ssize_t consumed; int align_specified = 0; + int fill_char_specified = 0; - format->fill_char = '\0'; + format->fill_char = ' '; format->align = default_align; format->alternate = 0; format->sign = '\0'; @@ -195,6 +196,7 @@ parse_internal_render_format_spec(STRINGLIB_CHAR *format_spec, if (end-ptr >= 2 && is_alignment_token(ptr[1])) { format->align = ptr[1]; format->fill_char = ptr[0]; + fill_char_specified = 1; align_specified = 1; ptr += 2; } @@ -218,7 +220,7 @@ parse_internal_render_format_spec(STRINGLIB_CHAR *format_spec, } /* The special case for 0-padding (backwards compat) */ - if (format->fill_char == '\0' && end-ptr >= 1 && ptr[0] == '0') { + if (!fill_char_specified && end-ptr >= 1 && ptr[0] == '0') { format->fill_char = '0'; if (!align_specified) { format->align = '='; @@ -715,8 +717,7 @@ format_string_internal(PyObject *value, const InternalFormatSpec *format) /* Write into that space. First the padding. */ p = fill_padding(STRINGLIB_STR(result), len, - format->fill_char=='\0'?' ':format->fill_char, - lpad, rpad); + format->fill_char, lpad, rpad); /* Then the source string. */ memcpy(p, STRINGLIB_STR(value), len * sizeof(STRINGLIB_CHAR)); @@ -893,8 +894,7 @@ format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format, /* Populate the memory. */ fill_number(STRINGLIB_STR(result), &spec, pnumeric_chars, n_digits, - prefix, format->fill_char == '\0' ? ' ' : format->fill_char, - &locale, format->type == 'X'); + prefix, format->fill_char, &locale, format->type == 'X'); done: Py_XDECREF(tmp); @@ -1048,8 +1048,7 @@ format_float_internal(PyObject *value, /* Populate the memory. */ fill_number(STRINGLIB_STR(result), &spec, p, n_digits, NULL, - format->fill_char == '\0' ? ' ' : format->fill_char, &locale, - 0); + format->fill_char, &locale, 0); done: PyMem_Free(buf); @@ -1265,8 +1264,7 @@ format_complex_internal(PyObject *value, /* Populate the memory. First, the padding. */ p = fill_padding(STRINGLIB_STR(result), n_re_total + n_im_total + 1 + add_parens * 2, - format->fill_char=='\0' ? ' ' : format->fill_char, - lpad, rpad); + format->fill_char, lpad, rpad); if (add_parens) *p++ = '('; diff --git a/Objects/stringlib/transmogrify.h b/Objects/stringlib/transmogrify.h index 1e132e5..be595a6 100644 --- a/Objects/stringlib/transmogrify.h +++ b/Objects/stringlib/transmogrify.h @@ -15,7 +15,7 @@ stringlib_expandtabs(PyObject *self, PyObject *args) { const char *e, *p; char *q; - size_t i, j; + Py_ssize_t i, j; PyObject *u; int tabsize = 8; @@ -25,35 +25,31 @@ stringlib_expandtabs(PyObject *self, PyObject *args) /* First pass: determine size of output string */ i = j = 0; e = STRINGLIB_STR(self) + STRINGLIB_LEN(self); - for (p = STRINGLIB_STR(self); p < e; p++) + for (p = STRINGLIB_STR(self); p < e; p++) { if (*p == '\t') { if (tabsize > 0) { - j += tabsize - (j % tabsize); - if (j > PY_SSIZE_T_MAX) { - PyErr_SetString(PyExc_OverflowError, - "result is too long"); - return NULL; - } + Py_ssize_t incr = tabsize - (j % tabsize); + if (j > PY_SSIZE_T_MAX - incr) + goto overflow; + j += incr; } } else { + if (j > PY_SSIZE_T_MAX - 1) + goto overflow; j++; if (*p == '\n' || *p == '\r') { + if (i > PY_SSIZE_T_MAX - j) + goto overflow; i += j; j = 0; - if (i > PY_SSIZE_T_MAX) { - PyErr_SetString(PyExc_OverflowError, - "result is too long"); - return NULL; - } } } - - if ((i + j) > PY_SSIZE_T_MAX) { - PyErr_SetString(PyExc_OverflowError, "result is too long"); - return NULL; } + if (i > PY_SSIZE_T_MAX - j) + goto overflow; + /* Second pass: create output string and fill it */ u = STRINGLIB_NEW(NULL, i + j); if (!u) @@ -62,7 +58,7 @@ stringlib_expandtabs(PyObject *self, PyObject *args) j = 0; q = STRINGLIB_STR(u); - for (p = STRINGLIB_STR(self); p < e; p++) + for (p = STRINGLIB_STR(self); p < e; p++) { if (*p == '\t') { if (tabsize > 0) { i = tabsize - (j % tabsize); @@ -77,8 +73,12 @@ stringlib_expandtabs(PyObject *self, PyObject *args) if (*p == '\n' || *p == '\r') j = 0; } - + } + return u; + overflow: + PyErr_SetString(PyExc_OverflowError, "result too long"); + return NULL; } Py_LOCAL_INLINE(PyObject *) diff --git a/Objects/stringobject.c b/Objects/stringobject.c index e1ea3cd..0b6d36c 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -748,8 +748,8 @@ PyObject *PyString_DecodeEscape(const char *s, UTF-8 bytes may follow. */ } } - if (p-buf < newlen && _PyString_Resize(&v, p - buf)) - goto failed; + if (p-buf < newlen) + _PyString_Resize(&v, p - buf); /* v is cleared on error */ return v; failed: Py_DECREF(v); @@ -3091,24 +3091,25 @@ string_expandtabs(PyStringObject *self, PyObject *args) i = 0; /* chars up to and including most recent \n or \r */ j = 0; /* chars since most recent \n or \r (use in tab calculations) */ e = PyString_AS_STRING(self) + PyString_GET_SIZE(self); /* end of input */ - for (p = PyString_AS_STRING(self); p < e; p++) - if (*p == '\t') { - if (tabsize > 0) { - incr = tabsize - (j % tabsize); - if (j > PY_SSIZE_T_MAX - incr) - goto overflow1; - j += incr; + for (p = PyString_AS_STRING(self); p < e; p++) { + if (*p == '\t') { + if (tabsize > 0) { + incr = tabsize - (j % tabsize); + if (j > PY_SSIZE_T_MAX - incr) + goto overflow1; + j += incr; + } } - } - else { - if (j > PY_SSIZE_T_MAX - 1) - goto overflow1; - j++; - if (*p == '\n' || *p == '\r') { - if (i > PY_SSIZE_T_MAX - j) + else { + if (j > PY_SSIZE_T_MAX - 1) goto overflow1; - i += j; - j = 0; + j++; + if (*p == '\n' || *p == '\r') { + if (i > PY_SSIZE_T_MAX - j) + goto overflow1; + i += j; + j = 0; + } } } @@ -3124,25 +3125,26 @@ string_expandtabs(PyStringObject *self, PyObject *args) q = PyString_AS_STRING(u); /* next output char */ qe = PyString_AS_STRING(u) + PyString_GET_SIZE(u); /* end of output */ - for (p = PyString_AS_STRING(self); p < e; p++) - if (*p == '\t') { - if (tabsize > 0) { - i = tabsize - (j % tabsize); - j += i; - while (i--) { - if (q >= qe) - goto overflow2; - *q++ = ' '; + for (p = PyString_AS_STRING(self); p < e; p++) { + if (*p == '\t') { + if (tabsize > 0) { + i = tabsize - (j % tabsize); + j += i; + while (i--) { + if (q >= qe) + goto overflow2; + *q++ = ' '; + } } } - } - else { - if (q >= qe) - goto overflow2; - *q++ = *p; - j++; - if (*p == '\n' || *p == '\r') - j = 0; + else { + if (q >= qe) + goto overflow2; + *q++ = *p; + j++; + if (*p == '\n' || *p == '\r') + j = 0; + } } return u; diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c index 00f2e47..a3c185e 100644 --- a/Objects/tupleobject.c +++ b/Objects/tupleobject.c @@ -881,8 +881,7 @@ _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize) _Py_ForgetReference((PyObject *) v); /* DECREF items deleted by shrinkage */ for (i = newsize; i < oldsize; i++) { - Py_XDECREF(v->ob_item[i]); - v->ob_item[i] = NULL; + Py_CLEAR(v->ob_item[i]); } sv = PyObject_GC_Resize(PyTupleObject, v, newsize); if (sv == NULL) { @@ -928,8 +927,7 @@ PyTuple_Fini(void) #if PyTuple_MAXSAVESIZE > 0 /* empty tuples are used all over the place and applications may * rely on the fact that an empty tuple is a singleton. */ - Py_XDECREF(free_list[0]); - free_list[0] = NULL; + Py_CLEAR(free_list[0]); (void)PyTuple_ClearFreeList(); #endif diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 5ce9c88..29f9bce 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -3520,8 +3520,7 @@ static void make_encode_exception(PyObject **exceptionObject, goto onError; return; onError: - Py_DECREF(*exceptionObject); - *exceptionObject = NULL; + Py_CLEAR(*exceptionObject); } } @@ -4826,8 +4825,7 @@ static void make_translate_exception(PyObject **exceptionObject, goto onError; return; onError: - Py_DECREF(*exceptionObject); - *exceptionObject = NULL; + Py_CLEAR(*exceptionObject); } } @@ -5620,7 +5618,7 @@ PyUnicode_Join(PyObject *separator, PyObject *seq) PyObject *item; Py_ssize_t i; - fseq = PySequence_Fast(seq, ""); + fseq = PySequence_Fast(seq, "can only join an iterable"); if (fseq == NULL) { return NULL; } |