Python 2-3 compatibility

Terry Jan Reedy tjreedy at udel.edu
Sun Jun 2 12:51:19 EDT 2013


On 6/2/2013 10:13 AM, Jason Swails wrote:

> Because Python 2.4 and 2.5 don't support the
>
> except Exception as err:
>
> syntax, I've used
>
> except Exception, err:
>
> Is there any way of getting this effect in a way compatible with Py2.4
> and 3.x?

Don't do either. Just catch the exception with 'except Exception:' and 
access the details in the body. I googled 'python 2 to 3 exceptions' and 
the second hit is http://python3porting.com/noconv.html which covers 
what you are doing ;-). From that page

"If you need to support Python versions lower than Python 2.6 and you 
need access to the exception that was raised, you can get that in all 
versions through the exc_info() function:

 >>> import sys
 >>> try:
...     a = 1/'0'
... except (ZeroDivisionError, TypeError):
...     e = sys.exc_info()[1]
...     print(e.args[0])
unsupported operand type(s) for /: 'int' and 'str'
"
There are more tips on that page, a reference to the six module, and 
more hits on the search page. Good luck. You are not the first to 
support the same range of versions (and for the same reasons).

--
Terry Jan Reedy







More information about the Python-list mailing list