Commit 88887aa3 authored by Jeremy Hylton's avatar Jeremy Hylton

small updates to string_join:

    use PyString_AS_STRING macro on local string object
    when resizing string, make sure resized string will always be big enough
    split string containing error message across two lines
add test to string_tests that causes resizing
parent 672fac0d
...@@ -123,6 +123,9 @@ def run_method_tests(test): ...@@ -123,6 +123,9 @@ def run_method_tests(test):
test('join', '.', u'a.b.c', ['a', u'b', 'c']) test('join', '.', u'a.b.c', ['a', u'b', 'c'])
test('join', '.', u'a.b.c', ['a', 'b', u'c']) test('join', '.', u'a.b.c', ['a', 'b', u'c'])
test('join', '.', TypeError, ['a', u'b', 3]) test('join', '.', TypeError, ['a', u'b', 3])
for i in [5, 25, 125]:
test('join', '-', ((('a' * i) + '-') * i)[:-1],
['a' * i] * i)
test('join', ' ', TypeError, BadSeq1()) test('join', ' ', TypeError, BadSeq1())
test('join', ' ', 'a b c', BadSeq2()) test('join', ' ', 'a b c', BadSeq2())
......
...@@ -743,7 +743,7 @@ string_join(PyStringObject *self, PyObject *args) ...@@ -743,7 +743,7 @@ string_join(PyStringObject *self, PyObject *args)
char *p; char *p;
int seqlen = 0; int seqlen = 0;
int sz = 100; int sz = 100;
int i, slen; int i, slen, sz_incr;
PyObject *orig, *seq, *item; PyObject *orig, *seq, *item;
if (!PyArg_ParseTuple(args, "O:join", &orig)) if (!PyArg_ParseTuple(args, "O:join", &orig))
...@@ -770,7 +770,7 @@ string_join(PyStringObject *self, PyObject *args) ...@@ -770,7 +770,7 @@ string_join(PyStringObject *self, PyObject *args)
if (!(res = PyString_FromStringAndSize((char*)NULL, sz))) if (!(res = PyString_FromStringAndSize((char*)NULL, sz)))
goto finally; goto finally;
p = PyString_AsString(res); p = PyString_AS_STRING(res);
for (i = 0; i < seqlen; i++) { for (i = 0; i < seqlen; i++) {
item = PySequence_Fast_GET_ITEM(seq, i); item = PySequence_Fast_GET_ITEM(seq, i);
...@@ -781,17 +781,20 @@ string_join(PyStringObject *self, PyObject *args) ...@@ -781,17 +781,20 @@ string_join(PyStringObject *self, PyObject *args)
return PyUnicode_Join((PyObject *)self, seq); return PyUnicode_Join((PyObject *)self, seq);
} }
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"sequence item %i: expected string, %.80s found", "sequence item %i: expected string,"
" %.80s found",
i, item->ob_type->tp_name); i, item->ob_type->tp_name);
goto finally; goto finally;
} }
slen = PyString_GET_SIZE(item); slen = PyString_GET_SIZE(item);
while (reslen + slen + seplen >= sz) { while (reslen + slen + seplen >= sz) {
if (_PyString_Resize(&res, sz*2)) { /* at least double the size of the string */
sz_incr = slen + seplen > sz ? slen + seplen : sz;
if (_PyString_Resize(&res, sz + sz_incr)) {
goto finally; goto finally;
} }
sz *= 2; sz += sz_incr;
p = PyString_AsString(res) + reslen; p = PyString_AS_STRING(res) + reslen;
} }
if (i > 0) { if (i > 0) {
memcpy(p, sep, seplen); memcpy(p, sep, seplen);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment