Message359888
tempfile.NamedTemporaryFile creates its wrapper like so:
try:
file = _io.open(fd, mode, buffering=buffering,
newline=newline, encoding=encoding, errors=errors)
return _TemporaryFileWrapper(file, name, delete)
except BaseException:
_os.unlink(name)
_os.close(fd)
raise
If _TemporaryFileWrapper throws any kind of exception (even KeyboardInterrupt), this closes `fd` but leaks a valid `file` pointing to that fd. The `file` will later attempt to close the `fd` when it is collected, which can lead to subtle bugs. (This particular issue contributed to this bug: https://nedbatchelder.com/blog/202001/bug_915_please_help.html)
This should probably be rewritten as:
try:
file = _io.open(fd, mode, buffering=buffering,
newline=newline, encoding=encoding, errors=errors)
except:
_os.unlink(name)
_os.close(fd)
raise
try:
return _TemporaryFileWrapper(file, name, delete)
except BaseException:
_os.unlink(name)
file.close()
raise
or perhaps use nested try blocks to avoid the _os.unlink duplication. |
|
Date |
User |
Action |
Args |
2020-01-13 04:56:47 | nneonneo | set | recipients:
+ nneonneo |
2020-01-13 04:56:47 | nneonneo | set | messageid: <1578891407.19.0.452054572225.issue39318@roundup.psfhosted.org> |
2020-01-13 04:56:47 | nneonneo | link | issue39318 messages |
2020-01-13 04:56:46 | nneonneo | create | |
|