> 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." |