bpo-29982: Add "ignore_cleanup_errors" param to tempfile.TemporaryDirectory() by CAM-Gerlach · Pull Request #24793 · python/cpython (original) (raw)

Resolves bpo-29982 by adding an ignore_cleanup_errors parameter to tempfile.TemporaryDirectory(), which allows for a best-effort cleanup without stopping on errors, such as PermissionErrors on Windows due to files being open, transient access failures or other runtime exceptions. This behavior matches that when passing ignore_errors=True to shutil.rmtree, and allows using the nicer higher-level interface, context-manager support and more robust removal implementation of tempfile.TemporaryDirectory() to clean up as much of the temporary directory as possible, without stopping and raising an exception mid-cleanup, which can otherwise potentially occur at a non-deterministic time when the directory is garbage-collected, or during interpreter shutdown.

Additionally, as the other part of the issue described in bpo-29982 (and as required for the tests and many real-world use cases), ensures that cleanup() runs correctly on subsequent attempts if the directory was not completely cleaned up after the first call, instead of silently (and surprisingly to the user) failing to do anything at all.

Example use cases for this include regebro/pyroma#28 (which prompted this proposal), the original scenario described in bpo-29982 , and one of the two use cases described by @asottile in bpo-25024 Since I assume everything will be squashed to one commit anyway, I've split this up into several to make it easier to review and modify.

Key implementation questions to draw reviewer attention to:

Thanks!

https://bugs.python.org/issue29982