![](https://secure.gravatar.com/avatar/83c1cb58fae2faedb80305a0798383d1.jpg?s=120&d=mm&r=g)
Hi folks. I've been porting things from Python2 to Python3 for a while, but I just found out today about python3's -b option, which emits a warning when comparing str to bytes (or vice versa), or when converting bytes to str using the str initializer. I noticed that it also seems to even help with dictionary keys - nice. My question is: if -b is no longer detecting things to warn about, how much does that say about how far along your str-vs-bytes-correctness is? Thanks.
![](https://secure.gravatar.com/avatar/396e3de53320abf9855d912cd3d9431f.jpg?s=120&d=mm&r=g)
I don't understand the question Sent from my phone with my typo-happy thumbs. Please excuse my brevity On Tue, Jul 19, 2022, 18:17 Dan Stromberg <drsalists@gmail.com> wrote:
Hi folks.
I've been porting things from Python2 to Python3 for a while, but I just found out today about python3's -b option, which emits a warning when comparing str to bytes (or vice versa), or when converting bytes to str using the str initializer. I noticed that it also seems to even help with dictionary keys - nice.
My question is: if -b is no longer detecting things to warn about, how much does that say about how far along your str-vs-bytes-correctness is?
Thanks.
_______________________________________________ code-quality mailing list -- code-quality@python.org To unsubscribe send an email to code-quality-leave@python.org https://mail.python.org/mailman3/lists/code-quality.python.org/ Member address: graffatcolmingov@gmail.com
![](https://secure.gravatar.com/avatar/32b10232489646c1940c202e91f475b9.jpg?s=120&d=mm&r=g)
When porting code from python2 -> python3, one of the biggest hurdles is getting all the things that used to be just str, converted to sometimes-str-sometimes-bytes. python3 -b warns about comparisons and likely-unsafe conversions. If -b is no longer alerting about these, what does that really mean? Is it just instrumenting the str and bytes __eq__ methods and str initializer? Or is it somehow more thorough than that? On Tue, Jul 19, 2022 at 4:17 PM Dan Stromberg <drsalists@gmail.com> wrote:
Hi folks.
I've been porting things from Python2 to Python3 for a while, but I just found out today about python3's -b option, which emits a warning when comparing str to bytes (or vice versa), or when converting bytes to str using the str initializer. I noticed that it also seems to even help with dictionary keys - nice.
My question is: if -b is no longer detecting things to warn about, how much does that say about how far along your str-vs-bytes-correctness is?
Thanks.
_______________________________________________ code-quality mailing list -- code-quality@python.org To unsubscribe send an email to code-quality-leave@python.org https://mail.python.org/mailman3/lists/code-quality.python.org/ Member address: strombrg@gmail.com
![](https://secure.gravatar.com/avatar/de311342220232e618cb27c9936ab9bf.jpg?s=120&d=mm&r=g)
On 7/19/22 16:43, Dan Stromberg wrote:
When porting code from python2 -> python3, one of the biggest hurdles is getting all the things that used to be just str, converted to sometimes-str-sometimes-bytes.
python3 -b warns about comparisons and likely-unsafe conversions.
If -b is no longer alerting about these, what does that really mean?
Another way to phrase the question: What kind of str/bytes problems does -b not warn about? -- ~Ethan~
![](https://secure.gravatar.com/avatar/5615a372d9866f203a22b2c437527bbb.jpg?s=120&d=mm&r=g)
I think the documentation is fairly clear. The -b option only checks *comparisons*, not any other operations. And only equality and inequality comparisons at that. https://docs.python.org/3/using/cmdline.html#cmdoption-b Although the documentation doesn't mentioned it, -b also checks for calling str() on a bytes object. Other operations always fail: * Order comparisons (less than, greater than) between string and bytes are always an error in Python3 * Likewise for concatenation b'' + '' * And other string methods, e.g. 'hello'.index(b'e') raises. Most operations on mixed str/bytes already fail. The only troublesome cases are the calls to str() and == and != comparisons, and they are checked by -b. What else is there to check? Not checked: you can mix bytes and strings in the `and` and `or` operators, same as you can mix any combination of types: 'hello' or b'goodbye' or [2.5, 3.6] # Legal. Also not checked: identity comparison with `is`. A bytes object and a string object will never be identical. Neither of those should be checked. As for what that means with regard to your application, that depends on how extensive your testing is. -b only checks comparisons which are actually reached at runtime, not comparisons that aren't reached: [steve ~]$ python3 -b -c "print(('a' == 'a') if True else ('a' == b'a'))" True -- Steve
![](https://secure.gravatar.com/avatar/569649aec0e36546b1eebd1ea6a84d48.jpg?s=120&d=mm&r=g)
I think the documentation is fairly clear. The -b option only checks *comparisons*, not any other operations. And only equality and inequality comparisons at that. https://docs.python.org/3/using/cmdline.html#cmdoption-b Although the documentation doesn't mentioned it, -b also checks for calling str() on a bytes object. Other operations always fail: * Order comparisons (less than, greater than) between string and bytes are always an error in Python3 * Likewise for concatenation b'' + '' * And other string methods, e.g. 'hello'.index(b'e') raises. Most operations on mixed str/bytes already fail. The only troublesome cases are the calls to str() and == and != comparisons, and they are checked by -b. What else is there to check? Not checked: you can mix bytes and strings in the `and` and `or` operators, same as you can mix any combination of types: 'hello' or b'goodbye' or [2.5, 3.6] # Legal. Also not checked: identity comparison with `is`. A bytes object and a string object will never be identical. Neither of those should be checked. As for what that means with regard to your application, that depends on how extensive your testing is. -b only checks comparisons which are actually reached at runtime, not comparisons that aren't reached: [steve ~]$ python3 -b -c "print(('a' == 'a') if True else ('a' == b'a'))" True -- Steve
participants (6)
-
Dan Stromberg
-
Dan Stromberg
-
Ethan Furman
-
Ian Stapleton Cordasco
-
Steven D'Aprano
-
Steven D'Aprano