I hope I’m not being too keen here, but I’m increasingly convinced that fixing shutil.rmtree()
by building upon pathlib.Path.walk()
is the right way to go. The process looks like this:
- Add
pathlib.Path.fwalk()
method, so we can avoid symlink races inrmtree()
- Nicely complements the
walk()
method we added in 3.12. - Draft PR here
- Nicely complements the
- Add optional follow_junctions argument to
pathlib.Path.walk()
, so we can avoid walking into junctions on Windows inrmtree()
- A small patch.
- Implement
shutil.rmtree()
usingpathlib.Path.walk()
andfwalk()
- This makes
shutil.py
about 100 lines shorter, and thermtree()
algorithm more obvious.
- This makes
Note how we don’t need a function/method that yields os.DirEntry
objects at any stage, so this strikes me as the least invasive way to solve the problem. If/when we introduce scantree()
, we could always revisit the rmtree()
implementation.
Also pinging @Ovsyanka, who previously brought up adding Path.fwalk()
.