Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

return in finally swallows exceptions #651

Closed
iritkatriel opened this issue Oct 28, 2024 · 3 comments · Fixed by #653
Closed

return in finally swallows exceptions #651

iritkatriel opened this issue Oct 28, 2024 · 3 comments · Fixed by #653

Comments

@iritkatriel
Copy link
Contributor

In

there is a return statement in a finally block, which would swallow any in-flight exception.

This means that if an unhandled exception (including a BaseException such as KeyboardInterrupt) is raised from the try body, it will not propagate on as expected.

If the intention is to suppress all exceptions, I would propose to make this clear by using "except BaseException".

See also https://docs.python.org/3/tutorial/errors.html#defining-clean-up-actions.

@fjosw
Copy link
Collaborator

fjosw commented Nov 3, 2024

Hi @iritkatriel, thanks for your suggestion. I'm not sure about the original intention behind this line but also suspect that it is supposed to suppress all exceptions. I added

except BaseException:
    pass

in #653, is this what you had in mind?

@iritkatriel
Copy link
Contributor Author

Then you don't need the finally clause anymore. Just have the return dedented to the level of the except.

@j-towns
Copy link
Collaborator

j-towns commented Nov 4, 2024

I could be wrong, but I'm fairly confident that the intention was just to catch AttributeErrors from the assignments, which I guess could occur when wrapping a callable object that is not an ordinary Python function. Something like

def wraps(fun, namestr="{fun}", docstr="{doc}", **kwargs):
    def _wraps(f):
        try:
            f.__name__ = namestr.format(fun=get_name(fun), **kwargs)
            f.__doc__ = docstr.format(fun=get_name(fun), doc=get_doc(fun), **kwargs)
        except AttributeError: pass
        return f
    return _wraps

I'm not sure why the function was written in the way it was.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants