Skip to content

Commit b15e221

Browse files
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.
1 parent 3d2a091 commit b15e221

3 files changed

Lines changed: 20 additions & 20 deletions

File tree

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ What's New in Python 3.6.0 alpha 4
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #27704: Optimized creating bytes and bytearray from byte-like objects
14+
and iterables. Speed up to 3 times for short objects. Original patch by
15+
Naoki Inada.
16+
1317
- Issue #26823: Large sections of repeated lines in tracebacks are now
1418
abbreviated as "[Previous line repeated {count} more times]" by the builtin
1519
traceback rendering. Patch by Emanuel Barry.

Objects/bytearrayobject.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -795,17 +795,15 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
795795
}
796796

797797
/* Is it an int? */
798-
count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
799-
if (count == -1 && PyErr_Occurred()) {
800-
if (PyErr_ExceptionMatches(PyExc_OverflowError))
798+
if (PyIndex_Check(arg)) {
799+
count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
800+
if (count == -1 && PyErr_Occurred()) {
801801
return -1;
802-
PyErr_Clear();
803-
}
804-
else if (count < 0) {
805-
PyErr_SetString(PyExc_ValueError, "negative count");
806-
return -1;
807-
}
808-
else {
802+
}
803+
if (count < 0) {
804+
PyErr_SetString(PyExc_ValueError, "negative count");
805+
return -1;
806+
}
809807
if (count > 0) {
810808
if (PyByteArray_Resize((PyObject *)self, count))
811809
return -1;

Objects/bytesobject.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2563,17 +2563,15 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
25632563
return NULL;
25642564
}
25652565
/* Is it an integer? */
2566-
size = PyNumber_AsSsize_t(x, PyExc_OverflowError);
2567-
if (size == -1 && PyErr_Occurred()) {
2568-
if (PyErr_ExceptionMatches(PyExc_OverflowError))
2566+
if (PyIndex_Check(x)) {
2567+
size = PyNumber_AsSsize_t(x, PyExc_OverflowError);
2568+
if (size == -1 && PyErr_Occurred()) {
25692569
return NULL;
2570-
PyErr_Clear();
2571-
}
2572-
else if (size < 0) {
2573-
PyErr_SetString(PyExc_ValueError, "negative count");
2574-
return NULL;
2575-
}
2576-
else {
2570+
}
2571+
if (size < 0) {
2572+
PyErr_SetString(PyExc_ValueError, "negative count");
2573+
return NULL;
2574+
}
25772575
new = _PyBytes_FromSize(size, 1);
25782576
if (new == NULL)
25792577
return NULL;

0 commit comments

Comments
 (0)