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
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
Look up __newpath__ on path type.
  • Loading branch information
barneygale committed Apr 29, 2023
commit cdedc926aa4ad6e7031a4520047ae22a99a83a64
29 changes: 15 additions & 14 deletions 29 Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ def _load_parts(self):

def _from_parsed_parts(self, drv, root, tail):
path_str = self._format_parsed_parts(drv, root, tail)
path = self.__newpath__(path_str)
path = type(self).__newpath__(self, path_str)
path._str = path_str or '.'
path._drv = drv
path._root = root
Expand Down Expand Up @@ -581,7 +581,8 @@ def relative_to(self, other, /, *_deprecated, walk_up=False):
"scheduled for removal in Python {remove}")
warnings._deprecated("pathlib.PurePath.relative_to(*args)", msg,
remove=(3, 14))
other = self.__newpath__(other, *_deprecated)
path_cls = type(self)
other = path_cls.__newpath__(self, other, *_deprecated)
for step, path in enumerate([other] + list(other.parents)):
if self.is_relative_to(path):
break
Expand All @@ -590,7 +591,7 @@ def relative_to(self, other, /, *_deprecated, walk_up=False):
if step and not walk_up:
raise ValueError(f"{str(self)!r} is not in the subpath of {str(other)!r}")
parts = ['..'] * step + self._tail[len(path._tail):]
return self.__newpath__(*parts)
return path_cls.__newpath__(self, *parts)

def is_relative_to(self, other, /, *_deprecated):
"""Return True if the path is relative to another path or False.
Expand All @@ -601,7 +602,7 @@ def is_relative_to(self, other, /, *_deprecated):
"scheduled for removal in Python {remove}")
warnings._deprecated("pathlib.PurePath.is_relative_to(*args)",
msg, remove=(3, 14))
other = self.__newpath__(other, *_deprecated)
other = type(self).__newpath__(self, other, *_deprecated)
return other == self or other in self.parents

@property
Expand All @@ -619,7 +620,7 @@ def joinpath(self, *pathsegments):
paths) or a totally different path (if one of the arguments is
anchored).
"""
return self.__newpath__(self._raw_path, *pathsegments)
return type(self).__newpath__(self, self._raw_path, *pathsegments)

def __truediv__(self, key):
try:
Expand All @@ -629,7 +630,7 @@ def __truediv__(self, key):

def __rtruediv__(self, key):
try:
return self.__newpath__(key, self._raw_path)
return type(self).__newpath__(self, key, self._raw_path)
except TypeError:
return NotImplemented

Expand Down Expand Up @@ -678,7 +679,7 @@ def match(self, path_pattern):
"""
Return True if this path matches the given pattern.
"""
pat = self.__newpath__(path_pattern)
pat = type(self).__newpath__(self, path_pattern)
if not pat.parts:
raise ValueError("empty pattern")
pat_parts = pat._parts_normcase
Expand Down Expand Up @@ -753,7 +754,7 @@ def _make_child_relpath(self, name):
path_str = f'{path_str}{name}'
else:
path_str = name
path = self.__newpath__(path_str)
path = type(self).__newpath__(self, path_str)
path._str = path_str
path._drv = self.drive
path._root = self.root
Expand Down Expand Up @@ -803,7 +804,7 @@ def samefile(self, other_path):
try:
other_st = other_path.stat()
except AttributeError:
other_st = self.__newpath__(other_path).stat()
other_st = type(self).__newpath__(self, other_path).stat()
return self._flavour.samestat(st, other_st)

def iterdir(self):
Expand Down Expand Up @@ -865,7 +866,7 @@ def absolute(self):
cwd = self._flavour.abspath(self.drive)
else:
cwd = os.getcwd()
return self.__newpath__(cwd, self._raw_path)
return type(self).__newpath__(self, cwd, self._raw_path)

def resolve(self, strict=False):
"""
Expand All @@ -883,7 +884,7 @@ def check_eloop(e):
except OSError as e:
check_eloop(e)
raise
p = self.__newpath__(s)
p = type(self).__newpath__(self, s)

# In non-strict mode, realpath() doesn't raise on symlink loops.
# Ensure we get an exception by calling stat()
Expand Down Expand Up @@ -973,7 +974,7 @@ def readlink(self):
"""
if not hasattr(os, "readlink"):
raise NotImplementedError("os.readlink() not available on this system")
return self.__newpath__(os.readlink(self))
return type(self).__newpath__(self, os.readlink(self))

def touch(self, mode=0o666, exist_ok=True):
"""
Expand Down Expand Up @@ -1062,7 +1063,7 @@ def rename(self, target):
Returns the new Path instance pointing to the target path.
"""
os.rename(self, target)
return self.__newpath__(target)
return type(self).__newpath__(self, target)

def replace(self, target):
"""
Expand All @@ -1075,7 +1076,7 @@ def replace(self, target):
Returns the new Path instance pointing to the target path.
"""
os.replace(self, target)
return self.__newpath__(target)
return type(self).__newpath__(self, target)

def symlink_to(self, target, target_is_directory=False):
"""
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.