[Python-3000-checkins] r57673 - in python/branches/py3k: Lib/test/test_bytes.py Objects/bytesobject.c
jeremy.hylton
python-3000-checkins at python.org
Wed Aug 29 20:47:16 CEST 2007
Author: jeremy.hylton
Date: Wed Aug 29 20:47:16 2007
New Revision: 57673
Modified:
python/branches/py3k/Lib/test/test_bytes.py
python/branches/py3k/Objects/bytesobject.c
Log:
Make it an error to compare a bytes object and a Unicode object.
Modified: python/branches/py3k/Lib/test/test_bytes.py
==============================================================================
--- python/branches/py3k/Lib/test/test_bytes.py (original)
+++ python/branches/py3k/Lib/test/test_bytes.py Wed Aug 29 20:47:16 2007
@@ -130,12 +130,12 @@
self.assertEqual(str8("abc") < b"ab", False)
self.assertEqual(str8("abc") <= b"ab", False)
- # Bytes should never compare equal to Unicode!
+ # Bytes can't be compared to Unicode!
# Test this for all expected byte orders and Unicode character sizes
- self.assertEqual(b"\0a\0b\0c" == "abc", False)
- self.assertEqual(b"\0\0\0a\0\0\0b\0\0\0c" == "abc", False)
- self.assertEqual(b"a\0b\0c\0" == "abc", False)
- self.assertEqual(b"a\0\0\0b\0\0\0c\0\0\0" == "abc", False)
+ self.assertRaises(TypeError, lambda: b"\0a\0b\0c" == "abc")
+ self.assertRaises(TypeError, lambda: b"\0\0\0a\0\0\0b\0\0\0c" == "abc")
+ self.assertRaises(TypeError, lambda: b"a\0b\0c\0" == "abc")
+ self.assertRaises(TypeError, lambda: b"a\0\0\0b\0\0\0c\0\0\0" == "abc")
def test_nohash(self):
self.assertRaises(TypeError, hash, bytes())
Modified: python/branches/py3k/Objects/bytesobject.c
==============================================================================
--- python/branches/py3k/Objects/bytesobject.c (original)
+++ python/branches/py3k/Objects/bytesobject.c Wed Aug 29 20:47:16 2007
@@ -959,8 +959,14 @@
Py_ssize_t minsize;
int cmp;
- /* Bytes can be compared to anything that supports the (binary) buffer
- API. Except Unicode. */
+ /* Bytes can be compared to anything that supports the (binary)
+ buffer API. Except that a comparison with Unicode is always an
+ error, even if the comparison is for equality. */
+ if (PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type) ||
+ PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type)) {
+ PyErr_SetString(PyExc_TypeError, "can't compare bytes and str");
+ return NULL;
+ }
self_size = _getbuffer(self, &self_bytes);
if (self_size < 0) {
More information about the Python-3000-checkins
mailing list