0

As you can read in "man python" :

-3 Warn about Python 3.x incompatibilities that 2to3 cannot trivially fix.

This command allready help me in the past to avoid incompatibility between code for Python 2.x & 3.x.

However, i don't have any warning for this code. Should I trust "python -3" as returning an exhaustive result ?

NB : Surprinsigly, there is no mention of this option here.

Community
  • 1
  • 1
  • 1
    Because that code isn't incompatible with 3.x. Unless you mean using `__nonzero__`? In which case, it's probably because it's looking for "incompatibilities that 2to3 cannot **trivially** fix" and that's a pretty trivial fix. It's unclear what code you're talking about. – Morgan Thrapp Jul 08 '16 at 15:56
  • Your link goes to the Python 3 documentation. Python 3 doesn't have that flag. (Surprisingly, the [Python 2 documentation](https://docs.python.org/2/using/cmdline.html#cmdoption-3) doesn't mention the 2to3 caveat.) – user2357112 Jul 08 '16 at 16:07
  • @user2357112 -- Yeah, I'm surprised by that too (which is why my initial answer had code that 2to3 handles trivially). – mgilson Jul 08 '16 at 16:17
  • 1
    I've added a [bug](http://bugs.python.org/issue27470) against the documentation. Who knows how long it will be before they fix it (or if they do)... But at least it's there... – mgilson Jul 08 '16 at 16:24

1 Answers1

2

No, you should not treat that warning as exhaustive. The following code behaves differently on python2.x and python3.x but emits no warnings:

from __future__ import print_function

class test(object):
    def __nonzero__(self):
        return False

t = test()
if t:
    print('Hello')

(at least not as of python2.7.10 on OSX). Ok, perhaps this code can be handled by 2to3 (it is) -- though I would still expect a warning based on the official documentation.

Warn about Python 3.x possible incompatibilities by emitting a DeprecationWarning for features that are removed or significantly changed in Python 3.

Here's an example of some code that isn't handled by 2to3 but still no warning is emitted using the -3 option:

from __future__ import print_function

import subprocess

class Pipe(object):
    def __init__(self,openstr):
        self.gnu_process=subprocess.Popen(openstr.split(),
                                          stdin=subprocess.PIPE)

    def put_in_pipe(self,mystr):
        print(mystr, file=self.gnu_process.stdin)

if __name__=="__main__":
    print("This simple program just echoes what you say (control-d to exit)")
    p = Pipe("cat -")
    while True:
        try:
            inpt = raw_input()
        except EOFError:
            break
        print('putting in pipe:%s' % inpt)
        p.put_in_pipe(inpt)

The problem with this code is that in python3, my pipe expects a bytes object but it's getting a str (python2.x's unicode).

Community
  • 1
  • 1
mgilson
  • 300,191
  • 65
  • 633
  • 696