Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
bpo-43757: make pathlib use os.path.realpath() to resolve all symlinks in a path#25264
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
23 commits Select commit Hold shift + click to select a range
164dbfd bpo-43757: make pathlib use os.path.realpath() to resolve all symlink…
barneygale 2894e0a Adjust `os.path.realpath` to match `pathlib.Path.resolve()` behaviour…
barneygale 495bd8b Call `os.stat()` to raise `OSError(errno.ELOOP, ...)`.
barneygale 492c66b Remove unused import
barneygale c8bac15 Stop ignoring ERROR_CANT_RESOLVE_FILENAME error in _getfinalpathname_…
barneygale fe69688 Fix symlink detection and `posixpath` tests.
barneygale 4114be9 Raise RuntimeError in Path.resolve() if realpath() raises ERROR_CANT_…
barneygale 2a594ce Remove unused import
barneygale fb36a40 Make documentation a little more general to accurately cover Windows.
barneygale 0634486 First pass on fixing `ntpath` tests
barneygale 600fc9e Second pass on fixing `ntpath` tests
barneygale 047471c Fix indentation
barneygale bf3a3f7 Copy description of 'strict' from pathlib.
barneygale f839db0 Add tests
barneygale 2d30bb7 Add NEWS
barneygale 68c6533 Fix ntpath tests (pass 1)
barneygale 9fa60eb Add some notes on OS differences
barneygale f979947 Split NEWS
barneygale a82bc18 Do not suppress initial ERROR_CANT_RESOLVE_FILENAME error in non-stri…
barneygale 86c318c realpath(): only raise OSError for symlink loops in strict mode
barneygale 7362f7e Fix test_ntpath (pass 1)
barneygale 72147c5 Fix test_ntpath (pass 2)
barneygale df04357 Split up exception handling in resolve() for greater clarity.
barneygale 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
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
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 |
|---|---|---|
| @@ -14,12 +14,6 @@ | ||
| from urllib.parse import quote_from_bytes as urlquote_from_bytes | ||
| if os.name == 'nt': | ||
| from nt import _getfinalpathname | ||
| else: | ||
| _getfinalpathname = None | ||
| __all__ = [ | ||
| "PurePath", "PurePosixPath", "PureWindowsPath", | ||
| "Path", "PosixPath", "WindowsPath", | ||
| @@ -29,14 +23,17 @@ | ||
| # Internals | ||
| # | ||
| _WINERROR_NOT_READY = 21 # drive exists but is not accessible | ||
| _WINERROR_INVALID_NAME = 123 # fix for bpo-35306 | ||
| _WINERROR_CANT_RESOLVE_FILENAME = 1921 # broken symlink pointing to itself | ||
| # EBADF - guard against macOS `stat` throwing EBADF | ||
| _IGNORED_ERROS = (ENOENT, ENOTDIR, EBADF, ELOOP) | ||
| _IGNORED_WINERRORS = ( | ||
| 21, # ERROR_NOT_READY - drive exists but is not accessible | ||
| 123, # ERROR_INVALID_NAME - fix for bpo-35306 | ||
| 1921, # ERROR_CANT_RESOLVE_FILENAME - fix for broken symlink pointing to itself | ||
| ) | ||
| _WINERROR_NOT_READY, | ||
| _WINERROR_INVALID_NAME, | ||
| _WINERROR_CANT_RESOLVE_FILENAME) | ||
| def _ignore_error(exception): | ||
| return (getattr(exception, 'errno', None) in _IGNORED_ERROS or | ||
| @@ -186,30 +183,6 @@ def casefold_parts(self, parts): | ||
| def compile_pattern(self, pattern): | ||
| return re.compile(fnmatch.translate(pattern), re.IGNORECASE).fullmatch | ||
| def resolve(self, path, strict=False): | ||
| s = str(path) | ||
| if not s: | ||
| return path._accessor.getcwd() | ||
| previous_s = None | ||
| if _getfinalpathname is not None: | ||
| if strict: | ||
| return self._ext_to_normal(_getfinalpathname(s)) | ||
| else: | ||
| tail_parts = [] # End of the path after the first one not found | ||
| while True: | ||
| try: | ||
| s = self._ext_to_normal(_getfinalpathname(s)) | ||
| except FileNotFoundError: | ||
| previous_s = s | ||
| s, tail = os.path.split(s) | ||
| tail_parts.append(tail) | ||
| if previous_s == s: | ||
| return path | ||
| else: | ||
| return os.path.join(s, *reversed(tail_parts)) | ||
| # Means fallback on absolute | ||
| return None | ||
| def _split_extended_path(self, s, ext_prefix=ext_namespace_prefix): | ||
| prefix = '' | ||
| if s.startswith(ext_prefix): | ||
| @@ -220,10 +193,6 @@ def _split_extended_path(self, s, ext_prefix=ext_namespace_prefix): | ||
| s = '\\' + s[3:] | ||
| return prefix, s | ||
| def _ext_to_normal(self, s): | ||
| # Turn back an extended path into a normal DOS-like path | ||
| return self._split_extended_path(s)[1] | ||
| def is_reserved(self, parts): | ||
| # NOTE: the rules for reserved names seem somewhat complicated | ||
| # (e.g. r"..\NUL" is reserved but not r"foo\NUL"). | ||
| @@ -281,54 +250,6 @@ def casefold_parts(self, parts): | ||
| def compile_pattern(self, pattern): | ||
| return re.compile(fnmatch.translate(pattern)).fullmatch | ||
| def resolve(self, path, strict=False): | ||
| sep = self.sep | ||
| accessor = path._accessor | ||
| seen ={} | ||
| def _resolve(path, rest): | ||
| if rest.startswith(sep): | ||
| path = '' | ||
| for name in rest.split(sep): | ||
| if not name or name == '.': | ||
| # current dir | ||
| continue | ||
| if name == '..': | ||
| # parent dir | ||
| path, _, _ = path.rpartition(sep) | ||
| continue | ||
| if path.endswith(sep): | ||
| newpath = path + name | ||
| else: | ||
| newpath = path + sep + name | ||
| if newpath in seen: | ||
| # Already seen this path | ||
| path = seen[newpath] | ||
| if path is not None: | ||
| # use cached value | ||
| continue | ||
| # The symlink is not resolved, so we must have a symlink loop. | ||
| raise RuntimeError("Symlink loop from %r" % newpath) | ||
| # Resolve the symbolic link | ||
| try: | ||
| target = accessor.readlink(newpath) | ||
| except OSError as e: | ||
| if e.errno != EINVAL and strict: | ||
| raise | ||
| # Not a symlink, or non-strict mode. We just leave the path | ||
| # untouched. | ||
| path = newpath | ||
| else: | ||
| seen[newpath] = None # not resolved symlink | ||
| path = _resolve(path, target) | ||
| seen[newpath] = path # resolved symlink | ||
| return path | ||
| # NOTE: according to POSIX, getcwd() cannot contain path components | ||
| # which are symlinks. | ||
| base = '' if path.is_absolute() else accessor.getcwd() | ||
| return _resolve(base, str(path)) or sep | ||
| def is_reserved(self, parts): | ||
| return False | ||
| @@ -424,6 +345,8 @@ def group(self, path): | ||
| expanduser = staticmethod(os.path.expanduser) | ||
| realpath = staticmethod(os.path.realpath) | ||
barneygale marked this conversation as resolved. Outdated Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| _normal_accessor = _NormalAccessor() | ||
| @@ -1132,15 +1055,27 @@ def resolve(self, strict=False): | ||
| normalizing it (for example turning slashes into backslashes under | ||
| Windows). | ||
| """ | ||
| s = self._flavour.resolve(self, strict=strict) | ||
| if s is None: | ||
| # No symlink resolution => for consistency, raise an error if | ||
| # the path doesn't exist or is forbidden | ||
| self.stat() | ||
| s = str(self.absolute()) | ||
| # Now we have no symlinks in the path, it's safe to normalize it. | ||
| normed = self._flavour.pathmod.normpath(s) | ||
| return self._from_parts((normed,)) | ||
| def check_eloop(e): | ||
| winerror = getattr(e, 'winerror', 0) | ||
| if e.errno == ELOOP or winerror == _WINERROR_CANT_RESOLVE_FILENAME: | ||
| raise RuntimeError("Symlink loop from %r" % e.filename) | ||
| try: | ||
| s = self._accessor.realpath(self, strict=strict) | ||
| except OSError as e: | ||
| check_eloop(e) | ||
| raise | ||
| p = self._from_parts((s,)) | ||
| # In non-strict mode, realpath() doesn't raise on symlink loops. | ||
| # Ensure we get an exception by calling stat() | ||
| if not strict: | ||
| try: | ||
| p.stat() | ||
| except OSError as e: | ||
| check_eloop(e) | ||
| return p | ||
| def stat(self, *, follow_symlinks=True): | ||
| """ | ||
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
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
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.