]> granicus.if.org Git - python/commitdiff
Issue #23681: The -b option now affects comparisons of bytes with int.
authorSerhiy Storchaka <storchaka@gmail.com>
Fri, 20 Mar 2015 14:54:57 +0000 (16:54 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Fri, 20 Mar 2015 14:54:57 +0000 (16:54 +0200)
Doc/using/cmdline.rst
Doc/whatsnew/3.5.rst
Lib/test/test_bytes.py
Misc/NEWS
Objects/bytesobject.c

index 4017ce897c6bb52c341bc520296fb2c844c67b95..f52afa0229fa185801489ade531880b089e5de1e 100644 (file)
@@ -190,9 +190,12 @@ Miscellaneous options
 
 .. cmdoption:: -b
 
-   Issue a warning when comparing str and bytes. Issue an error when the
+   Issue a warning when comparing :class:`bytes` or :class:`bytearray` with
+   :class:`str` or :class:`bytes` with :class:`int`.  Issue an error when the
    option is given twice (:option:`-bb`).
 
+   .. versionchanged: 3.5
+      Affects comparisons of :class:`bytes` with :class:`int`.
 
 .. cmdoption:: -B
 
index a70f0b893761afeb9dbc22e987a8673bece8b27e..b22c65769573a1a0f9d2d31772bf1886be196814 100644 (file)
@@ -162,6 +162,8 @@ Some smaller changes made to the core Python language are:
   error handlers now works with decoding and translating.
   (Contributed by Serhiy Storchaka in :issue:`19676` and :issue:`22286`.)
 
+* The :option:`-b` option now affects comparisons of :class:`bytes` with
+  :class:`int`.  (Contributed by Serhiy Storchaka in :issue:`23681`)
 
 
 New Modules
index a9f64a0dccbcb44da70941f511955f233a66e4da..ad283002da88bef492266fdacb8901aa237bab3d 100644 (file)
@@ -1338,20 +1338,35 @@ class AssortedBytesTest(unittest.TestCase):
         b = bytearray()
         self.assertFalse(b.replace(b'', b'') is b)
 
+    @unittest.skipUnless(sys.flags.bytes_warning,
+                         "BytesWarning is needed for this test: use -bb option")
     def test_compare(self):
-        if sys.flags.bytes_warning:
-            def bytes_warning():
-                return test.support.check_warnings(('', BytesWarning))
-            with bytes_warning():
-                b'' == ''
-            with bytes_warning():
-                b'' != ''
-            with bytes_warning():
-                bytearray(b'') == ''
-            with bytes_warning():
-                bytearray(b'') != ''
-        else:
-            self.skipTest("BytesWarning is needed for this test: use -bb option")
+        def bytes_warning():
+            return test.support.check_warnings(('', BytesWarning))
+        with bytes_warning():
+            b'' == ''
+        with bytes_warning():
+            '' == b''
+        with bytes_warning():
+            b'' != ''
+        with bytes_warning():
+            '' != b''
+        with bytes_warning():
+            bytearray(b'') == ''
+        with bytes_warning():
+            '' == bytearray(b'')
+        with bytes_warning():
+            bytearray(b'') != ''
+        with bytes_warning():
+            '' != bytearray(b'')
+        with bytes_warning():
+            b'\0' == 0
+        with bytes_warning():
+            0 == b'\0'
+        with bytes_warning():
+            b'\0' != 0
+        with bytes_warning():
+            0 != b'\0'
 
     # Optimizations:
     # __iter__? (optimization)
index 455c0c1c27a385182d7f603250aef0c5e3d273c6..0f098c5e6d37c6954cf2e1b6daa9d2e7813a9b80 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@ Release date: 2015-03-28
 Core and Builtins
 -----------------
 
+- Issue #23681: The -b option now affects comparisons of bytes with int.
+
 - Issue #23632: Memoryviews now allow tuple indexing (including for
   multi-dimensional memoryviews).
 
index e0ac1ab7644fb4f2cc166213d5673b42bf07159c..5a9dfbd5dbb0f59082df81753d6a99192e0610f9 100644 (file)
@@ -1385,14 +1385,23 @@ bytes_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 || op == Py_NE) &&
-            (PyObject_IsInstance((PyObject*)a,
-                                 (PyObject*)&PyUnicode_Type) ||
-            PyObject_IsInstance((PyObject*)b,
-                                 (PyObject*)&PyUnicode_Type))) {
-            if (PyErr_WarnEx(PyExc_BytesWarning,
-                        "Comparison between bytes and string", 1))
-                return NULL;
+        if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) {
+            if (PyObject_IsInstance((PyObject*)a,
+                                    (PyObject*)&PyUnicode_Type) ||
+                PyObject_IsInstance((PyObject*)b,
+                                    (PyObject*)&PyUnicode_Type)) {
+                if (PyErr_WarnEx(PyExc_BytesWarning,
+                            "Comparison between bytes and string", 1))
+                    return NULL;
+            }
+            else if (PyObject_IsInstance((PyObject*)a,
+                                    (PyObject*)&PyLong_Type) ||
+                PyObject_IsInstance((PyObject*)b,
+                                    (PyObject*)&PyLong_Type)) {
+                if (PyErr_WarnEx(PyExc_BytesWarning,
+                            "Comparison between bytes and int", 1))
+                    return NULL;
+            }
         }
         result = Py_NotImplemented;
     }