summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2016-08-15 06:46:07 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2016-08-15 06:46:07 (GMT)
commiteb24988962728b3edbd346ced938c4120e7e2eed (patch)
tree3ad9b38b964477f98327ffed1c338b49b1533757
parentd00342347e467981b52368235b99a22dc264dab1 (diff)
downloadcpython-eb24988962728b3edbd346ced938c4120e7e2eed.zip
cpython-eb24988962728b3edbd346ced938c4120e7e2eed.tar.gz
cpython-eb24988962728b3edbd346ced938c4120e7e2eed.tar.bz2
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.
-rw-r--r--Misc/NEWS4
-rw-r--r--Objects/bytearrayobject.c18
-rw-r--r--Objects/bytesobject.c18
3 files changed, 20 insertions, 20 deletions
diff --git a/Misc/NEWS b/Misc/NEWS
index 9e4b2d0..a34289b 100644
--- 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.
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index f8c21d4..de2dca9 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -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;
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 5f77867..ff87dfe 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -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;