mirror of
https://github.com/python/cpython.git
synced 2025-08-28 12:45:07 +00:00
Patch #1049 by Thomas Lee.
Changes comparisons between PyBytes and PyUnicode to return unequal instead of raising TypeError.
This commit is contained in:
parent
6ccd3f2dbc
commit
1e35e76532
2 changed files with 9 additions and 7 deletions
|
@ -130,12 +130,14 @@ class BytesTest(unittest.TestCase):
|
||||||
self.assertEqual(str8("abc") < b"ab", False)
|
self.assertEqual(str8("abc") < b"ab", False)
|
||||||
self.assertEqual(str8("abc") <= b"ab", False)
|
self.assertEqual(str8("abc") <= b"ab", False)
|
||||||
|
|
||||||
# Bytes can't be compared to Unicode!
|
# Byte comparisons with unicode should always fail!
|
||||||
# Test this for all expected byte orders and Unicode character sizes
|
# Test this for all expected byte orders and Unicode character sizes
|
||||||
self.assertRaises(TypeError, lambda: b"\0a\0b\0c" == "abc")
|
self.assertEqual(b"\0a\0b\0c" == "abc", False)
|
||||||
self.assertRaises(TypeError, lambda: b"\0\0\0a\0\0\0b\0\0\0c" == "abc")
|
self.assertEqual(b"\0\0\0a\0\0\0b\0\0\0c" == "abc", False)
|
||||||
self.assertRaises(TypeError, lambda: b"a\0b\0c\0" == "abc")
|
self.assertEqual(b"a\0b\0c\0" == "abc", False)
|
||||||
self.assertRaises(TypeError, lambda: b"a\0\0\0b\0\0\0c\0\0\0" == "abc")
|
self.assertEqual(b"a\0\0\0b\0\0\0c\0\0\0" == "abc", False)
|
||||||
|
self.assertEqual(bytes() == str(), False)
|
||||||
|
self.assertEqual(bytes() != str(), True)
|
||||||
|
|
||||||
def test_nohash(self):
|
def test_nohash(self):
|
||||||
self.assertRaises(TypeError, hash, bytes())
|
self.assertRaises(TypeError, hash, bytes())
|
||||||
|
|
|
@ -964,8 +964,8 @@ 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)) {
|
||||||
PyErr_SetString(PyExc_TypeError, "can't compare bytes and str");
|
Py_INCREF(Py_NotImplemented);
|
||||||
return NULL;
|
return Py_NotImplemented;
|
||||||
}
|
}
|
||||||
|
|
||||||
self_size = _getbuffer(self, &self_bytes);
|
self_size = _getbuffer(self, &self_bytes);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue