diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2016-08-15 06:46:07 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2016-08-15 06:46:07 (GMT) |
commit | eb24988962728b3edbd346ced938c4120e7e2eed (patch) | |
tree | 3ad9b38b964477f98327ffed1c338b49b1533757 /Objects/bytesobject.c | |
parent | d00342347e467981b52368235b99a22dc264dab1 (diff) | |
download | cpython-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.
Diffstat (limited to 'Objects/bytesobject.c')
-rw-r--r-- | Objects/bytesobject.c | 18 |
1 files changed, 8 insertions, 10 deletions
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; |