8000 GH-101362: Omit path anchor from `pathlib.PurePath()._parts` by barneygale · Pull Request #102476 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-101362: Omit path anchor from pathlib.PurePath()._parts #102476

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
Changes from 1 commit
Commits
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
Optimize _make_child_relpath()
  • Loading branch information
barneygale committed Apr 3, 2023
commit 7a4e92fdf1d87d5f6b560c118e3ce827082619cd
30 changes: 20 additions & 10 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def __init__(self, name, child_parts, flavour):

def _select_from(self, parent_path, is_dir, exists, scandir, normcase):
try:
path = parent_path._make_child_relpath(self.name)
path = parent_path._make_child(self.name)
if (is_dir if self.dironly else exists)(path):
for p in self.successor._select_from(path, is_dir, exists, scandir, normcase):
yield p
Expand Down Expand Up @@ -154,7 +154,7 @@ def _select_from(self, parent_path, is_dir, exists, scandir, normcase):
continue
name = entry.name
if self.match(normcase(name)):
path = parent_path._make_child_relpath(name)
path = parent_path._make_child(name)
for p in self.successor._select_from(path, is_dir, exists, scandir, normcase):
yield p
except PermissionError:
Expand All @@ -181,7 +181,7 @@ def _iterate_directories(self, parent_path, is_dir, scandir):
if not _ignore_error(e):
raise
if entry_is_dir and not entry.is_symlink():
path = parent_path._make_child_relpath(entry.name)
path = parent_path._make_child(entry.name)
for p in self._iterate_directories(path, is_dir, scandir):
yield p
except PermissionError:
Expand Down Expand Up @@ -703,11 +703,21 @@ def __new__(cls, *args, **kwargs):
cls = WindowsPath if os.name == 'nt' else PosixPath
return object.__new__(cls)

def _make_child_relpath(self, part):
# This is an optimization used for dir walking. `part` must be
# a single part relative to this path.
tail = self._tail + [part]
return self._from_parsed_parts(self.drive, self.root, tail)
def _make_child(self, name):
path_str = str(self)
tail = self._tail
if tail:
path_str = f'{path_str}{self._flavour.sep}{name}'
elif path_str != '.':
path_str = f'{path_str}{name}'
else:
path_str = name
path = type(self)(path_str)
path._str = path_str
path._drv = self.drive
path._root = self.root
path._tail_cached = tail + [name]
return path

def __enter__(self):
# In previous versions of pathlib, __exit__() marked this path as
Expand Down Expand Up @@ -762,7 +772,7 @@ def iterdir(self):
special entries '.' and '..' are not included.
"""
for name in os.listdir(self):
yield self._make_child_relpath(name)
yield self._make_child(name)

def _scandir(self):
# bpo-24132: a future version of pathlib will support subclassing of
Expand Down Expand Up @@ -1244,7 +1254,7 @@ def walk(self, top_down=True, on_error=None, follow_symlinks=False):
else:
paths.append((path, dirnames, filenames))

paths += [path._make_child_relpath(d) for d in reversed(dirnames)]
paths += [path._make_child(d) for d in reversed(dirnames)]


class PosixPath(Path, PurePosixPath):
Expand Down
0