summaryrefslogtreecommitdiffstats
path: root/Objects/bytesobject.c
diff options
context:
space:
mode:
authorAlexandre Vassalotti <alexandre@peadrop.com>2009-04-03 06:38:02 (GMT)
committerAlexandre Vassalotti <alexandre@peadrop.com>2009-04-03 06:38:02 (GMT)
commite2641f45b6305f9371cecdcb8765998e2946ca94 (patch)
tree320793635bb0dd2574ee2e561426f9c7fd88abac /Objects/bytesobject.c
parent1102062abb05e6558ecd93b014e532014b8eb4bf (diff)
downloadcpython-e2641f45b6305f9371cecdcb8765998e2946ca94.zip
cpython-e2641f45b6305f9371cecdcb8765998e2946ca94.tar.gz
cpython-e2641f45b6305f9371cecdcb8765998e2946ca94.tar.bz2
Optimize slicing of bytes and bytearray by avoiding useless copying.
This restores the behavior that was present in Python 2.x.
Diffstat (limited to 'Objects/bytesobject.c')
-rw-r--r--Objects/bytesobject.c12
1 files changed, 5 insertions, 7 deletions
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index a4a2e65..d3b598e 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -951,19 +951,17 @@ string_subscript(PyBytesObject* self, PyObject* item)
slicelength);
}
else {
- source_buf = PyBytes_AsString((PyObject*)self);
- result_buf = (char *)PyMem_Malloc(slicelength);
- if (result_buf == NULL)
- return PyErr_NoMemory();
+ source_buf = PyBytes_AS_STRING(self);
+ result = PyBytes_FromStringAndSize(NULL, slicelength);
+ if (result == NULL)
+ return NULL;
+ result_buf = PyBytes_AS_STRING(result);
for (cur = start, i = 0; i < slicelength;
cur += step, i++) {
result_buf[i] = source_buf[cur];
}
- result = PyBytes_FromStringAndSize(result_buf,
- slicelength);
- PyMem_Free(result_buf);
return result;
}
}