-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Have -b warn when directly comparing ints and bytes #67869
Comments
To help writing Python 2/3 code the -b flag should switch on a warning when comparing an int to a bytes object in Python 2. This will help when someone writes something like |
6 tests failed: And all of them look as bugs. |
Here is a patch that adds required feature and fixes existing bugs in the stdlib and tests. No one of failed tests was false positive. |
These changes should probably be backported to 3.4. |
LGTM, just one minor comment in review. |
I didn't understand the issue. $ python2
>>> b'A'[0] == 65
False
$ python3
Python 3.4.1 (default, Nov 3 2014, 14:38:10)
>>> b'A'[0] == 65
True Oh ok, now I get it: Python 2 and Python 3 behaves differently when comparing a string of a single byte and an integer. Ok to raise a warning when -b command line option is used. |
What if someone writes line[-1] == 0 and line is a Unicode string? Should we emit a warning? I patched locally PyUnicode_RichCompare() to emit a warning. Hum, there are *many* warnings in argparse, http.client, and many other modules. I don't think that it's a good idea. It looks common to use a string for a sentinel (ex: state = "UNKNOWN") and then store an int (ex: state = 0). So comparison need to check the type (ex: isinstance(state, str) and state == "UNKNOWN") which is verbose and annoying. So no, we should not emit a warning :-) |
I did one final pass on the patch and only had wording comments, so tweak those and it LGTM. Assigned to Serhiy since it's obviously his patch. =) Only other thing is to either open up a separate bug or at least apply the fixes to the stdlib in Python 3.4 as well as 3.5. |
New changeset 104c55bc2276 by Serhiy Storchaka in branch '3.4': New changeset 817f1f47824c by Serhiy Storchaka in branch 'default': New changeset 68e9cf0ae93d by Serhiy Storchaka in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: