Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

return in finally swallows exceptions #13

Open
iritkatriel opened this issue Oct 24, 2024 · 7 comments
Open

return in finally swallows exceptions #13

iritkatriel opened this issue Oct 24, 2024 · 7 comments
Assignees
Labels
bug Something isn't working

Comments

@iritkatriel
Copy link

In

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

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

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

@CNSeniorious000
Copy link
Member

Thanks. I considered it acceptable because I thought that try-block would only raise an IndexError at the end of the input string.

def skip_blank(text: str, index: int):
try:
while text[index].isspace():
index += 1
finally:
return index

Did you find some other exceptions raised but shallowed inside that block?

@CNSeniorious000
Copy link
Member

I see. You meant KeyboardInterrupt. I think other BaseException wouldn't raise inside that 🤔

Why do you want to interrupt that process? Are you trying to parse a long string with a lot of spaces?

@CNSeniorious000 CNSeniorious000 added bug Something isn't working question Further information is requested labels Oct 24, 2024
@iritkatriel
Copy link
Author

I'm not using this code. I am doing static analysis of many open source projects to understand usages of finally (and incidentally reporting problems when I see them).

@iritkatriel
Copy link
Author

iritkatriel commented Oct 24, 2024

I considered it acceptable because I thought that try-block would only raise an IndexError at the end of the input string.

If your intention is to swallow all exceptions, I'd do that with explicitly with except BaseException: pass to make it clear that it's intentional. Your call.

@CNSeniorious000 CNSeniorious000 removed the question Further information is requested label Oct 24, 2024
@CNSeniorious000 CNSeniorious000 self-assigned this Oct 24, 2024
@CNSeniorious000
Copy link
Member

Thanks for reporting. And I'm a bit curious if you are working on a linting tool or if you are involved in some statistical research? I would love to learn about your findings.

@iritkatriel
Copy link
Author

Thanks for reporting. And I'm a bit curious if you are working on a linting tool or if you are involved in some statistical research? I would love to learn about your findings.

https://github.com/iritkatriel/finally/blob/main/README.md

@CNSeniorious000
Copy link
Member

thx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

When branches are created from issues, their pull requests are automatically linked.

2 participants