diff --git a/Lib/test/test_StringIO.py b/Lib/test/test_StringIO.py index 42f307ad578613..e855462dda8849 100644 --- a/Lib/test/test_StringIO.py +++ b/Lib/test/test_StringIO.py @@ -166,6 +166,23 @@ def test_unicode(self): self.assertEqual(s, unicode('abcuvwxyz!')) self.assertEqual(type(s), types.UnicodeType) + def test_py3k_string_cmp(self): + "abc" == 123 + "abc" == object() + u"abc" == 123 + u"abc" == object() + with test_support.check_py3k_warnings(("comparing unicode and byte strings has different semantics in 3.x: convert the first string to bytes.", + DeprecationWarning)): + u"test str" == "test unicode" + deprecations = [] + if sys.py3kwarning: + deprecations += [ + ("comparing unicode and byte strings has different semantics in 3.x: convert the second string to byte.", DeprecationWarning), + ("comparing unicode and byte strings has different semantics in 3.x: convert the first string to bytes.", DeprecationWarning) + ] + with test_support.check_py3k_warnings(*deprecations): + "test str" == u"test unicode" + class TestcStringIO(TestGenericStringIO): MODULE = cStringIO diff --git a/Objects/stringobject.c b/Objects/stringobject.c index c47d32f4060f40..fd477627f6e62e 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -1209,6 +1209,9 @@ string_richcompare(PyStringObject *a, PyStringObject *b, int op) PyObject *result; /* Make sure both arguments are strings. */ + if ((PyString_Check(a) && PyUnicode_Check(b)) && PyErr_WarnPy3k("comparing unicode and byte strings has different semantics in 3.x: convert the second string to byte.", 1) < 0) { + goto out; + } if (!(PyString_Check(a) && PyString_Check(b))) { result = Py_NotImplemented; goto out; diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index a859fa05214d8c..c00f7f365555d3 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -6269,6 +6269,10 @@ PyObject *PyUnicode_RichCompare(PyObject *left, { int result; + if (PyString_Check(right) && PyErr_WarnPy3k("comparing unicode and byte strings has different semantics in 3.x: convert the first string to bytes.", 1) < 0){ + goto onError; + } + result = PyUnicode_Compare(left, right); if (result == -1 && PyErr_Occurred()) goto onError;