[New-bugs-announce] [issue34066] Possible resource warning in "with open()"
Serhiy Storchaka
report at bugs.python.org
Sat Jul 7 13:11:52 EDT 2018
New submission from Serhiy Storchaka <storchaka+cpython at gmail.com>:
The bytecode generated for "with open()":
with open(path) as file:
data = file.read()
1 0 LOAD_NAME 0 (open)
2 LOAD_NAME 1 (path)
4 CALL_FUNCTION 1
6 SETUP_WITH 14 (to 22)
8 STORE_NAME 2 (file)
2 10 LOAD_NAME 2 (file)
12 LOAD_METHOD 3 (read)
14 CALL_METHOD 0
16 STORE_NAME 4 (data)
18 POP_BLOCK
20 BEGIN_FINALLY
>> 22 WITH_CLEANUP_START
24 WITH_CLEANUP_FINISH
26 END_FINALLY
28 LOAD_CONST 0 (None)
30 RETURN_VALUE
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 issue29988.
----------
components: Interpreter Core
messages: 321224
nosy: Mark.Shannon, benjamin.peterson, ncoghlan, serhiy.storchaka, yselivanov
priority: normal
severity: normal
status: open
title: Possible resource warning in "with open()"
type: resource usage
versions: Python 3.6, Python 3.7, Python 3.8
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34066>
_______________________________________
More information about the New-bugs-announce
mailing list