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
-b option undocumented #55890
Comments
Python 2 has a -b command-line option which is undocumented. It is used by the warnings machinery to decide to warn about (-b) or raise an error (-bb) if someone uses str(bytes) or str(buffer) (explanation courtesy of grep and Python/pythonrun.c). |
Are you sure? (see bpo-10471). I don't see how -b could do anything in python2, since bytes and string are the same there (and, indeed, from command line experimentation it doesn't seem to). |
The code definitely is in 2.7. Python/_warnings.c:861: if (Py_BytesWarningFlag > 1) |
rdmurray@hey: |
test_bytes has a branch for -b and passes without it, with -b and with -bb. I really don’t know. |
Even "from __future__ import unicode_literals" doesn't make it do anything. Perhaps Christian merged it by mistake in [5341b30b1812]? |
Nick Coghlan wrote:
It looks more like some parts were left out in the merge |
Hi Marc I tried reproducing this for bytearray using Python 2.7.2 but I can't see a warning. devel@moses: hi hi Please correct me if I'm wrong anywhere here. I'd like to work on this. |
Try this to trigger a warning: python2 -b -c 'bytearray("3") == u"3"' |
Might be worth making this addition from 3 (I'm not sure how to add this to 2) Building on Martin's example: On all of these, python2 is emily-mba:cpython emily$ python2
>>> bytearray("3") == u"3"
False
emily-mba:cpython emily$ python2 -b
>>> bytearray("3") == u"3"
__main__:1: BytesWarning: Comparison between bytearray and string
False
emily-mba:cpython emily$ python2 -bb
>>> bytearray("3") == u"3"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
BytesWarning: Comparison between bytearray and string |
Here's an attempt (based on 3's main.c http://hg.python.org/cpython/file/8866ac6f2269/Modules/main.c) |
Trouble is, in Python 2 bytes() and str() are the same thing, and most of those conditions don’t apply. Maybe something like this is more correct: -b : issue warnings about comparing bytearray with unicode. |
As well as the usage string, it is missing from the list of options in the main documentation at Doc/using/cmdline.rst. There are a couple of places (sys.rst and warnings.rst) that reference :option:`-b`, and newer versions of Sphinx complain about this. |
I have attached a patch for 2.7 that adds -b and -bb to the command line documentation in Modules/main.c and Doc/library/cmdline.rst, following the suggested text provided by Martin. Interestingly, unicode(), bytearray(), and str() don't seem to be transitive in 2.7. >>> u"3" == str(3)
True
>>> str(3) == bytearray("3")
True
>>> u"3" == bytearray("3")
False |
Right, the lack of transitivity comes from the fact that: >>> u"3" == str(3)
True Is really shorthand for: >>> u"3" == str(3).decode("ascii")
True However, the implicit decoding only triggers for *exactly* bytes instances, so in the bytearray case it needs to be explicit: >>> u"3" == bytearray(b"3").decode("ascii")
True |
Added some review comments on the patch. The only critical one was changing a :class:`bytes` reference to :class:`unicode`, but I also suggested it may be worth mentioning that it *doesn't* enable warnings about all binary/text comparisons the way the Python 3 one does. |
Thanks for the :class: markup edits. I'm still learning this stuff. |
Thanks for the patch, Greg! I implemented Nick's suggestions and merged it. |
-b
and-bb
options #1562Note: 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: