[Python-3000] proposal: comparing bytes and str raises TypeError

Jeremy Hylton jeremy at alum.mit.edu
Wed Aug 29 19:31:51 CEST 2007


As I was cleaning up the http libraries, I noticed a lot of code that
has comparisons with string literals.  As we change code to return
bytes instead of strings, these comparisons start to fail silently.
When you're lucky, you have a test that catches the failure.  In the
httplib case, there were a couple places where the code got stuck in a
loop, because it was waiting for a socket to return "" before exiting.
 There are lots of places where we are not so lucky.

I made a local change to my bytesobject.c to raise an exception
whenever it is compared to a PyUnicode_Object.  This has caught a
number of real bugs that weren't caught by the test suite.  I think we
should make this the expected behavior for comparisons of bytes and
strings, because users are going to have the same problem and it's
hard to track down without changing the interpreter.

The obvious downside is that you can't have a heterogeneous containers
that mix strings and bytes:
>>> L = ["1", b"1"]
>>> "1" in L
True
>>> "2" in L
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't compare str and bytes

But I'm not sure that we actually need to support this case.

Jeremy


More information about the Python-3000 mailing list