Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1dd4982

Browse files
Issue #23681: The -b option now affects comparisons of bytes with int.
1 parent ee4c0b9 commit 1dd4982

5 files changed

Lines changed: 53 additions & 22 deletions

File tree

Doc/using/cmdline.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,12 @@ Miscellaneous options
190190

191191
.. cmdoption:: -b
192192

193-
Issue a warning when comparing str and bytes. Issue an error when the
193+
Issue a warning when comparing :class:`bytes` or :class:`bytearray` with
194+
:class:`str` or :class:`bytes` with :class:`int`. Issue an error when the
194195
option is given twice (:option:`-bb`).
195196

197+
.. versionchanged: 3.5
198+
Affects comparisons of :class:`bytes` with :class:`int`.
196199
197200
.. cmdoption:: -B
198201

Doc/whatsnew/3.5.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ Some smaller changes made to the core Python language are:
162162
error handlers now works with decoding and translating.
163163
(Contributed by Serhiy Storchaka in :issue:`19676` and :issue:`22286`.)
164164

165+
* The :option:`-b` option now affects comparisons of :class:`bytes` with
166+
:class:`int`. (Contributed by Serhiy Storchaka in :issue:`23681`)
165167

166168

167169
New Modules

Lib/test/test_bytes.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,20 +1338,35 @@ def test_return_self(self):
13381338
b = bytearray()
13391339
self.assertFalse(b.replace(b'', b'') is b)
13401340

1341+
@unittest.skipUnless(sys.flags.bytes_warning,
1342+
"BytesWarning is needed for this test: use -bb option")
13411343
def test_compare(self):
1342-
if sys.flags.bytes_warning:
1343-
def bytes_warning():
1344-
return test.support.check_warnings(('', BytesWarning))
1345-
with bytes_warning():
1346-
b'' == ''
1347-
with bytes_warning():
1348-
b'' != ''
1349-
with bytes_warning():
1350-
bytearray(b'') == ''
1351-
with bytes_warning():
1352-
bytearray(b'') != ''
1353-
else:
1354-
self.skipTest("BytesWarning is needed for this test: use -bb option")
1344+
def bytes_warning():
1345+
return test.support.check_warnings(('', BytesWarning))
1346+
with bytes_warning():
1347+
b'' == ''
1348+
with bytes_warning():
1349+
'' == b''
1350+
with bytes_warning():
1351+
b'' != ''
1352+
with bytes_warning():
1353+
'' != b''
1354+
with bytes_warning():
1355+
bytearray(b'') == ''
1356+
with bytes_warning():
1357+
'' == bytearray(b'')
1358+
with bytes_warning():
1359+
bytearray(b'') != ''
1360+
with bytes_warning():
1361+
'' != bytearray(b'')
1362+
with bytes_warning():
1363+
b'\0' == 0
1364+
with bytes_warning():
1365+
0 == b'\0'
1366+
with bytes_warning():
1367+
b'\0' != 0
1368+
with bytes_warning():
1369+
0 != b'\0'
13551370

13561371
# Optimizations:
13571372
# __iter__? (optimization)

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Release date: 2015-03-28
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #23681: The -b option now affects comparisons of bytes with int.
14+
1315
- Issue #23632: Memoryviews now allow tuple indexing (including for
1416
multi-dimensional memoryviews).
1517

Objects/bytesobject.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,14 +1385,23 @@ bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op)
13851385

13861386
/* Make sure both arguments are strings. */
13871387
if (!(PyBytes_Check(a) && PyBytes_Check(b))) {
1388-
if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE) &&
1389-
(PyObject_IsInstance((PyObject*)a,
1390-
(PyObject*)&PyUnicode_Type) ||
1391-
PyObject_IsInstance((PyObject*)b,
1392-
(PyObject*)&PyUnicode_Type))) {
1393-
if (PyErr_WarnEx(PyExc_BytesWarning,
1394-
"Comparison between bytes and string", 1))
1395-
return NULL;
1388+
if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) {
1389+
if (PyObject_IsInstance((PyObject*)a,
1390+
(PyObject*)&PyUnicode_Type) ||
1391+
PyObject_IsInstance((PyObject*)b,
1392+
(PyObject*)&PyUnicode_Type)) {
1393+
if (PyErr_WarnEx(PyExc_BytesWarning,
1394+
"Comparison between bytes and string", 1))
1395+
return NULL;
1396+
}
1397+
else if (PyObject_IsInstance((PyObject*)a,
1398+
(PyObject*)&PyLong_Type) ||
1399+
PyObject_IsInstance((PyObject*)b,
1400+
(PyObject*)&PyLong_Type)) {
1401+
if (PyErr_WarnEx(PyExc_BytesWarning,
1402+
"Comparison between bytes and int", 1))
1403+
return NULL;
1404+
}
13961405
}
13971406
result = Py_NotImplemented;
13981407
}

0 commit comments

Comments
 (0)