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
Apply suggestions from code review
  • Loading branch information
brettcannon authored Jul 22, 2022
commit eef6054b680a6ea06e8dbaf803beabe7c50c8a9f
4 changes: 2 additions & 2 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,7 @@ def expanduser(self):
return self

def walk(self, top_down=True, on_error=None, follow_symlinks=False):
"""Generate a top-down directory tree from this directory, similar to os.walk()"""
"""Walk the directory tree from this directory, similar to os.walk()."""
sys.audit("pathlib.Path.walk", self, on_error, follow_symlinks)
return self._walk(top_down, on_error, follow_symlinks)

Expand All @@ -1342,7 +1342,7 @@ def _walk(self, top_down, on_error, follow_symlinks):
try:
is_dir = entry.is_dir(follow_symlinks=follow_symlinks)
except OSError:
# same behavior as os.path.isdir()
# Carried over from os.path.isdir().
is_dir = False

if is_dir:
Expand Down
30 changes: 13 additions & 17 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2479,10 +2479,8 @@ def test_complex_symlinks_relative_dot_dot(self):
self._check_complex_symlinks(os.path.join('dirA', '..'))

class WalkTests(unittest.TestCase):
cls = pathlib.Path

def setUp(self):
P = self.cls
self.addCleanup(os_helper.rmtree, os_helper.TESTFN)

# Build:
Expand All @@ -2502,7 +2500,7 @@ def setUp(self):
# broken_link3
# TEST2/
# tmp4 a lone file
self.walk_path = P(os_helper.TESTFN, "TEST1")
self.walk_path = pathlib.Path(os_helper.TESTFN, "TEST1")
self.sub1_path = self.walk_path / "SUB1"
self.sub11_path = self.sub1_path / "SUB11"
self.sub2_path = self.walk_path / "SUB2"
Expand All @@ -2512,8 +2510,8 @@ def setUp(self):
tmp3_path = self.sub2_path / "tmp3"
tmp5_path = sub21_path / "tmp3"
self.link_path = self.sub2_path / "link"
t2_path = P(os_helper.TESTFN, "TEST2")
tmp4_path = P(os_helper.TESTFN, "TEST2", "tmp4")
t2_path = pathlib.Path(os_helper.TESTFN, "TEST2")
tmp4_path = pathlib.Path(os_helper.TESTFN, "TEST2", "tmp4")
broken_link_path = self.sub2_path / "broken_link"
broken_link2_path = self.sub2_path / "broken_link2"
broken_link3_path = self.sub2_path / "broken_link3"
Expand All @@ -2530,8 +2528,8 @@ def setUp(self):
if os_helper.can_symlink():
os.symlink(os.path.abspath(t2_path), self.link_path)
os.symlink('broken', broken_link_path, True)
os.symlink(P('tmp3', 'broken'), broken_link2_path, True)
os.symlink(P('SUB21', 'tmp5'), broken_link3_path, True)
os.symlink(pathlib.Path('tmp3', 'broken'), broken_link2_path, True)
os.symlink(pathlib.Path('SUB21', 'tmp5'), broken_link3_path, True)
self.sub2_tree = (self.sub2_path, ["SUB21"],
["broken_link", "broken_link2", "broken_link3",
"link", "tmp3"])
Expand All @@ -2552,7 +2550,6 @@ def setUp(self):
del self.sub2_tree[1][:1]

def test_walk_topdown(self):
P = self.cls
all = list(self.walk_path.walk())

self.assertEqual(len(all), 4)
Expand Down Expand Up @@ -2658,26 +2655,25 @@ def test_walk_bad_dir(self):
path1new.rename(path1)

def test_walk_many_open_files(self):
P = self.cls
depth = 30
base = P(os_helper.TESTFN, 'deep')
p = P(base, *(['d']*depth))
p.mkdir(parents=True)
base = pathlib.Path(os_helper.TESTFN, 'deep')
path = pathlib.Path(base, *(['d']*depth))
path.mkdir(parents=True)

iters = [base.walk(top_down=False) for _ in range(100)]
for i in range(depth + 1):
expected = (p, ['d'] if i else [], [])
expected = (path, ['d'] if i else [], [])
for it in iters:
self.assertEqual(next(it), expected)
p = p.parent
path = path.parent

iters = [base.walk(top_down=True) for _ in range(100)]
p = base
path = base
for i in range(depth + 1):
expected = (p, ['d'] if i < depth else [], [])
expected = (path, ['d'] if i < depth else [], [])
for it in iters:
self.assertEqual(next(it), expected)
p = p / 'd'
path = path / 'd'


class PathTest(_BasePathTest, unittest.TestCase):
Expand Down
0