`pathlib.Path.rename()` and `replace()` may move a file before rejecting a bytes target
Chưa có ai nhận issue này.
- 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ả
Bug report
Bug description:
pathlib.Path.rename() and Path.replace() can successfully move a file and then raise TypeError when the target is a bytes path, or an os.PathLike object whose __fspath__() method returns bytes.
The important issue is not whether pathlib should support bytes paths. Pathlib deliberately requires string paths. The problem is that target validation takes place only after the filesystem has already been modified. A caller that sees the exception may reasonably assume that the rename or replacement failed,even though the source no longer exists and the destination now contains the file.
Reproducer
import os
import tempfile
from pathlib import Path
for method_name in ("rename", "replace"):
with tempfile.TemporaryDirectory() as directory:
source = Path(directory, "source")
target = Path(directory, "target")
source.write_text("payload")
try:
getattr(source, method_name)(os.fsencode(target))
except Exception as error:
print(method_name, type(error).__name__, str(error))
print("source exists:", source.exists())
print("target exists:", target.exists())
print("target contents:", target.read_text())
result:
rename TypeError argument should be a str or an os.PathLike object where __fspath__ returns a str, not 'bytes'
source exists: False
target exists: True
target contents: payload
replace TypeError argument should be a str or an os.PathLike object where __fspath__ returns a str, not 'bytes'
source exists: False
target exists: True
target contents: payload
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
- gh-156036
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng cách chạy reproducer được cung cấp, sau đó tìm pathlib.Path.rename() và Path.replace() rồi theo dõi thời điểm target của chúng được xác thực. Hoàn thành khi các target bytes bị từ chối trước khi một trong hai thao tác thay đổi trạng thái của source hoặc destination, với coverage cho cả hai method.
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
- Lỗi
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- 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
- 35/100