diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-03-20 14:54:57 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-03-20 14:54:57 (GMT) |
commit | 1dd49824df49d0132b21d90c12bb596da89d7a17 (patch) | |
tree | 7c76fae39c0d2160d8abcb5e908d7b46a9cd8f01 | |
parent | ee4c0b9dcfb550094cca086a032d44393b5c3642 (diff) | |
download | cpython-1dd49824df49d0132b21d90c12bb596da89d7a17.zip cpython-1dd49824df49d0132b21d90c12bb596da89d7a17.tar.gz cpython-1dd49824df49d0132b21d90c12bb596da89d7a17.tar.bz2 |
Issue #23681: The -b option now affects comparisons of bytes with int.
-rw-r--r-- | Doc/using/cmdline.rst | 5 | ||||
-rw-r--r-- | Doc/whatsnew/3.5.rst | 2 | ||||
-rw-r--r-- | Lib/test/test_bytes.py | 41 | ||||
-rw-r--r-- | Misc/NEWS | 2 | ||||
-rw-r--r-- | Objects/bytesobject.c | 25 |
5 files changed, 53 insertions, 22 deletions
diff --git a/Doc/using/cmdline.rst b/Doc/using/cmdline.rst index 4017ce8..f52afa0 100644 --- a/Doc/using/cmdline.rst +++ b/Doc/using/cmdline.rst @@ -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 diff --git a/Doc/whatsnew/3.5.rst b/Doc/whatsnew/3.5.rst index a70f0b8..b22c657 100644 --- a/Doc/whatsnew/3.5.rst +++ b/Doc/whatsnew/3.5.rst @@ -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 diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index a9f64a0..ad28300 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -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) @@ -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). diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index e0ac1ab..5a9dfbd 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -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; } |