I'm looking for a way to do this without checking for the Python version used.
Please refer to How to write exception reraising code that's compatible with both Python 2 and Python 3? for details about this, since this question extends that one.
Basically, I can generalize this as, "What if the method-based exception raises a language-based exception?"
According to Python try...except comma vs 'as' in except, the following show the right syntax for Python 3 and Python 2:
Python 3:
except MyError as e
Python 2, for versions 2.6+:
except MyError as e
#OR#
except MyError, e
Python 2.5-:
except MyError, e
A little background:
I have a sticky situation in which a script will need to be run on many an ancient Linux machine, in which a variety of different Python versions, including Python 2.5, will be used.
Unfortunately, I have to distribute this as a single, size limited file, which puts some constraints on how much importing I can do.
Also, I'm interested in the case in which one of these may misreport its version, or in code that can be used without necessarily checking for a version. This could be worked around, though, of course.
six
and see what they do. Nothing about checking which version of Python is running (which usually involves a sample line and catching an exception) implies using two different sets of source code. Also,sys.version_info
is good down through 2.0. Use that. – Banket//
thing? There is no need to and is hugely distracting. – Tymothy