summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorAlexandre Vassalotti <alexandre@peadrop.com>2008-04-14 22:40:08 (GMT)
committerAlexandre Vassalotti <alexandre@peadrop.com>2008-04-14 22:40:08 (GMT)
commitbcdc4685ba3ee470f5a4ddc85244b3f86a61ff4d (patch)
tree8c5575d142d1ecdb140fc61811c5a0b7ec9f2662 /Objects
parentf9e7ebe165215c8eadd16c7988c5d20c0d114cf5 (diff)
downloadcpython-bcdc4685ba3ee470f5a4ddc85244b3f86a61ff4d.zip
cpython-bcdc4685ba3ee470f5a4ddc85244b3f86a61ff4d.tar.gz
cpython-bcdc4685ba3ee470f5a4ddc85244b3f86a61ff4d.tar.bz2
Backport manually r62342 from the py3k branch to the trunk.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/bytesobject.c40
1 files changed, 23 insertions, 17 deletions
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 0b40f83..542f266 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -2659,7 +2659,7 @@ end of B.");
static PyObject *
bytes_extend(PyBytesObject *self, PyObject *arg)
{
- PyObject *it, *item, *tmp, *res;
+ PyObject *it, *item, *bytes_obj;
Py_ssize_t buf_size = 0, len = 0;
int value;
char *buf;
@@ -2679,40 +2679,46 @@ bytes_extend(PyBytesObject *self, PyObject *arg)
/* Try to determine the length of the argument. 32 is abitrary. */
buf_size = _PyObject_LengthHint(arg, 32);
- buf = (char *)PyMem_Malloc(buf_size * sizeof(char));
- if (buf == NULL)
- return PyErr_NoMemory();
+ bytes_obj = PyBytes_FromStringAndSize(NULL, buf_size);
+ if (bytes_obj == NULL)
+ return NULL;
+ buf = PyBytes_AS_STRING(bytes_obj);
while ((item = PyIter_Next(it)) != NULL) {
if (! _getbytevalue(item, &value)) {
Py_DECREF(item);
Py_DECREF(it);
- PyMem_Free(buf);
+ Py_DECREF(bytes_obj);
return NULL;
}
buf[len++] = value;
Py_DECREF(item);
+
if (len >= buf_size) {
- char *new_buf;
buf_size = len + (len >> 1) + 1;
- new_buf = (char *)PyMem_Realloc(buf, buf_size * sizeof(char));
- if (new_buf == NULL) {
+ if (PyBytes_Resize((PyObject *)bytes_obj, buf_size) < 0) {
Py_DECREF(it);
- PyMem_Free(buf);
- return PyErr_NoMemory();
+ Py_DECREF(bytes_obj);
+ return NULL;
}
- buf = new_buf;
+ /* Recompute the `buf' pointer, since the resizing operation may
+ have invalidated it. */
+ buf = PyBytes_AS_STRING(bytes_obj);
}
}
Py_DECREF(it);
- /* XXX: Is possible to avoid a full copy of the buffer? */
- tmp = PyBytes_FromStringAndSize(buf, len);
- res = bytes_extend(self, tmp);
- Py_DECREF(tmp);
- PyMem_Free(buf);
+ /* Resize down to exact size. */
+ if (PyBytes_Resize((PyObject *)bytes_obj, len) < 0) {
+ Py_DECREF(bytes_obj);
+ return NULL;
+ }
- return res;
+ if (bytes_setslice(self, Py_SIZE(self), Py_SIZE(self), bytes_obj) == -1)
+ return NULL;
+ Py_DECREF(bytes_obj);
+
+ Py_RETURN_NONE;
}
PyDoc_STRVAR(pop__doc__,