Python 2-3 compatibility

Dan Stromberg drsalists at gmail.com
Sun Jun 2 12:41:34 EDT 2013


From
http://stromberg.dnsalias.org/~dstromberg/Intro-to-Python/Python%202%20and%203.pdf:

Try/Except: both 2.x and 3.x

try:
   print(1/0)
except ZeroDivisionError:
   extra = sys.exc_info()[1]
   print('oops')

HTH



On Sun, Jun 2, 2013 at 7:13 AM, Jason Swails <jason.swails at gmail.com> wrote:

> Hello Everyone,
>
> I have a Python script that I wrote to support a project that I work on
> (that runs primarily on Unix OSes).  Given its support role in this
> package, this script should not introduce any other dependencies.  As a
> result, I wrote the script in Python 2, since every Linux currently ships
> with 2.4--2.7 as its system Python (RHEL 5, still popular in my field,
> ships with 2.4).
>
> However, I've heard news that Ubuntu is migrating to Python 3 soon (next
> release??), and that's a platform we actively try to support due to its
> popularity.  I've tried writing the code to support both 2 and 3 as much as
> possible, but with priority put on supporting 2.4 over 3.
>
> Now that Python 3-compatibility is about to become more important, I'm
> looking for a way to catch and store exceptions in a compatible way.
>
> 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?  Of course I could duplicate every module with 2to3 and do
> sys.version_info-conditional imports, but I'd rather avoid duplicating all
> of the code if possible.
>
> Any suggestions are appreciated.
>
> Thanks!
> Jason
>
> --
> Jason M. Swails
> Quantum Theory Project,
> University of Florida
> Ph.D. Candidate
> 352-392-4032
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130602/5b3f562c/attachment.html>


More information about the Python-list mailing list