This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author serhiy.storchaka
Recipients Albert.Zeyer, Seth.Troisi, hitbox, joernheissler, nedbat, nneonneo, sanjioh, serhiy.storchaka
Date 2020-01-14.09:07:53
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>

1. In general, it is hard to avoid leaks because an exception like KeyboardInterrupt or MemoryError can be raised virtually anywhere, even before we save a file descriptor. We may rewrite the code so that it will use few simple bytecode instructions and disable interruption before these instructions. This may solve the problem in CPython, but alternate implementation will need to handle this theirself.

2. It is not safe to close a file descriptor if is failed, because it can already be closed in, and we do not know where it was closed or no. It can be solved by using the opener argument, but we will need to patch the name attribute of the file returned by, and it is not easy, because the type of the result of depends on the mode and buffering arguments, and only FileIO has writable name attribute. This will need additional complex code. We can also change the behavior of, make it either always closing fd or never closing it in case of error. But this will break working code, or introduce leaks in working code, so such change cannot be backported. In any case we should revisit all other cases of using with an open file descriptor in the stdlib (including TemporaryFile).

3. I afraid that removing a file while the file descriptor is open may not work on Windows. Seems this case is not well tested.

As for using `except Exception` instead of `except BaseException`, it is better to use the later, or even the bare `except`.

I worked on this issue yesterday, but then found new problems, it will take several days or weeks to solve them. As a workaround I suggest to skip temporary the test which monkeypatches _TemporaryFileWrapper.
Date User Action Args
2020-01-14 09:07:54serhiy.storchakasetrecipients: + serhiy.storchaka, nneonneo, nedbat, Seth.Troisi, Albert.Zeyer, sanjioh, joernheissler, hitbox
2020-01-14 09:07:54serhiy.storchakasetmessageid: <>
2020-01-14 09:07:54serhiy.storchakalinkissue39318 messages
2020-01-14 09:07:53serhiy.storchakacreate