Skip to content

Commit 313467e

Browse files
bpo-42435: Speed up comparison of bytes and bytearray object (GH--23461)
* Speed up comparison of bytes objects with non-bytes objects when option -b is specified. * Speed up comparison of bytarray objects with non-buffer object.
1 parent 5ef53a8 commit 313467e

File tree

3 files changed

+14
-33
lines changed

3 files changed

+14
-33
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Speed up comparison of bytes objects with non-bytes objects when option :option:`-b`
2+
is specified. Speed up comparison of bytarray objects with non-buffer object.

Objects/bytearrayobject.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,23 +1005,19 @@ bytearray_richcompare(PyObject *self, PyObject *other, int op)
10051005
{
10061006
Py_ssize_t self_size, other_size;
10071007
Py_buffer self_bytes, other_bytes;
1008-
int cmp, rc;
1008+
int cmp;
10091009

10101010
/* Bytes can be compared to anything that supports the (binary)
10111011
buffer API. Except that a comparison with Unicode is always an
10121012
error, even if the comparison is for equality. */
1013-
rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type);
1014-
if (!rc)
1015-
rc = PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type);
1016-
if (rc < 0)
1017-
return NULL;
1018-
if (rc) {
1019-
if (_Py_GetConfig()->bytes_warning && (op == Py_EQ || op == Py_NE)) {
1020-
if (PyErr_WarnEx(PyExc_BytesWarning,
1021-
"Comparison between bytearray and string", 1))
1022-
return NULL;
1013+
if (!PyObject_CheckBuffer(self) || !PyObject_CheckBuffer(other)) {
1014+
if (PyUnicode_Check(self) || PyUnicode_Check(other)) {
1015+
if (_Py_GetConfig()->bytes_warning && (op == Py_EQ || op == Py_NE)) {
1016+
if (PyErr_WarnEx(PyExc_BytesWarning,
1017+
"Comparison between bytearray and string", 1))
1018+
return NULL;
1019+
}
10231020
}
1024-
10251021
Py_RETURN_NOTIMPLEMENTED;
10261022
}
10271023

Objects/bytesobject.c

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,36 +1538,19 @@ bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op)
15381538
int c;
15391539
Py_ssize_t len_a, len_b;
15401540
Py_ssize_t min_len;
1541-
int rc;
15421541

15431542
/* Make sure both arguments are strings. */
15441543
if (!(PyBytes_Check(a) && PyBytes_Check(b))) {
15451544
if (_Py_GetConfig()->bytes_warning && (op == Py_EQ || op == Py_NE)) {
1546-
rc = PyObject_IsInstance((PyObject*)a,
1547-
(PyObject*)&PyUnicode_Type);
1548-
if (!rc)
1549-
rc = PyObject_IsInstance((PyObject*)b,
1550-
(PyObject*)&PyUnicode_Type);
1551-
if (rc < 0)
1552-
return NULL;
1553-
if (rc) {
1545+
if (PyUnicode_Check(a) || PyUnicode_Check(b)) {
15541546
if (PyErr_WarnEx(PyExc_BytesWarning,
15551547
"Comparison between bytes and string", 1))
15561548
return NULL;
15571549
}
1558-
else {
1559-
rc = PyObject_IsInstance((PyObject*)a,
1560-
(PyObject*)&PyLong_Type);
1561-
if (!rc)
1562-
rc = PyObject_IsInstance((PyObject*)b,
1563-
(PyObject*)&PyLong_Type);
1564-
if (rc < 0)
1550+
if (PyLong_Check(a) || PyLong_Check(b)) {
1551+
if (PyErr_WarnEx(PyExc_BytesWarning,
1552+
"Comparison between bytes and int", 1))
15651553
return NULL;
1566-
if (rc) {
1567-
if (PyErr_WarnEx(PyExc_BytesWarning,
1568-
"Comparison between bytes and int", 1))
1569-
return NULL;
1570-
}
15711554
}
15721555
}
15731556
Py_RETURN_NOTIMPLEMENTED;

0 commit comments

Comments
 (0)