changeset: 95101:68e9cf0ae93d user: Serhiy Storchaka date: Fri Mar 20 16:54:57 2015 +0200 files: Doc/using/cmdline.rst Doc/whatsnew/3.5.rst Lib/test/test_bytes.py Misc/NEWS Objects/bytesobject.c description: Issue #23681: The -b option now affects comparisons of bytes with int. diff -r 817f1f47824c -r 68e9cf0ae93d Doc/using/cmdline.rst --- a/Doc/using/cmdline.rst Fri Mar 20 16:48:02 2015 +0200 +++ b/Doc/using/cmdline.rst Fri Mar 20 16:54:57 2015 +0200 @@ -190,9 +190,12 @@ .. 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 -r 817f1f47824c -r 68e9cf0ae93d Doc/whatsnew/3.5.rst --- a/Doc/whatsnew/3.5.rst Fri Mar 20 16:48:02 2015 +0200 +++ b/Doc/whatsnew/3.5.rst Fri Mar 20 16:54:57 2015 +0200 @@ -162,6 +162,8 @@ 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 -r 817f1f47824c -r 68e9cf0ae93d Lib/test/test_bytes.py --- a/Lib/test/test_bytes.py Fri Mar 20 16:48:02 2015 +0200 +++ b/Lib/test/test_bytes.py Fri Mar 20 16:54:57 2015 +0200 @@ -1338,20 +1338,35 @@ 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) diff -r 817f1f47824c -r 68e9cf0ae93d Misc/NEWS --- a/Misc/NEWS Fri Mar 20 16:48:02 2015 +0200 +++ b/Misc/NEWS Fri Mar 20 16:54:57 2015 +0200 @@ -10,6 +10,8 @@ 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 -r 817f1f47824c -r 68e9cf0ae93d Objects/bytesobject.c --- a/Objects/bytesobject.c Fri Mar 20 16:48:02 2015 +0200 +++ b/Objects/bytesobject.c Fri Mar 20 16:54:57 2015 +0200 @@ -1385,14 +1385,23 @@ /* 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; }