Issue 18893: invalid exception handling in Lib/ctypes/macholib/dyld.py (original) (raw)

The exception handling clauses in framework_find() are weird.

def framework_find(fn, executable_path=None, env=None): """ Find a framework using dyld semantics in a very loose manner.

Will take input such as:
    Python
    Python.framework
    Python.framework/Versions/Current
"""
try:
    return dyld_find(fn, executable_path=executable_path, env=env)
except ValueError as e:
    pass
fmwk_index = fn.rfind('.framework')
if fmwk_index == -1:
    fmwk_index = len(fn)
    fn += '.framework'
fn = os.path.join(fn, os.path.basename(fn[:fmwk_index]))
try:
    return dyld_find(fn, executable_path=executable_path, env=env)
except ValueError:
    raise e

My guess is that this is left-over code from Py2.x. Since it doesn't make sense to catch an exception in the second clause just to re-raise it, I think the intention was really to re-raise the original exception caught in the first clause, which no longer works that way in Py3.

The fix would then be to assign the exception to a new variable in the first except clause and re-raise that in the second.

I found this problem because Cython rejected the module with a compile error about "e" being undefined in the last line.