python / python/cpython

Promote _delete member of TemporaryDirectory to delete

Đang mở
#128,397 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

stdlib type-feature
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với TemporaryDirectory và vòng đời enter/exit của nó, tập trung vào cách quyết định xóa được lưu trữ và sử dụng sau khi đã vào context. Xem lại PR gh-128643 được liên kết để nắm công việc đang được thực hiện; hoàn thành nghĩa là thuộc tính công khai delete hỗ trợ thay đổi hành vi dọn dẹp sau enter.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
operating-systems
Loại issue
Tính năng
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.