]> granicus.if.org Git - python/commitdiff
STINNER Victor (haypo)'s patch for bug 3988, Byte warning mode and b'' != ''
authorBarry Warsaw <barry@python.org>
Fri, 17 Oct 2008 01:50:37 +0000 (01:50 +0000)
committerBarry Warsaw <barry@python.org>
Fri, 17 Oct 2008 01:50:37 +0000 (01:50 +0000)
Also, his patch to runtests.sh to pass the -bb option (issue 4125).

Lib/test/test_bytes.py
Objects/bytearrayobject.c
Objects/bytesobject.c

index 81d2dad3d21bdb3b42a3b3ce2d9144939d54451f..c3681437d54c79c5fc1b57072a1e886b8160b8f5 100644 (file)
@@ -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)
index 03e51e8a43dce0ace0df1712f85983471f4be17c..997a835551205521bf02d440d9db760c05afa5ce 100644 (file)
@@ -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;
index 0d0efc9689e93f117b3e660c42968de2b2fedd8f..76b7f522005c5c678f90b8e95989a083c3ca2f97 100644 (file)
@@ -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,