[Python-ideas] Warn about comparing bytes to int for `python3 -b`

Victor Stinner victor.stinner at gmail.com
Mon Mar 16 16:21:57 CET 2015


Hi,

2015-03-16 16:11 GMT+01:00 Brett Cannon <bcannon at gmail.com>:
> One of the rather subtle issues with writing Python 2/3 code is that
> indexing on bytes in Python 2 returns a length-1 bytes object while in
> Python 3 it returns an int. Because ==/!= always returns True/False it can
> be a very subtle failure and tough to track down.

I worked on such patch in the past, but I lost it :-) I can help to
rewrite it if needed.

So yes, it *is* very useful to port a large Python 2 project to Python 3.

For example, you may not be able to run your application with Python 3
because a third party library cannot be imported on Python 3, so it
blocks the whole work on porting an application to Python 3. Until the
module is ported, you may want to prepare the port. Checking
bytes==str just by reading the source code is difficult.

Other issues which can only be "seen" at runtime when running an
application on Python 3 :

- "x > 0" with x=None => TypeError is raised in Python 3
- x / 8 where x is an int => becomes a float in Python 3, it's hard to
detect this issue in Python 2 just by reading the source code :-/

> What do people think of extending -b/-bb in Python 3 to warn when performing
> equality between an int and a bytes object of any length? I don't want to
> restrict to length-1 bytes objects because people may be doing comparisons
> where the result can be length-1 or any other length and thus would still
> have a subtle bug to pick up. Do people think this would raise a ton of
> false-positives? Would people find it useful?

First ensure that the stdlib doesn't raise any BytesWarning exception.

For example, os.get_exec_path() has to modify warnings filters
temporary in Python 3 :-/

    # {b'PATH': ...}.get('PATH') and {'PATH': ...}.get(b'PATH') emit a
    # BytesWarning when using python -b or python -bb: ignore the warning

Victor


More information about the Python-ideas mailing list