Skip to content

Commit bb5d72c

Browse files
authored
Merge pull request #34 from nanjekyejoannah/warn_string_cmp_with_recent_assumptions
Warn for string cmp with new string warning assumptions
2 parents 35e650e + 29ae1a9 commit bb5d72c

3 files changed

Lines changed: 24 additions & 0 deletions

File tree

Lib/test/test_StringIO.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,23 @@ def test_unicode(self):
166166
self.assertEqual(s, unicode('abcuvwxyz!'))
167167
self.assertEqual(type(s), types.UnicodeType)
168168

169+
def test_py3k_string_cmp(self):
170+
"abc" == 123
171+
"abc" == object()
172+
u"abc" == 123
173+
u"abc" == object()
174+
with test_support.check_py3k_warnings(("comparing unicode and byte strings has different semantics in 3.x: convert the first string to bytes.",
175+
DeprecationWarning)):
176+
u"test str" == "test unicode"
177+
deprecations = []
178+
if sys.py3kwarning:
179+
deprecations += [
180+
("comparing unicode and byte strings has different semantics in 3.x: convert the second string to byte.", DeprecationWarning),
181+
("comparing unicode and byte strings has different semantics in 3.x: convert the first string to bytes.", DeprecationWarning)
182+
]
183+
with test_support.check_py3k_warnings(*deprecations):
184+
"test str" == u"test unicode"
185+
169186
class TestcStringIO(TestGenericStringIO):
170187
MODULE = cStringIO
171188

Objects/stringobject.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,9 @@ string_richcompare(PyStringObject *a, PyStringObject *b, int op)
12091209
PyObject *result;
12101210

12111211
/* Make sure both arguments are strings. */
1212+
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) {
1213+
goto out;
1214+
}
12121215
if (!(PyString_Check(a) && PyString_Check(b))) {
12131216
result = Py_NotImplemented;
12141217
goto out;

Objects/unicodeobject.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6269,6 +6269,10 @@ PyObject *PyUnicode_RichCompare(PyObject *left,
62696269
{
62706270
int result;
62716271

6272+
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){
6273+
goto onError;
6274+
}
6275+
62726276
result = PyUnicode_Compare(left, right);
62736277
if (result == -1 && PyErr_Occurred())
62746278
goto onError;

0 commit comments

Comments
 (0)