Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force Python 3 to raise exception when comparing bytes with string

Python 3 returns "False" when comparing bytes and string objects.

>>> b'' == ''
False

Is there a way to force the interpreter raise an exception instead? Maybe some flag when invoking python3?

like image 806
mechatroner Avatar asked Oct 19 '25 00:10

mechatroner


1 Answers

Passing the -b flag to Python3 will emit a warning when comparing bytes and strings. Passing -bb will raise an exception.

$  python3 -c "b'' == '' "
$  python3 -bc "b'' == '' "
<string>:1: BytesWarning: Comparison between bytes and string
$  python3 -bbc "b'' == '' "
Traceback (most recent call last):
  File "<string>", line 1, in <module>
BytesWarning: Comparison between bytes and string
like image 160
snakecharmerb Avatar answered Oct 20 '25 13:10

snakecharmerb