diff Objects/bytesobject.c @ 43001:eafc8b23589d

Make it an error to compare a bytes object and a Unicode object.
author Jeremy Hylton <jeremy@alum.mit.edu>
date Wed, 29 Aug 2007 18:47:16 +0000
parents 71cd5598b74d
children ac2d5d346252
line wrap: on
line diff
--- a/Objects/bytesobject.c	Wed Aug 29 18:44:54 2007 +0000
+++ b/Objects/bytesobject.c	Wed Aug 29 18:47:16 2007 +0000
@@ -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) {