]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #27704: Optimized creating bytes and bytearray from byte-like objects
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 15 Aug 2016 06:46:07 +0000 (09:46 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Mon, 15 Aug 2016 06:46:07 +0000 (09:46 +0300)
and iterables.  Speed up to 3 times for short objects.  Original patch by
Naoki Inada.

Misc/NEWS
Objects/bytearrayobject.c
Objects/bytesobject.c

index 9e4b2d01db1a8508f16cb422b5881a3fe79b4c81..a34289bebb58cbeb6ef81dbe0046806b8327acef 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,10 @@ What's New in Python 3.6.0 alpha 4
 Core and Builtins
 -----------------
 
+- Issue #27704: Optimized creating bytes and bytearray from byte-like objects
+  and iterables.  Speed up to 3 times for short objects.  Original patch by
+  Naoki Inada.
+
 - Issue #26823: Large sections of repeated lines in tracebacks are now
   abbreviated as "[Previous line repeated {count} more times]" by the builtin
   traceback rendering. Patch by Emanuel Barry.
index f8c21d4e623c15561868f1a74a7924588cbd82ee..de2dca95ec8a86661a0b781a81dbbf5bd5c371a2 100644 (file)
@@ -795,17 +795,15 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
     }
 
     /* Is it an int? */
-    count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
-    if (count == -1 && PyErr_Occurred()) {
-        if (PyErr_ExceptionMatches(PyExc_OverflowError))
+    if (PyIndex_Check(arg)) {
+        count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
+        if (count == -1 && PyErr_Occurred()) {
             return -1;
-        PyErr_Clear();
-    }
-    else if (count < 0) {
-        PyErr_SetString(PyExc_ValueError, "negative count");
-        return -1;
-    }
-    else {
+        }
+        if (count < 0) {
+            PyErr_SetString(PyExc_ValueError, "negative count");
+            return -1;
+        }
         if (count > 0) {
             if (PyByteArray_Resize((PyObject *)self, count))
                 return -1;
index 5f7786726e22cf1c1cfeadec3583b3ecef2bc07d..ff87dfe775c98efb0f141aef8dc5bb6cad819304 100644 (file)
@@ -2563,17 +2563,15 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
         return NULL;
     }
     /* Is it an integer? */
-    size = PyNumber_AsSsize_t(x, PyExc_OverflowError);
-    if (size == -1 && PyErr_Occurred()) {
-        if (PyErr_ExceptionMatches(PyExc_OverflowError))
+    if (PyIndex_Check(x)) {
+        size = PyNumber_AsSsize_t(x, PyExc_OverflowError);
+        if (size == -1 && PyErr_Occurred()) {
             return NULL;
-        PyErr_Clear();
-    }
-    else if (size < 0) {
-        PyErr_SetString(PyExc_ValueError, "negative count");
-        return NULL;
-    }
-    else {
+        }
+        if (size < 0) {
+            PyErr_SetString(PyExc_ValueError, "negative count");
+            return NULL;
+        }
         new = _PyBytes_FromSize(size, 1);
         if (new == NULL)
             return NULL;