[issue10070] 2to3 wishes for already-2to3'ed files

Bobby Impollonia report at bugs.python.org
Sat Nov 13 01:02:45 CET 2010


Bobby Impollonia <bobbyi at gmail.com> added the comment:

> Can you please post an example Python module that has this markup you are asking for, so I can show you how to achieve what you want without the markup?

Hi, Martin. Here's a real-world example:
http://www.sqlalchemy.org/trac/browser/lib/sqlalchemy/types.py?rev=6884:b181f1e53603

The syntax is:
# Py3K
# <python 3 code>
# Py2K
<python 2 code>
# end Py2K

For example, starting on line 152 we have,
        # Py3K
        #return unicode(self.compile())
        # Py2K
        return unicode(self.compile()).\
                        encode('ascii', 'backslashreplace')
        # end Py2K

When the code is converted with 2to3, the py3k code on line 153 will be uncommented and used to replace the py2k code on lines 155-156. Having the py3k version commented before conversion resolves the issue that "you must not use syntax that is exclusively Python 3 in an if-python3 block".

Here is their modified version of 2to3 to support this syntax:
http://www.sqlalchemy.org/trac/browser/sa2to3.py

Their explanation is:
"This tool monkeypatches a preprocessor onto lib2to3.refactor.RefactoringTool, so that conditional sections can replace non-fixable Python 2 code sections for the appropriate Python 3 version before 2to3 is run."

----------
nosy: +bobbyi

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue10070>
_______________________________________


More information about the Python-bugs-list mailing list