diff options
author | Guido van Rossum <guido@python.org> | 2007-04-09 00:49:13 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2007-04-09 00:49:13 (GMT) |
commit | ebea9beab34d9c7552764c071b0d7fdb4744092b (patch) | |
tree | 2a5027f9ab3b229a97a65f57c162a6ba9e5c03ea | |
parent | 343e97ff7f939fc16d144a196a95ecf2e82c061e (diff) | |
download | cpython-ebea9beab34d9c7552764c071b0d7fdb4744092b.zip cpython-ebea9beab34d9c7552764c071b0d7fdb4744092b.tar.gz cpython-ebea9beab34d9c7552764c071b0d7fdb4744092b.tar.bz2 |
Bytes should never equal unicode.
Add tests for str <cmpop> bytes.
-rw-r--r-- | Lib/test/test_bytes.py | 21 | ||||
-rw-r--r-- | Objects/bytesobject.c | 7 |
2 files changed, 27 insertions, 1 deletions
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 2999aa6..e90bcee 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -114,6 +114,27 @@ class BytesTest(unittest.TestCase): self.assertEqual(b"abc" < "ab", False) self.assertEqual(b"abc" <= "ab", False) + self.assertEqual("abc" == b"abc", True) + self.assertEqual("ab" != b"abc", True) + self.assertEqual("ab" <= b"abc", True) + self.assertEqual("ab" < b"abc", True) + self.assertEqual("abc" >= b"ab", True) + self.assertEqual("abc" > b"ab", True) + + self.assertEqual("abc" != b"abc", False) + self.assertEqual("ab" == b"abc", False) + self.assertEqual("ab" > b"abc", False) + self.assertEqual("ab" >= b"abc", False) + self.assertEqual("abc" < b"ab", False) + self.assertEqual("abc" <= b"ab", False) + + # But they should never compare equal to Unicode! + # Test this for all expected byte orders and Unicode character sizes + self.assertEqual(b"\0a\0b\0c" == u"abc", False) + self.assertEqual(b"\0\0\0a\0\0\0b\0\0\0c" == u"abc", False) + self.assertEqual(b"a\0b\0c\0" == u"abc", False) + self.assertEqual(b"a\0\0\0b\0\0\0c\0\0\0" == u"abc", False) + def test_nohash(self): self.assertRaises(TypeError, hash, bytes()) diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index 34f381a..d985fc7 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -848,7 +848,12 @@ bytes_richcompare(PyObject *self, PyObject *other, int op) int cmp; /* For backwards compatibility, bytes can be compared to anything that - supports the (binary) buffer API. */ + supports the (binary) buffer API. Except Unicode. */ + + if (PyUnicode_Check(self) || PyUnicode_Check(other)) { + Py_INCREF(Py_NotImplemented); + return Py_NotImplemented; + } self_buffer = self->ob_type->tp_as_buffer; if (self_buffer == NULL || |