This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Byte warning mode and b'' != ''
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.0
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: barry Nosy List: barry, benjamin.peterson, brett.cannon, christian.heimes, pitrou, vstinner
Priority: release blocker Keywords: patch

Created on 2008-09-28 11:32 by christian.heimes, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
bytes_ne_warning-3.patch vstinner, 2008-10-14 23:17 Raise a warning on b'' != '' and bytearray(b'') != ''
Messages (12)
msg73969 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-09-28 11:32
In byte warning mode (-b or -bb command line argument) b'' == '' raises
an exception but b'' != '' doesn't.

./python -bb
>>> b'' == ''
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
BytesWarning: Comparison between bytes and string
>>> b'' != ''
True

I can't recall why I implemented the byte warning mode this way. But
nowadays I think it could hide bugs in code like "while egg != ''" where
egg is a byte instance.
msg73973 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-09-28 14:18
+1

I was just confused by this fact yesterday. :)
msg73979 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2008-09-28 17:49
+1 as well. Lib/pty.py had a line like that ("while buf != ''") and I
wondered why no exception was thrown with -bb while the variable was a
bytes object.
msg73984 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2008-09-28 19:26
Here is a patch for this issue.
msg73985 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-09-28 19:32
Thanks for the patch but you've missed a spot in bytearrayobject.c:

Objects/bytearrayobject.c:        if (Py_BytesWarningFlag && op == Py_EQ) {
Objects/bytesobject.c:          if (Py_BytesWarningFlag && (op == Py_EQ) &&
msg73986 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-09-28 19:37
It would also be nice to have tests. (in test_bytes)
msg73989 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2008-09-28 21:09
@christian.heimes: Oops, i totally forget the bytearray() type. Here 
is a new patch.
msg73990 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2008-09-28 21:10
I don't know how to activate BytesWarning as error (as "python3 -bb" 
does). Here is an patch for tests only working with "python3 -bb".
msg73991 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-09-28 21:12
warnings.simplefilter("always", BytesWarning) should do the trick.
msg74063 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2008-09-30 02:19
Setting as a deferred blocker since this is a 3.0 thing and not a 2.6 thing.
msg74782 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2008-10-14 23:17
New patch including the test. The test doesn't fail anymore if -bb is 
not set (test is just ignored). See also issue #4125 (force -bb option 
for runtests.sh).
msg74891 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2008-10-17 01:51
r66950
History
Date User Action Args
2022-04-11 14:56:39adminsetgithub: 48238
2008-10-17 01:51:12barrysetstatus: open -> closed
messages: + msg74891
2008-10-14 23:17:32vstinnersetfiles: + bytes_ne_warning-3.patch
messages: + msg74782
2008-10-14 23:16:21vstinnersetfiles: - test_bytes.patch
2008-10-14 23:16:17vstinnersetfiles: - bytes_ne_warning-2.patch
2008-10-02 12:56:01barrysetpriority: deferred blocker -> release blocker
2008-09-30 02:19:01brett.cannonsetpriority: release blocker -> deferred blocker
nosy: + brett.cannon
messages: + msg74063
2008-09-28 21:12:53benjamin.petersonsetmessages: + msg73991
2008-09-28 21:10:44vstinnersetfiles: + test_bytes.patch
messages: + msg73990
2008-09-28 21:09:21vstinnersetfiles: - bytes_ne_warning.patch
2008-09-28 21:09:16vstinnersetfiles: + bytes_ne_warning-2.patch
messages: + msg73989
2008-09-28 19:37:10benjamin.petersonsetmessages: + msg73986
2008-09-28 19:32:10christian.heimessetmessages: + msg73985
2008-09-28 19:26:26vstinnersetfiles: + bytes_ne_warning.patch
keywords: + patch
messages: + msg73984
nosy: + vstinner
2008-09-28 17:49:19pitrousetnosy: + pitrou
messages: + msg73979
2008-09-28 14:18:20benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg73973
2008-09-28 11:32:56christian.heimescreate