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

Guido van Rossum guido at python.org
Wed Aug 29 19:53:28 CEST 2007


Thanks! I simply forgot about this. Can yuo check in the change to
bytesobject.c? We'll deal with the fallout shortly.

On 8/29/07, Jeremy Hylton <jeremy at alum.mit.edu> wrote:
> 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
> _______________________________________________
> Python-3000 mailing list
> Python-3000 at python.org
> http://mail.python.org/mailman/listinfo/python-3000
> Unsubscribe: http://mail.python.org/mailman/options/python-3000/guido%40python.org
>


-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-3000 mailing list