8000 gh-90385: Add `pathlib.Path.walk()` method by zmievsa · Pull Request #92517 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-90385: Add pathlib.Path.walk() method #92517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
ac622b7
Add Path.walk and Path.walk_bottom_up methods
zmievsa May 8, 2022
14f031a
Fix errors in Path.walk docstrings and add caching of entries
zmievsa May 9, 2022
b203517
Merge branch 'main' into bpo-46227/add-pathlib.Path.walk-method
Ovsyanka83 May 9, 2022
3ad60a9
Refactor symlink handling
zmievsa May 9, 2022
889d7fe
Merge branch 'bpo-46227/add-pathlib.Path.walk-method' of github.com:O…
zmievsa May 9, 2022
2f98823
Add Path.walk docs and unite Path.walk interfaces
zmievsa May 10, 2022
513030a
Remove Path.walk_bottom_up definition
zmievsa May 10, 2022
5fdd72e
📜🤖 Added by blurb_it.
blurb-it[bot] May 10, 2022
452f24e
Add Path.walk tests
zmievsa May 10, 2022
3702a12
Make Path.walk variable naming consistent
zmievsa May 10, 2022
fabc925
Remove redundant FIXME
zmievsa May 10, 2022
b387b54
Minor Path.walk docs and tests fixes
zmievsa May 10, 2022
097fbbf
Merge branch 'main' into bpo-46227/add-pathlib.Path.walk-method
merwok Jun 27, 2022
76fadfc
Update Doc/library/pathlib.rst
Ovsyanka83 Jun 30, 2022
0c19871
Update Doc/library/pathlib.rst
Ovsyanka83 Jun 30, 2022
50b4a2b
Update Doc/library/pathlib.rst
Ovsyanka83 Jun 30, 2022
cade3e9
Update Doc/library/pathlib.rst
Ovsyanka83 Jun 30, 2022
b32627c
Update Doc/library/pathlib.rst
Ovsyanka83 Jun 30, 2022
d1a0833
Update Doc/library/pathlib.rst
Ovsyanka83 Jun 30, 2022
e367f1f
Update Doc/library/pathlib.rst
Ovsyanka83 Jun 30, 2022
bf8b0eb
Fix 'no blank lines' error
zmievsa Jun 30, 2022
d8667c7
Apply suggestions from code review
Ovsyanka83 Jul 3, 2022
4509797
More code review fixes for Path.walk
zmievsa Jul 3, 2022
20a73ed
Merge branch 'main' into bpo-46227/add-pathlib.Path.walk-method
Ovsyanka83 Jul 3, 2022
e61d57b
Merge branch 'main' into bpo-46227/add-pathlib.Path.walk-method
brettcannon Jul 8, 2022
15d96b9
Apply suggestions from code review
Ovsyanka83 Jul 9, 2022
92e1a7a
Apply suggestions from code review
Ovsyanka83 Jul 9, 2022
c509da3
Merge branch 'main' into bpo-46227/add-pathlib.Path.walk-method
Ovsyanka83 Jul 9, 2022
cfa730d
Code review fixes
zmievsa Jul 10, 2022
7aec96d
Clarify pathlib.Path.walk() error handling
zmievsa Jul 10, 2022
38fe1e5
Apply suggestions from code review
Ovsyanka83 Jul 10, 2022
eef3ba3
Code review fixes
zmievsa Jul 10, 2022
4dfdcd7
Merge branch 'bpo-46227/add-pathlib.Path.walk-method' of github.com:O…
zmievsa Jul 10, 2022
8fe3b62
Apply suggestions from code review
Ovsyanka83 Jul 12, 2022
e8ea6ba
Code review fixes
zmievsa Jul 12, 2022
79cf8fd
Remove backticks around True and False
zmievsa Jul 13, 2022
bed850e
Apply suggestions from code review
Ovsyanka83 Jul 17, 2022
203ec3d
Apply suggestions from code review
zmievsa Jul 17, 2022
eef6054
Apply suggestions from code review
brettcannon Jul 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Code review fixes
  • Loading branch information
