python / python/cpython

Promote _delete member of TemporaryDirectory to delete

未关闭
#128,397 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stdlib type-feature
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 TemporaryDirectory 及其 enter/exit 生命周期入手,重点关注进入上下文后删除决策是如何存储和使用的。查看链接的 PR gh-128643,了解已经在进行的工作;完成的标准是公共 delete 属性支持在 enter 之后更改清理行为。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
operating-systems
Issue 类型
功能
难度
2/5
预计耗时
1-3 小时
活跃度
停滞
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。