Search code examples
pythonpython-3.xsyntaxcompatibilitypython-2.x

making python 3 exception backward compatible


What is the backward (2.6 if possible) compatible syntax of this code block (from PEP 3109) :

try:
   self.spawn(pp_args)
except DistutilsExecError as msg:
   raise CompileError from msg

Solution

  • This is as close as you'd come in python-2.x:

    try:
       self.spawn(pp_args)
    except DistutilsExecError as msg:
        print "DistutilsExecError : " + str(DistutilsExecError(msg))
        print
        print "The above exception was the direct cause of the following exception:"
        raise CompileError