zmievsa committed Jul 10, 2022
commit cfa730d48c4ba26dd767433e94213a303c1799a8
8 changes: 4 additions & 4 deletions Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ call fails (for example because the path doesn't exist).
does not keep track of the directories it has already visited.

.. note::
This method assumes that the current working directory does not change while
:meth:`Path.walk` assumes that the current working directory does not change while
walking relative paths.

.. note::
Expand All @@ -1012,10 +1012,10 @@ call fails (for example because the path doesn't exist).
.. note::

Unlike :func:`os.walk`, :meth:`Path.walk` lists symlinks to directories into
*filenames* if *follow_symlinks* is ``True``.
*filenames* if *follow_symlinks* is ``False``.

This example displays the number of bytes used by all files in each directory,
while ignoring `__pycache__` directories.
while ignoring ``__pycache__`` directories.
:file:`__pycache__` subdirectory::

from pathlib import Path
Expand All @@ -1031,7 +1031,7 @@ call fails (for example because the path doesn't exist).
if '__pycache__' in dirs:
dirs.remove('__pycache__')

This next example is asimple implementation of :func:`shutil.rmtree`.
This next example is a simple implementation of :func:`shutil.rmtree`.
Walking the tree bottom-up is essential as :func:`rmdir` doesn't allow
deleting a directory before it is empty::

Expand Down
44 changes: 30 additions & 14 deletions Lib/test/test_pathlib.py
10000
Original file line number Diff line number Diff line change
Expand Up @@ -2480,11 +2480,11 @@ def test_complex_symlinks_relative_dot_dot(self):

class WalkTests(unittest.TestCase):
"""Tests for Path.walk()

They are mostly just copies of os.walk tests converted to use Paths
because os.walk tests are already mature and cover many edge cases
that we could miss writing new tests.

It is not combined with Path* tests because its setup is aimed at
testing a bit more complex cases than Path's setup.
"""
Expand All @@ -2510,7 +2510,7 @@ def setUp(self):
# tmp3
# SUB21/ not readable
# tmp5
# link/ a symlink to TESTFN.2
# link/ a symlink to TEST2
# broken_link
# broken_link2
# broken_link3
Expand All @@ -2519,22 +2519,22 @@ def setUp(self):
self.walk_path = P(os_helper.TESTFN, "TEST1")
self.sub1_path = self.walk_path / "SUB1"
self.sub11_path = self.sub1_path / "SUB11"
sub2_path = self.walk_path / "SUB2"
sub21_path= sub2_path / "SUB21"
self.sub2_path = self.walk_path / "SUB2"
sub21_path= self.sub2_path / "SUB21"
tmp1_path = self.walk_path / "tmp1"
tmp2_path = self.sub1_path / "tmp2"
tmp3_path = sub2_path / "tmp3"
tmp3_path = self.sub2_path / "tmp3"
tmp5_path = sub21_path / "tmp3"
self.link_path = sub2_path / "link"
self.link_path = self.sub2_path / "link"
t2_path = P(os_helper.TESTFN, "TEST2")
tmp4_path = P(os_helper.TESTFN, "TEST2", "tmp4")
broken_link_path = sub2_path / "broken_link"
broken_link2_path = sub2_path / "broken_link2"
broken_link3_path = sub2_path / "broken_link3"
broken_link_path = self.sub2_path / "broken_link"
broken_link2_path = self.sub2_path / "broken_link2"
broken_link3_path = self.sub2_path / "broken_link3"

# Create stuff.
os.makedirs(self.sub11_path)
os.makedirs(sub2_path)
os.makedirs(self.sub2_path)
os.makedirs(sub21_path)
os.makedirs(t2_path)

Expand All @@ -2547,11 +2547,11 @@ def setUp(self):
os.symlink('broken', broken_link_path, True)
os.symlink(join('tmp3', 'broken'), broken_link2_path, True)
os.symlink(join('SUB21', 'tmp5'), broken_link3_path, True)
self.sub2_tree = (sub2_path, ["SUB21"],
self.sub2_tree = (self.sub2_path, ["SUB21"],
["broken_link", "broken_link2", "broken_link3",
"link", "tmp3"])
else:
self.sub2_tree = (sub2_path, ["SUB21"], ["tmp3"])
self.sub2_tree = (self.sub2_path, ["SUB21"], ["tmp3"])

if not is_emscripten:
# Emscripten fails with inaccessible directory
Expand Down Expand Up @@ -2628,7 +2628,7 @@ def test_walk_bottom_up(self):
self.sub2_tree)

@os_helper.skip_unless_symlink
def test_walk_symlink(self):
def test_walk_follow_symlinks(self):
# Walk, following symlinks.
walk_it = self.walk(self.walk_path, follow_symlinks=True)
for root, dirs, files in walk_it:
Expand All @@ -2639,6 +2639,22 @@ def test_walk_symlink(self):
else:
self.fail("Didn't follow symlink with follow_symlinks=True")

def test_walk_symlink_location(self):
""" Tests whether symlinks end up in filenames or dirnames depending
on the `follow_symlinks` argument
"""
walk_it = self.walk(self.walk_path, follow_symlinks=False)
for root, dirs, files in walk_it:
if root == self.sub2_path:
self.assertIn("link", files)
break

walk_it = self.walk(self.walk_path, follow_symlinks=True)
for root, dirs, files in walk_it:
if root == self.sub2_path:
self.assertIn("link", dirs)
break

def test_walk_bad_dir(self):
# Walk top-down.
errors = []
Expand Down
0