Commit 9e9dcd6d authored by Barry Warsaw's avatar Barry Warsaw

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).
parent e94a37f3
...@@ -9,6 +9,7 @@ import os ...@@ -9,6 +9,7 @@ import os
import re import re
import sys import sys
import copy import copy
import operator
import pickle import pickle
import tempfile import tempfile
import unittest import unittest
...@@ -863,6 +864,17 @@ class AssortedBytesTest(unittest.TestCase): ...@@ -863,6 +864,17 @@ class AssortedBytesTest(unittest.TestCase):
b = bytearray() b = bytearray()
self.failIf(b.replace(b'', b'') is b) 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: # Optimizations:
# __iter__? (optimization) # __iter__? (optimization)
# __reversed__? (optimization) # __reversed__? (optimization)
......
...@@ -939,7 +939,7 @@ bytes_richcompare(PyObject *self, PyObject *other, int op) ...@@ -939,7 +939,7 @@ bytes_richcompare(PyObject *self, PyObject *other, int op)
error, even if the comparison is for equality. */ error, even if the comparison is for equality. */
if (PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type) || if (PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type) ||
PyObject_IsInstance(other, (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, if (PyErr_WarnEx(PyExc_BytesWarning,
"Comparison between bytearray and string", 1)) "Comparison between bytearray and string", 1))
return NULL; return NULL;
......
...@@ -818,7 +818,7 @@ string_richcompare(PyBytesObject *a, PyBytesObject *b, int op) ...@@ -818,7 +818,7 @@ string_richcompare(PyBytesObject *a, PyBytesObject *b, int op)
/* Make sure both arguments are strings. */ /* Make sure both arguments are strings. */
if (!(PyBytes_Check(a) && PyBytes_Check(b))) { 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_IsInstance((PyObject*)a,
(PyObject*)&PyUnicode_Type) || (PyObject*)&PyUnicode_Type) ||
PyObject_IsInstance((PyObject*)b, PyObject_IsInstance((PyObject*)b,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment