`pathlib.Path.rename()` and `replace()` may move a file before rejecting a bytes target
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
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
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先运行提供的 reproducer,然后定位 pathlib.Path.rename() 和 Path.replace(),并跟踪它们的目标何时经过验证。完成标准是:在任一操作改变源或目标状态之前拒绝 bytes 目标,并为两种方法都提供覆盖。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- operating-systems
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100