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
Show file tree
Hide file tree
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
Remove caching of Path.parts
  • Loading branch information
barneygale committed Apr 3, 2023
commit c8d4b384e6106cb486c5f5e4cf1be1fc99225ebf
18 changes: 6 additions & 12 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ class PurePath(object):
"""
__slots__ = (
'_raw_path', '_drv', '_root', '_tail_cached',
'_str', '_hash', '_parts_tuple', '_parts_normcase_cached',
'_str', '_hash', '_parts_normcase_cached',
)
_flavour = os.path

Expand Down Expand Up @@ -544,7 +544,7 @@ def relative_to(self, other, /, *_deprecated, walk_up=False):
raise ValueError(f"{str(self)!r} and {str(other)!r} have different anchors")
if step and not walk_up:
raise ValueError(f"{str(self)!r} is not in the subpath of {str(other)!r}")
parts = ('..',) * step + self.parts[len(path.parts):]
parts = ['..'] * step + self._tail[len(path._tail):]
return path_cls(*parts)

def is_relative_to(self, other, /, *_deprecated):
Expand All @@ -563,16 +563,10 @@ def is_relative_to(self, other, /, *_deprecated):
def parts(self):
"""An object providing sequence-like access to the
components in the filesystem path."""
# We cache the tuple to avoid building a new one each time .parts
# is accessed. XXX is this necessary?
try:
return self._parts_tuple
except AttributeError:
if self.drive or self.root:
self._parts_tuple = (self.drive + self.root,) + tuple(self._tail)
else:
self._parts_tuple = tuple(self._tail)
return self._parts_tuple
if self.drive or self.root:
return (self.drive + self.root,) + tuple(self._tail)
else:
return tuple(self._tail)

def joinpath(self, *args):
"""Combine this path with one or several arguments, and return a
Expand Down
2 changes: 0 additions & 2 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,6 @@ def test_parts_common(self):
p = P('a/b')
parts = p.parts
self.assertEqual(parts, ('a', 'b'))
# The object gets reused.
self.assertIs(parts, p.parts)
# When the path is absolute, the anchor is a separate part.
p = P('/a/b')
parts = p.parts
Expand Down
0