'raise foo from bar' does not get refactored.

Issue #22 open
Joe Amenta
repo owner created an issue

Explicit exception chaining does not get refactored, and it is not supported in Python 2.

Comments (4)

  1. Anonymous

    The typical case I'm looking at is this:

    except UnixyProblem as p:
        raise Problem("blah blah") from p

    Now, in python2 I can achieve something similar:

    raise Problem("blah blah"), None, sys.exc_info[2]

    That preserves the original traceback, allowing me to see the platform specific cause, but handle it through my generic exception. (In my case, I'm using domain sockets on Unix and named pipes on Windows, so I have two completely separate libraries I have no control over.)

    Obviously I can only get that traceback within the exception handler in python 2, whereas python 3 keeps it in the traceback attribute. But, and I realize this is a little kludgey, what if we did this:

    def raise_from(exc, cause):
        typ, inst, traceback = sys.exc_info()
        if inst is exc:
            raise exc, None, traceback
            raise exc

    (If we're outside an exception handler, exc_info returns (None, None, None).)

    Basically, this makes exception wrapping reasonably useful in both 3 and 2, and degrades gracefully otherwise.

  2. Anonymous

    @anonymous, Awkardduet currently fixes it like this:

    --- test2.py    (original)
    +++ test2.py    (refactored)
    @@ -1,4 +1,4 @@
    -except UnixyProblem as p:
    -    raise Problem("blah blah") from p
    +except UnixyProblem, p:
    +    _3to2_exc = Problem(u"blah blah"); _3to2_exc.__cause__ = p; raise _3to2_exc

    Does it meed your needs?

  3. Log in to comment