Martin v. Löwis <mar...@v.loewis.de> added the comment: >> I don't think this can work. You may have to write code like >> >> if sys.version_info >= (3,): >> try: >> some_code() >> except Exception, e: >> pass >> >> (i.e. not use the "as" syntax), because it otherwise won't parse on >> Python 2. Hence, one has to rely on 2to3 fixing it, even though >> it will never be run on Python 2. > > I assume it should be "if sys.version_info < (3,):" since that looks > like Python 2 code, and that'll work with the above revised suggestion.
No, I meant this as stated. Suppose you would write (as you apparently expected me to) if sys.version_info >= (3,): try: some_code() except Exception as e: pass then the entire module (and not just this if-block) will fail to import in Python 2. Therefore, you must not use syntax that is exclusively Python 3 in an if-python3 block if you want to continue to use the code in Python 2 (and if you don't want to use the code in Python 2, you don't need the if block). Then you run the code (as I originally posted) through 2to3, and out you get the block that will then get executed in Python 3. Therefore, it is necessary to convert the code that is meant for Python 3 with 2to3 still. >> While I now understand what is being requested, I still fail to see >> the rationale. In my applications of 2to3, I never look at the generated >> code, so it doesn't bother me at all if print gets another pairs of >> brackets, or if redundant (but dead) import statements are inserted. > > Wow. We live in different mental worlds. It would not have occurred > to me to take the 2to3 output as more than helpful suggestions. Some > to be applied straight (like 'except' syntax), other to maybe apply > but also look closer at nearby code. So please reconsider. Using that approach will allow you to have a single source code for Python 2 and Python 3. You write it so that it works fine on Python 2, and let 2to3 generate the Python 3 version, which you then run unmodified. For that to work, it's important that any modifications that 2to3 won't do will be done *before* invoking 2to3, and these modifications must therefore then work with Python 2 as well (albeit possibly in a block that is never executed on Python 2). If, at some point, you are then ready to burn the bridges (i.e. give up Python 2 support), you run 2to3 once, and start removing all ugliness that you had collected during the transition phase. ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue10070> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com