Skip to content

Commit

Permalink
warn string cmp with new assumptions
Browse files Browse the repository at this point in the history
  • Loading branch information
nanjekyejoannah committed Dec 12, 2023
1 parent 2f90d9c commit 0bee79a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Lib/test/test_StringIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ def test_unicode(self):
self.assertEqual(s, unicode('abcuvwxyz!'))
self.assertEqual(type(s), types.UnicodeType)

def test_py3k_string_cmp(self):
with test_support.check_py3k_warnings(("comparing unicode and byte strings has different semantics in 3.x: convert the second string to byte.",
DeprecationWarning)):
"test str" == u"test unicode"
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"


class TestcStringIO(TestGenericStringIO):
MODULE = cStringIO

Expand Down
5 changes: 4 additions & 1 deletion Objects/stringobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1209,8 +1209,11 @@ string_richcompare(PyStringObject *a, PyStringObject *b, int op)
PyObject *result;

/* Make sure both arguments are strings. */
if (!(PyString_Check(a) && PyString_Check(b))) {
if (!(PyString_Check(a) && PyString_Check(b)) ) {
result = Py_NotImplemented;
if (PyErr_WarnPy3k("comparing unicode and byte strings has different semantics in 3.x: convert the second string to byte.", 1) < 0) {
goto out;
}
goto out;
}
if (a == b) {
Expand Down
4 changes: 4 additions & 0 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -6236,6 +6236,10 @@ int PyUnicode_Compare(PyObject *left,
PyUnicodeObject *u = NULL, *v = NULL;
int result;

if (!PyUnicode_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;
}

/* Coerce the two arguments */
u = (PyUnicodeObject *)PyUnicode_FromObject(left);
if (u == NULL)
Expand Down

0 comments on commit 0bee79a

Please sign in to comment.