From: Barry Warsaw Date: Fri, 17 Oct 2008 01:50:37 +0000 (+0000) Subject: STINNER Victor (haypo)'s patch for bug 3988, Byte warning mode and b'' != '' X-Git-Tag: v3.0rc2~62 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=9e9dcd6d4225faa6a8b19120f009e0253d16ab92;p=thirdparty%2FPython%2Fcpython.git 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). --- diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 81d2dad3d21b..c3681437d54c 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -9,6 +9,7 @@ import os import re import sys import copy +import operator import pickle import tempfile import unittest @@ -863,6 +864,17 @@ class AssortedBytesTest(unittest.TestCase): b = bytearray() self.failIf(b.replace(b'', b'') is b) + def test_compare(self): + if sys.flags.bytes_warning: + warnings.simplefilter('error', BytesWarning) + self.assertRaises(BytesWarning, operator.eq, b'', '') + self.assertRaises(BytesWarning, operator.ne, b'', '') + self.assertRaises(BytesWarning, operator.eq, bytearray(b''), '') + self.assertRaises(BytesWarning, operator.ne, bytearray(b''), '') + else: + # raise test.support.TestSkipped("BytesWarning is needed for this test: use -bb option") + pass + # Optimizations: # __iter__? (optimization) # __reversed__? (optimization) diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 03e51e8a43dc..997a83555120 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -939,7 +939,7 @@ bytes_richcompare(PyObject *self, PyObject *other, int op) error, even if the comparison is for equality. */ if (PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type) || PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type)) { - if (Py_BytesWarningFlag && op == Py_EQ) { + if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) { if (PyErr_WarnEx(PyExc_BytesWarning, "Comparison between bytearray and string", 1)) return NULL; diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index 0d0efc9689e9..76b7f522005c 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -818,7 +818,7 @@ string_richcompare(PyBytesObject *a, PyBytesObject *b, int op) /* Make sure both arguments are strings. */ if (!(PyBytes_Check(a) && PyBytes_Check(b))) { - if (Py_BytesWarningFlag && (op == Py_EQ) && + if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE) && (PyObject_IsInstance((PyObject*)a, (PyObject*)&PyUnicode_Type) || PyObject_IsInstance((PyObject*)b,