[Python-checkins] cpython: Issue #27704: Optimized creating bytes and bytearray from byte-like objects

serhiy.storchaka python-checkins at python.org
Mon Aug 15 02:46:26 EDT 2016


https://hg.python.org/cpython/rev/789a42401009
changeset:   102660:789a42401009
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Mon Aug 15 09:46:07 2016 +0300
summary:
  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.

files:
  Misc/NEWS                 |   4 ++++
  Objects/bytearrayobject.c |  18 ++++++++----------
  Objects/bytesobject.c     |  18 ++++++++----------
  3 files changed, 20 insertions(+), 20 deletions(-)


diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,10 @@
 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.
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -795,17 +795,15 @@
     }
 
     /* 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;
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -2563,17 +2563,15 @@
         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;

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list