python / python/cpython

Promote _delete member of TemporaryDirectory to delete

Open
#128,397 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Feature or enhancement

Proposal:

TemporaryDirectory.__delete__ gained a delete parameter to support retaining temporary files for diagnostic purposes.

In a running system, you may wish gather diagnostic files on failure, or when a specific signal is received. Supporting this fully requires allowing the decision of whether to delete files to be made after the TemporaryDirectory has been __enter__ed.

Assigning to _delete can achieve this effect, but _delete is not part of the public interface.

Therefore, I propose changing TemporaryDirectory._delete to TemporaryDirectory.delete.

Example use to retain temporary files on error:

with TemporaryDirectory() as temp_dir:
    try:
        thing_that_may_raise()
    except Exception:
        temp_dir.delete = False
        raise

Alternatives considered:

  • Provide a function to control this behaviour
    • We can always turn delete into a property if necessary
  • Users can provide subclasses of TemporaryDirectory to achieve this
    • Temporary Directory doesn't have enough of a public API for this to be viable. Using the private API would be morally equivalent to modifying _delete, and modifying _delete would be easier.
  • Users can provide a wrapper to prevent __exit__ from being called
    • __del__ might be implemented in the future, as it is for TemporaryFile.
Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs
  • gh-128643

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with TemporaryDirectory and its enter/exit lifecycle, focusing on how the deletion decision is stored and used after entering the context. Review the linked PR gh-128643 for the work already underway; done means the public delete attribute supports changing cleanup behavior after enter.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
operating-systems
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.