-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
Possible resource warning in "with open()" #78247
Comments
The bytecode generated for "with open()": with open(path) as file:
data = file.read() 1 0 LOAD_NAME 0 (open) 2 10 LOAD_NAME 2 (file) The execution can be interrupted by Ctrl-C between calling open() and entering the 'with' block. In this case the file object will be created, but its __enter__ and __exit__ methods will be not executed. As a result it will be closed after disappearing a reference to it and a ResourceWarning will be emitted. The solution is disabling interruption before the SETUP_WITH opcode. It is already disabled before SETUP_FINALLY and YIELD_FROM. It is worth to disable it before BEFORE_ASYNC_WITH for consistency although I don't have examples for it. See also bpo-29988. |
Maybe it is worth to disable interrupting before more opcodes. For example for fixing a problem with the contextlib.nullcontext example (bpo-34067) we need to disable interrupting before STORE_FAST, LOAD_FAST and JUMP_FORWARD. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: