Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
GH-104996: Defer joining of pathlib.PurePath() arguments.#104999
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
barneygale
merged 4 commits into
python:main
from
barneygale:gh-104996-pathlib-defer-path-joiningJun 7, 2023
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -195,10 +195,10 @@ def _select_unique(paths): | ||
| yielded = set() | ||
| try: | ||
| for path in paths: | ||
| raw_path = path._raw_path | ||
| if raw_path not in yielded: | ||
| path_str = str(path) | ||
| if path_str not in yielded: | ||
| yield path | ||
| yielded.add(raw_path) | ||
| yielded.add(path_str) | ||
| finally: | ||
| yielded.clear() | ||
| @@ -247,9 +247,9 @@ class PurePath: | ||
| """ | ||
| __slots__ = ( | ||
| # The `_raw_path` slot stores an unnormalized string path. This is set | ||
| # The `_raw_paths` slot stores unnormalized string paths. This is set | ||
| # in the `__init__()` method. | ||
| '_raw_path', | ||
| '_raw_paths', | ||
| # The `_drv`, `_root` and `_tail_cached` slots store parsed and | ||
| # normalized parts of the path. They are set when any of the `drive`, | ||
| @@ -306,10 +306,11 @@ def __init__(self, *args): | ||
| paths = [] | ||
| for arg in args: | ||
| if isinstance(arg, PurePath): | ||
| path = arg._raw_path | ||
| if arg._flavour is ntpath and self._flavour is posixpath: | ||
| # GH-103631: Convert separators for backwards compatibility. | ||
| path = path.replace('\\', '/') | ||
| paths.extend(path.replace('\\', '/') for path in arg._raw_paths) | ||
| else: | ||
| paths.extend(arg._raw_paths) | ||
| else: | ||
| try: | ||
| path = os.fspath(arg) | ||
| @@ -320,13 +321,8 @@ def __init__(self, *args): | ||
| "argument should be a str or an os.PathLike " | ||
| "object where __fspath__ returns a str, " | ||
| f"not {type(path).__name__!r}") | ||
| paths.append(path) | ||
| if len(paths) == 0: | ||
| self._raw_path = '' | ||
| elif len(paths) == 1: | ||
| self._raw_path = paths[0] | ||
| else: | ||
| self._raw_path = self._flavour.join(*paths) | ||
| paths.append(path) | ||
| self._raw_paths = paths | ||
| def with_segments(self, *pathsegments): | ||
| """Construct a new path object from any number of path-like objects. | ||
| @@ -356,7 +352,14 @@ def _parse_path(cls, path): | ||
| return drv, root, parsed | ||
| def _load_parts(self): | ||
| drv, root, tail = self._parse_path(self._raw_path) | ||
| paths = self._raw_paths | ||
| if len(paths) == 0: | ||
| path = '' | ||
| elif len(paths) == 1: | ||
| path = paths[0] | ||
| else: | ||
| path = self._flavour.join(*paths) | ||
| drv, root, tail = self._parse_path(path) | ||
| self._drv = drv | ||
| self._root = root | ||
| self._tail_cached = tail | ||
| @@ -687,10 +690,17 @@ def parents(self): | ||
| def is_absolute(self): | ||
| """True if the path is absolute (has both a root and, if applicable, | ||
| a drive).""" | ||
| # ntpath.isabs() is defective - see GH-44626 . | ||
| if self._flavour is ntpath: | ||
| # ntpath.isabs() is defective - see GH-44626. | ||
| return bool(self.drive and self.root) | ||
barneygale marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return self._flavour.isabs(self._raw_path) | ||
| elif self._flavour is posixpath: | ||
| # Optimization: work with raw paths on POSIX. | ||
| for path in self._raw_paths: | ||
barneygale marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if path.startswith('/'): | ||
| return True | ||
| return False | ||
| else: | ||
| return self._flavour.isabs(str(self)) | ||
| def is_reserved(self): | ||
| """Return True if the path contains one of the special names reserved | ||
2 changes: 2 additions & 0 deletions
2 Misc/NEWS.d/next/Library/2023-05-26-21-24-06.gh-issue-104996.aaW78g.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Improve performance of :class:`pathlib.PurePath` initialisation by | ||
| deferring joining of paths when multiple arguments are given. |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.