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.
This commit is contained in:
Serhiy Storchaka 2016-08-15 09:46:07 +03:00
parent d00342347e
commit eb24988962
3 changed files with 20 additions and 20 deletions

View file

@ -10,6 +10,10 @@ What's New in Python 3.6.0 alpha 4
Core and Builtins 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 - Issue #26823: Large sections of repeated lines in tracebacks are now
abbreviated as "[Previous line repeated {count} more times]" by the builtin abbreviated as "[Previous line repeated {count} more times]" by the builtin
traceback rendering. Patch by Emanuel Barry. traceback rendering. Patch by Emanuel Barry.

View file

@ -795,17 +795,15 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
} }
/* Is it an int? */ /* Is it an int? */
if (PyIndex_Check(arg)) {
count = PyNumber_AsSsize_t(arg, PyExc_OverflowError); count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
if (count == -1 && PyErr_Occurred()) { if (count == -1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_OverflowError))
return -1; return -1;
PyErr_Clear();
} }
else if (count < 0) { if (count < 0) {
PyErr_SetString(PyExc_ValueError, "negative count"); PyErr_SetString(PyExc_ValueError, "negative count");
return -1; return -1;
} }
else {
if (count > 0) { if (count > 0) {
if (PyByteArray_Resize((PyObject *)self, count)) if (PyByteArray_Resize((PyObject *)self, count))
return -1; return -1;

View file

@ -2563,17 +2563,15 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL; return NULL;
} }
/* Is it an integer? */ /* Is it an integer? */
if (PyIndex_Check(x)) {
size = PyNumber_AsSsize_t(x, PyExc_OverflowError); size = PyNumber_AsSsize_t(x, PyExc_OverflowError);
if (size == -1 && PyErr_Occurred()) { if (size == -1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_OverflowError))
return NULL; return NULL;
PyErr_Clear();
} }
else if (size < 0) { if (size < 0) {
PyErr_SetString(PyExc_ValueError, "negative count"); PyErr_SetString(PyExc_ValueError, "negative count");
return NULL; return NULL;
} }
else {
new = _PyBytes_FromSize(size, 1); new = _PyBytes_FromSize(size, 1);
if (new == NULL) if (new == NULL)
return NULL; return NULL;