Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

GH-100479: Add pathlib.PurePath.with_segments() #103975

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 24 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a6fdd0e
Add `pathlib.PurePath.makepath()`; unify path object construction
barneygale Nov 20, 2022
b061747
Fix reST role name.
barneygale Dec 24, 2022
99eb8b1
Move call to `os.getcwd()` back into `Path.cwd()`
barneygale Dec 24, 2022
4759d01
Merge branch 'main' into gh-100479-add-makepath
barneygale Jan 5, 2023
ef6f4c3
Merge branch 'main' into gh-100479-add-makepath
barneygale Apr 3, 2023
595b8ae
Add news blurb.
barneygale Apr 3, 2023
dcfe70a
Merge branch 'main' into gh-100479-add-makepath
barneygale Apr 9, 2023
117fe4b
Add whatsnew entry
barneygale Apr 10, 2023
e75dedc
Merge branch 'main' into gh-100479-add-makepath
barneygale Apr 12, 2023
5a6bd3f
Merge branch 'main' into gh-100479-add-makepath
barneygale Apr 13, 2023
f2f1048
other --> pathsegments
barneygale Apr 24, 2023
3c172fb
Update Lib/pathlib.py
barneygale Apr 24, 2023
4637109
joinpath(*args) --> joinpath(*pathsegments)
barneygale Apr 24, 2023
ae48454
Restore _PathParents
barneygale Apr 25, 2023
e7a8fe3
Add note to `parents` about potential reference cycle.
barneygale Apr 25, 2023
b3bb8bd
makepath() --> __newpath__()
barneygale Apr 28, 2023
cdedc92
Look up `__newpath__` on path type.
barneygale Apr 29, 2023
8341566
`__newpath__` --> `_newpath_`
barneygale Apr 29, 2023
610b0c4
`_newpath_` --> `with_segments`
barneygale May 1, 2023
7f28ed3
`with_segments` --> `with_path`
barneygale May 1, 2023
51fe663
Merge branch 'main' into gh-100479-add-makepath-redux
barneygale May 2, 2023
961e2bf
Revert "`with_segments` --> `with_path`"
barneygale May 4, 2023
39297cf
Merge branch 'main' into gh-100479-add-makepath-redux
barneygale May 5, 2023
4e28944
`*args` --> `*pathsegments`
barneygale May 5, 2023
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
Restore _PathParents
  • Loading branch information
barneygale committed Apr 25, 2023
commit ae4845441281e9b6cf2623388df28587afa239c2
6 changes: 2 additions & 4 deletions 6 Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,8 @@ Pure paths provide the following methods and properties:

.. attribute:: PurePath.parents

A tuple providing access to the logical ancestors of the path::
An immutable sequence providing access to the logical ancestors of
the path::

>>> p = PureWindowsPath('c:/foo/bar/setup.py')
>>> p.parents[0]
Expand All @@ -372,9 +373,6 @@ Pure paths provide the following methods and properties:
.. versionchanged:: 3.10
The parents sequence now supports :term:`slices <slice>` and negative index values.

.. versionchanged:: 3.12
Type changed from a tuple-like immutable sequence to a true tuple.

.. attribute:: PurePath.parent

The logical parent of the path::
Expand Down
39 changes: 32 additions & 7 deletions 39 Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import re
import sys
import warnings
from _collections_abc import Sequence
from errno import ENOENT, ENOTDIR, EBADF, ELOOP
from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO
from urllib.parse import quote_from_bytes as urlquote_from_bytes
Expand Down Expand Up @@ -206,6 +207,35 @@ def _select_from(self, parent_path, is_dir, exists, scandir, normcase):
# Public API
#

class _PathParents(Sequence):
"""This object provides sequence-like access to the logical ancestors
of a path. Don't try to construct it yourself."""
__slots__ = ('_path', '_drv', '_root', '_tail')

def __init__(self, path):
self._path = path
self._drv = path.drive
self._root = path.root
self._tail = path._tail

def __len__(self):
return len(self._tail)

def __getitem__(self, idx):
if isinstance(idx, slice):
return tuple(self[i] for i in range(*idx.indices(len(self))))

if idx >= len(self) or idx < -len(self):
raise IndexError(idx)
if idx < 0:
idx += len(self)
return self._path._from_parsed_parts(self._drv, self._root,
self._tail[:-idx - 1])

def __repr__(self):
return "<{}.parents>".format(type(self._path).__name__)


class PurePath(object):
"""Base class for manipulating paths without I/O.

Expand Down Expand Up @@ -615,13 +645,8 @@ def parent(self):

@property
def parents(self):
"""A tuple of this path's logical parents."""
drv = self.drive
root = self.root
tail = self._tail
return tuple(
self._from_parsed_parts(drv, root, tail[:idx])
for idx in reversed(range(len(tail))))
"""A sequence of this path's logical parents."""
return _PathParents(self)

def is_absolute(self):
"""True if the path is absolute (has both a root and, if applicable,
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.