Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8d8f7c5

Browse files
committed
Mention -b and -bb
1 parent 7095721 commit 8d8f7c5

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

Doc/howto/pyporting.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,37 @@ This means you need to choose what an API is going to accept and create and
319319
consistently stick to that API in both Python 2 and 3.
320320

321321

322+
Bytes / unicode comparison
323+
**************************
324+
325+
In Python 3, mixing bytes and unicode is forbidden in most situations; it
326+
will raise a :class:`TypeError` where Python 2 would have attempted an implicit
327+
coercion between types. However, there is one case where it doesn't and
328+
it can be very misleading::
329+
330+
>>> b"" == ""
331+
False
332+
333+
This is because comparison for equality is required by the language to always
334+
succeed (and return ``False`` for incompatible types). However, this also
335+
means that code incorrectly ported to Python 3 can display buggy behaviour
336+
if such comparisons are silently executed. To detect such situations,
337+
Python 3 has a ``-b`` flag that will display a warning::
338+
339+
$ python3 -b
340+
>>> b"" == ""
341+
__main__:1: BytesWarning: Comparison between bytes and string
342+
False
343+
344+
To turn the warning into an exception, use the ``-bb`` flag instead::
345+
346+
$ python3 -bb
347+
>>> b"" == ""
348+
Traceback (most recent call last):
349+
File "<stdin>", line 1, in <module>
350+
BytesWarning: Comparison between bytes and string
351+
352+
322353
``__str__()``/``__unicode__()``
323354
'''''''''''''''''''''''''''''''
324355
In Python 2, objects can specify both a string and unicode representation of

0 commit comments

Comments
 (0)