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

Skip to content

Commit 9e9dcd6

Browse files
committed
STINNER Victor (haypo)'s patch for bug 3988, Byte warning mode and b'' != ''
Also, his patch to runtests.sh to pass the -bb option (issue 4125).
1 parent e94a37f commit 9e9dcd6

3 files changed

Lines changed: 14 additions & 2 deletions

File tree

Lib/test/test_bytes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import re
1010
import sys
1111
import copy
12+
import operator
1213
import pickle
1314
import tempfile
1415
import unittest
@@ -863,6 +864,17 @@ def test_return_self(self):
863864
b = bytearray()
864865
self.failIf(b.replace(b'', b'') is b)
865866

867+
def test_compare(self):
868+
if sys.flags.bytes_warning:
869+
warnings.simplefilter('error', BytesWarning)
870+
self.assertRaises(BytesWarning, operator.eq, b'', '')
871+
self.assertRaises(BytesWarning, operator.ne, b'', '')
872+
self.assertRaises(BytesWarning, operator.eq, bytearray(b''), '')
873+
self.assertRaises(BytesWarning, operator.ne, bytearray(b''), '')
874+
else:
875+
# raise test.support.TestSkipped("BytesWarning is needed for this test: use -bb option")
876+
pass
877+
866878
# Optimizations:
867879
# __iter__? (optimization)
868880
# __reversed__? (optimization)

Objects/bytearrayobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ bytes_richcompare(PyObject *self, PyObject *other, int op)
939939
error, even if the comparison is for equality. */
940940
if (PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type) ||
941941
PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type)) {
942-
if (Py_BytesWarningFlag && op == Py_EQ) {
942+
if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) {
943943
if (PyErr_WarnEx(PyExc_BytesWarning,
944944
"Comparison between bytearray and string", 1))
945945
return NULL;

Objects/bytesobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ string_richcompare(PyBytesObject *a, PyBytesObject *b, int op)
818818

819819
/* Make sure both arguments are strings. */
820820
if (!(PyBytes_Check(a) && PyBytes_Check(b))) {
821-
if (Py_BytesWarningFlag && (op == Py_EQ) &&
821+
if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE) &&
822822
(PyObject_IsInstance((PyObject*)a,
823823
(PyObject*)&PyUnicode_Type) ||
824824
PyObject_IsInstance((PyObject*)b,

0 commit comments

Comments
 (0)