raise_from for PY3 bug #213

Closed
shoshber opened this Issue Jun 17, 2016 · 2 comments

Projects

None yet

3 participants

@shoshber
shoshber commented Jun 17, 2016 edited

raise_from throws NameError when the second parameter is an exception imported via import <module>.

See this commit where I had to work around it:
nipy/nipype@ec075eb#diff-2e44cd91a342e50500957ba5eb213f41R12

I suspect the problem is with _repr_strip, but I could not confirm: https://github.com/PythonCharmers/python-future/blob/master/src/future/utils/__init__.py#L397

@Varriount

This happens because raise_from tries to recreate the exact expression used to create the exception. Of course, this fails when the exception's repr method doesn't match up with the expression, or when the expression can't be directly evaluated.

It seems to me that the proper fix here would be to shove the 'exc' and 'cause' parameter values into the dictionaries passed into the 'exec' call:

def raise_from(exc, cause):
    """
    Equivalent to:

        raise EXCEPTION from CAUSE

    on Python 3. (See PEP 3134).
    """
    myglobals, mylocals = _get_caller_globals_and_locals()
    myglobals = myglobals.copy()
    myglobals['___python_future_raise_from_exc'] = exc
    myglobals['___python_future_raise_from_cause'] = cause

    execstr = "raise __python_future_raise_from_exc from __python_future_raise_from_cause"
    exec(execstr, myglobals, mylocals)
@edschofield
Contributor

Shoshana, thanks for reporting this! I think this is fixed now in v0.16.0 with @Varriount's patch. Please feel free to re-open this if you see any more problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment