diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2011-02-05 11:40:05 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2011-02-05 11:40:05 (GMT) |
commit | 8d8f7c5e01358223996d5127aae571cfddac78a8 (patch) | |
tree | d1245aa3412a3371c897ff1217e188510971ee40 /Doc | |
parent | 7095721d3eb563c7104c4b4a6a53aa62def29d53 (diff) | |
download | cpython-8d8f7c5e01358223996d5127aae571cfddac78a8.zip cpython-8d8f7c5e01358223996d5127aae571cfddac78a8.tar.gz cpython-8d8f7c5e01358223996d5127aae571cfddac78a8.tar.bz2 |
Mention -b and -bb
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/howto/pyporting.rst | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/Doc/howto/pyporting.rst b/Doc/howto/pyporting.rst index 3696f4b..cb86826 100644 --- a/Doc/howto/pyporting.rst +++ b/Doc/howto/pyporting.rst @@ -319,6 +319,37 @@ This means you need to choose what an API is going to accept and create and consistently stick to that API in both Python 2 and 3. +Bytes / unicode comparison +************************** + +In Python 3, mixing bytes and unicode is forbidden in most situations; it +will raise a :class:`TypeError` where Python 2 would have attempted an implicit +coercion between types. However, there is one case where it doesn't and +it can be very misleading:: + + >>> b"" == "" + False + +This is because comparison for equality is required by the language to always +succeed (and return ``False`` for incompatible types). However, this also +means that code incorrectly ported to Python 3 can display buggy behaviour +if such comparisons are silently executed. To detect such situations, +Python 3 has a ``-b`` flag that will display a warning:: + + $ python3 -b + >>> b"" == "" + __main__:1: BytesWarning: Comparison between bytes and string + False + +To turn the warning into an exception, use the ``-bb`` flag instead:: + + $ python3 -bb + >>> b"" == "" + Traceback (most recent call last): + File "<stdin>", line 1, in <module> + BytesWarning: Comparison between bytes and string + + ``__str__()``/``__unicode__()`` ''''''''''''''''''''''''''''''' In Python 2, objects can specify both a string and unicode representation of |