diff Objects/bytesobject.c @ 52768:643f44ad387f

Optimize slicing of bytes and bytearray by avoiding useless copying. This restores the behavior that was present in Python 2.x.
author Alexandre Vassalotti <alexandre@peadrop.com>
date Fri, 03 Apr 2009 06:38:02 +0000
parents debd14c489b9
children 28096ed73153
line wrap: on
line diff
--- a/Objects/bytesobject.c	Fri Apr 03 06:19:27 2009 +0000
+++ b/Objects/bytesobject.c	Fri Apr 03 06:38:02 2009 +0000
@@ -951,19 +951,17 @@
 				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;
 		}
 	}