'raise foo from bar' does not get refactored.

Joe Amenta avatarJoe Amenta 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:

    try:
        unix_foo()
    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
        else:
            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 @@
     try:
         unix_foo()
    -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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.