Atomic append fails to create a missing file
评估
调研方向
从 _write_file_atomic 开始,在 append=True 时,目标文件会在创建临时文件之前被无条件读取。使用 issue 中的复现步骤,设置 atomic=True,并让父目录和文件都不存在,然后与 atomic=False 路径进行比较。完成的标准是:原子追加会创建缺失的文件,第二次追加保留之前的内容,并且现有测试仍然通过。
由索引模型根据 Issue 内容生成。
描述
Describe the bug
write_file(path, content, append=True, atomic=True) raises OSError: Invalid file path when the destination does not exist. The same call with atomic=False creates the file, consistent with append mode.
Reproduce
from pathlib import Path
from tempfile import TemporaryDirectory
import fsutil
with TemporaryDirectory() as directory:
path = str(Path(directory) / "new" / "file.txt")
fsutil.write_file(path, content="first", append=True, atomic=True)
Reproduced with Python 3.12.14 on Windows and python-fsutil 0.18.0 source at d3a77c552346dc72a081284a6fd017ecc1ba234f. No mocked filesystem or injected failure is required. This was found during a code review, rather than reported from a production application.
Expected behavior
Create the missing file, as non-atomic append does. A subsequent append should preserve its previous content.
Cause and scope
_write_file_atomic reads the destination unconditionally when append=True, before creating a temporary file. Reading a missing destination raises immediately.
This differs from #194: it does not concern errors after replacement or the Windows-specific FileNotFoundError cleanup handler. Those paths can remain unchanged.
AI assistance was used to investigate the source and prepare reproduction and tests. A focused fix is ready.
- 主要语言
- Python
- 星标
- 268
- 派生
- 19
- 平均合并
- 8 天 13 小时
- 30 天内合并 PR
- 4
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 没有贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
fabiocaccamo/python-fsutil 的其他 Issue
-
Add `toml` files operation support.可能重新可做 @fabiocaccamo 于 1321 天前认领,目前没有进行中的 PR。 未关闭enhancement
fabiocaccamo/python-fsutil#42 · 已指派 1 人 ·
查看 fabiocaccamo/python-fsutil 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 68/100
Deltares/imod-python#1940 ·
维护者通常 1 天内回复
-
effort: low prio: low ready
难度 1/5 1 小时以内 新手友好度 82/100
digitalfabrik/integreat-cms#4621 ·
维护者通常 5 天内回复
-
难度 1/5 1 小时以内 新手友好度 88/100
ioos/compliance-checker#1375 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复
-
netbox status: backlog topic: testing type: housekeeping
难度 2/5 1-3 小时 新手友好度 70/100
netbox-community/netbox#23395 ·
维护者通常 1 天内回复