diff options
author | Steve Dower <steve.dower@microsoft.com> | 2016-11-09 12:58:31 -0800 |
---|---|---|
committer | Steve Dower <steve.dower@microsoft.com> | 2016-11-09 12:58:31 -0800 |
commit | bb132fc34ef2aeaf4cbe9a37b3f70833e088c6a0 (patch) | |
tree | 219c1d9f5f7786c17bfd1bc83cad69a2e8caa1f0 /Lib/pathlib.py | |
parent | 02f252f3af496f297dc8fc1f35a0634a686612a2 (diff) | |
parent | 98eb360531e5e3045b21b34afd32474cf8741c9b (diff) | |
download | cpython-git-bb132fc34ef2aeaf4cbe9a37b3f70833e088c6a0.tar.gz |
Issue #19717: Makes Path.resolve() succeed on paths that do not exist (patch by Vajrasky Kok)
Diffstat (limited to 'Lib/pathlib.py')
-rw-r--r-- | Lib/pathlib.py | 29 |
1 files changed, 23 insertions, 6 deletions
diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 1b5ab387a6..69653938ef 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -178,12 +178,26 @@ class _WindowsFlavour(_Flavour): def casefold_parts(self, parts): return [p.lower() for p in parts] - def resolve(self, path): + def resolve(self, path, strict=False): s = str(path) if not s: return os.getcwd() + previous_s = None if _getfinalpathname is not None: - return self._ext_to_normal(_getfinalpathname(s)) + if strict: + return self._ext_to_normal(_getfinalpathname(s)) + else: + while True: + try: + s = self._ext_to_normal(_getfinalpathname(s)) + except FileNotFoundError: + previous_s = s + s = os.path.abspath(os.path.join(s, os.pardir)) + else: + if previous_s is None: + return s + else: + return s + os.path.sep + os.path.basename(previous_s) # Means fallback on absolute return None @@ -285,7 +299,7 @@ class _PosixFlavour(_Flavour): def casefold_parts(self, parts): return parts - def resolve(self, path): + def resolve(self, path, strict=False): sep = self.sep accessor = path._accessor seen = {} @@ -315,7 +329,10 @@ class _PosixFlavour(_Flavour): target = accessor.readlink(newpath) except OSError as e: if e.errno != EINVAL: - raise + if strict: + raise + else: + return newpath # Not a symlink path = newpath else: @@ -1092,7 +1109,7 @@ class Path(PurePath): obj._init(template=self) return obj - def resolve(self): + def resolve(self, strict=False): """ Make the path absolute, resolving all symlinks on the way and also normalizing it (for example turning slashes into backslashes under @@ -1100,7 +1117,7 @@ class Path(PurePath): """ if self._closed: self._raise_closed() - s = self._flavour.resolve(self) + 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 |