From f329e78b9911f59c194dad5d76b531365bfac4eb Mon Sep 17 00:00:00 2001 From: barneygale Date: Fri, 29 Dec 2023 01:00:48 +0000 Subject: [PATCH 1/8] GH-113528: Deoptimise `pathlib._abc.PurePathBase.__str__()` Move `_str` cache slot from `PurePathBase` to `PurePath`. As a result, pathlib ABCs no longer cache their string representations. --- Lib/pathlib/__init__.py | 24 ++++++++++++++++++++++++ Lib/pathlib/_abc.py | 21 +-------------------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/Lib/pathlib/__init__.py b/Lib/pathlib/__init__.py index 2b4193c400a099..d24d6a688f5fcc 100644 --- a/Lib/pathlib/__init__.py +++ b/Lib/pathlib/__init__.py @@ -43,6 +43,11 @@ class PurePath(_abc.PurePathBase): """ __slots__ = ( + # The `_str` slot stores the string representation of the path, + # computed from the drive, root and tail when `__str__()` is called + # for the first time. It's used to implement `_str_normcase` + '_str', + # The `_str_normcase_cached` slot stores the string path with # normalized case. It is set when the `_str_normcase` property is # accessed for the first time. It's used to implement `__eq__()` @@ -100,6 +105,25 @@ def __reduce__(self): # when pickling related paths. return (self.__class__, self.parts) + def _from_parsed_parts(self, drv, root, tail): + path_str = self._format_parsed_parts(drv, root, tail) + path = self.with_segments(path_str) + path._str = path_str or '.' + path._drv = drv + path._root = root + path._tail_cached = tail + return path + + def __str__(self): + """Return the string representation of the path, suitable for + passing to system calls.""" + try: + return self._str + except AttributeError: + self._str = self._format_parsed_parts(self.drive, self.root, + self._tail) or '.' + return self._str + def __repr__(self): return "{}({!r})".format(self.__class__.__name__, self.as_posix()) diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py index f75b20a1d5f1e5..1fccd0bd518cc2 100644 --- a/Lib/pathlib/_abc.py +++ b/Lib/pathlib/_abc.py @@ -191,11 +191,6 @@ class PurePathBase: # tail are normalized. '_drv', '_root', '_tail_cached', - # The `_str` slot stores the string representation of the path, - # computed from the drive, root and tail when `__str__()` is called - # for the first time. It's used to implement `_str_normcase` - '_str', - # The '_resolving' slot stores a boolean indicating whether the path # is being processed by `PathBase.resolve()`. This prevents duplicate # work from occurring when `resolve()` calls `stat()` or `readlink()`. @@ -247,15 +242,6 @@ def _load_parts(self): self._root = root self._tail_cached = tail - def _from_parsed_parts(self, drv, root, tail): - path_str = self._format_parsed_parts(drv, root, tail) - path = self.with_segments(path_str) - path._str = path_str or '.' - path._drv = drv - path._root = root - path._tail_cached = tail - return path - @classmethod def _format_parsed_parts(cls, drv, root, tail): if drv or root: @@ -267,12 +253,7 @@ def _format_parsed_parts(cls, drv, root, tail): def __str__(self): """Return the string representation of the path, suitable for passing to system calls.""" - try: - return self._str - except AttributeError: - self._str = self._format_parsed_parts(self.drive, self.root, - self._tail) or '.' - return self._str + return self._format_parsed_parts(self.drive, self.root, self._tail) or '.' def as_posix(self): """Return the string representation of the path with forward (/) From e58c99f7bbf1636521f544fc7e073239ae34c36f Mon Sep 17 00:00:00 2001 From: barneygale Date: Fri, 29 Dec 2023 01:00:48 +0000 Subject: [PATCH 2/8] Stop caching `_drv`, `_root`, `_tail_cached`, and stop normalizing paths. --- Lib/pathlib/__init__.py | 120 +++++++++++++++---- Lib/pathlib/_abc.py | 135 ++++++---------------- Lib/test/test_pathlib/test_pathlib.py | 130 +++++++++++++++++++++ Lib/test/test_pathlib/test_pathlib_abc.py | 128 ++++---------------- 4 files changed, 291 insertions(+), 222 deletions(-) diff --git a/Lib/pathlib/__init__.py b/Lib/pathlib/__init__.py index fc4c4021f8ed6d..784d4beaefbed5 100644 --- a/Lib/pathlib/__init__.py +++ b/Lib/pathlib/__init__.py @@ -75,6 +75,15 @@ class PurePath(_abc.PurePathBase): """ __slots__ = ( + # The `_drv`, `_root` and `_tail_cached` slots store parsed and + # normalized parts of the path. They are set when any of the `drive`, + # `root` or `_tail` properties are accessed for the first time. The + # three-part division corresponds to the result of + # `os.path.splitroot()`, except that the tail is further split on path + # separators (i.e. it is a list of strings), and that the root and + # tail are normalized. + '_drv', '_root', '_tail_cached', + # The `_str` slot stores the string representation of the path, # computed from the drive, root and tail when `__str__()` is called # for the first time. It's used to implement `_str_normcase` @@ -136,25 +145,6 @@ def __reduce__(self): # when pickling related paths. return (self.__class__, self.parts) - def _from_parsed_parts(self, drv, root, tail): - path_str = self._format_parsed_parts(drv, root, tail) - path = self.with_segments(path_str) - path._str = path_str or '.' - path._drv = drv - path._root = root - path._tail_cached = tail - return path - - def __str__(self): - """Return the string representation of the path, suitable for - passing to system calls.""" - try: - return self._str - except AttributeError: - self._str = self._format_parsed_parts(self.drive, self.root, - self._tail) or '.' - return self._str - def __repr__(self): return "{}({!r})".format(self.__class__.__name__, self.as_posix()) @@ -219,6 +209,96 @@ def __ge__(self, other): return NotImplemented return self._parts_normcase >= other._parts_normcase + def __str__(self): + """Return the string representation of the path, suitable for + passing to system calls.""" + try: + return self._str + except AttributeError: + self._str = self._format_parsed_parts(self.drive, self.root, + self._tail) or '.' + return self._str + + @classmethod + def _format_parsed_parts(cls, drv, root, tail): + if drv or root: + return drv + root + cls.pathmod.sep.join(tail) + elif tail and cls.pathmod.splitdrive(tail[0])[0]: + tail = ['.'] + tail + return cls.pathmod.sep.join(tail) + + def _from_parsed_parts(self, drv, root, tail): + path_str = self._format_parsed_parts(drv, root, tail) + path = self.with_segments(path_str) + path._str = path_str or '.' + path._drv = drv + path._root = root + path._tail_cached = tail + return path + + @classmethod + def _parse_path(cls, path): + if not path: + return '', '', [] + sep = cls.pathmod.sep + altsep = cls.pathmod.altsep + if altsep: + path = path.replace(altsep, sep) + drv, root, rel = cls.pathmod.splitroot(path) + if not root and drv.startswith(sep) and not drv.endswith(sep): + drv_parts = drv.split(sep) + if len(drv_parts) == 4 and drv_parts[2] not in '?.': + # e.g. //server/share + root = sep + elif len(drv_parts) == 6: + # e.g. //?/unc/server/share + root = sep + parsed = [sys.intern(str(x)) for x in rel.split(sep) if x and x != '.'] + return drv, root, parsed + + @property + def drive(self): + """The drive prefix (letter or UNC path), if any.""" + try: + return self._drv + except AttributeError: + path = _abc.PurePathBase.__str__(self) + self._drv, self._root, self._tail_cached = self._parse_path(path) + return self._drv + + @property + def root(self): + """The root of the path, if any.""" + try: + return self._root + except AttributeError: + path = _abc.PurePathBase.__str__(self) + self._drv, self._root, self._tail_cached = self._parse_path(path) + return self._root + + @property + def _tail(self): + try: + return self._tail_cached + except AttributeError: + path = _abc.PurePathBase.__str__(self) + self._drv, self._root, self._tail_cached = self._parse_path(path) + return self._tail_cached + + @property + def anchor(self): + """The concatenation of the drive and root, or ''.""" + return self.drive + self.root + + @property + def parts(self): + """An object providing sequence-like access to the + components in the filesystem path.""" + if self.drive or self.root: + return (self.drive + self.root,) + tuple(self._tail) + else: + return tuple(self._tail) + @property def parent(self): """The logical parent of the path.""" @@ -419,7 +499,7 @@ def iterdir(self): def _scandir(self): return os.scandir(self) - def _make_child_entry(self, entry): + def _make_child_entry(self, entry, is_dir=False): # Transform an entry yielded from _scandir() into a path object. path_str = entry.name if str(self) == '.' else entry.path path = self.with_segments(path_str) diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py index 28fbca7ba3edb3..3ea2f16ef1d2c2 100644 --- a/Lib/pathlib/_abc.py +++ b/Lib/pathlib/_abc.py @@ -1,7 +1,6 @@ import functools import ntpath import posixpath -import sys from errno import ENOENT, ENOTDIR, EBADF, ELOOP, EINVAL from itertools import chain from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO @@ -86,7 +85,7 @@ def _select_children(parent_paths, dir_only, follow_symlinks, match): except OSError: continue if match(entry.name): - yield parent_path._make_child_entry(entry) + yield parent_path._make_child_entry(entry, dir_only) def _select_recursive(parent_paths, dir_only, follow_symlinks): @@ -109,7 +108,7 @@ def _select_recursive(parent_paths, dir_only, follow_symlinks): for entry in entries: try: if entry.is_dir(follow_symlinks=follow_symlinks): - paths.append(path._make_child_entry(entry)) + paths.append(path._make_child_entry(entry, dir_only)) continue except OSError: pass @@ -151,15 +150,6 @@ class PurePathBase: # in the `__init__()` method. '_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`, - # `root` or `_tail` properties are accessed for the first time. The - # three-part division corresponds to the result of - # `os.path.splitroot()`, except that the tail is further split on path - # separators (i.e. it is a list of strings), and that the root and - # tail are normalized. - '_drv', '_root', '_tail_cached', - # The '_resolving' slot stores a boolean indicating whether the path # is being processed by `PathBase.resolve()`. This prevents duplicate # work from occurring when `resolve()` calls `stat()` or `readlink()`. @@ -178,51 +168,16 @@ def with_segments(self, *pathsegments): """ return type(self)(*pathsegments) - @classmethod - def _parse_path(cls, path): - if not path: - return '', '', [] - sep = cls.pathmod.sep - altsep = cls.pathmod.altsep - if altsep: - path = path.replace(altsep, sep) - drv, root, rel = cls.pathmod.splitroot(path) - if not root and drv.startswith(sep) and not drv.endswith(sep): - drv_parts = drv.split(sep) - if len(drv_parts) == 4 and drv_parts[2] not in '?.': - # e.g. //server/share - root = sep - elif len(drv_parts) == 6: - # e.g. //?/unc/server/share - root = sep - parsed = [sys.intern(str(x)) for x in rel.split(sep) if x and x != '.'] - return drv, root, parsed - - def _load_parts(self): - paths = self._raw_paths - if len(paths) == 0: - path = '' - elif len(paths) == 1: - path = paths[0] - else: - path = self.pathmod.join(*paths) - drv, root, tail = self._parse_path(path) - self._drv = drv - self._root = root - self._tail_cached = tail - - @classmethod - def _format_parsed_parts(cls, drv, root, tail): - if drv or root: - return drv + root + cls.pathmod.sep.join(tail) - elif tail and cls.pathmod.splitdrive(tail[0])[0]: - tail = ['.'] + tail - return cls.pathmod.sep.join(tail) - def __str__(self): """Return the string representation of the path, suitable for passing to system calls.""" - return self._format_parsed_parts(self.drive, self.root, self._tail) or '.' + paths = self._raw_paths + if len(paths) == 1: + return paths[0] + elif paths: + return self.pathmod.join(*paths) + else: + return '' def as_posix(self): """Return the string representation of the path with forward (/) @@ -232,42 +187,23 @@ def as_posix(self): @property def drive(self): """The drive prefix (letter or UNC path), if any.""" - try: - return self._drv - except AttributeError: - self._load_parts() - return self._drv + return self.pathmod.splitdrive(str(self))[0] @property def root(self): """The root of the path, if any.""" - try: - return self._root - except AttributeError: - self._load_parts() - return self._root - - @property - def _tail(self): - try: - return self._tail_cached - except AttributeError: - self._load_parts() - return self._tail_cached + return self.pathmod.splitroot(str(self))[1] @property def anchor(self): """The concatenation of the drive and root, or ''.""" - anchor = self.drive + self.root - return anchor + drive, root, _ = self.pathmod.splitroot(str(self)) + return drive + root @property def name(self): """The final path component, if any.""" - path_str = str(self) - if not path_str or path_str == '.': - return '' - return self.pathmod.basename(path_str) + return self.pathmod.basename(str(self)) @property def suffix(self): @@ -309,12 +245,9 @@ def stem(self): def with_name(self, name): """Return a new path with the file name changed.""" m = self.pathmod - if not name or m.sep in name or (m.altsep and m.altsep in name) or name == '.': + if m.sep in name or (m.altsep and m.altsep in name): raise ValueError(f"Invalid name {name!r}") - parent, old_name = m.split(str(self)) - if not old_name or old_name == '.': - raise ValueError(f"{self!r} has an empty name") - return self.with_segments(parent, name) + return self.with_segments(m.dirname(str(self)), name) def with_stem(self, stem): """Return a new path with the stem changed.""" @@ -351,8 +284,7 @@ def relative_to(self, other, *, walk_up=False): raise ValueError(f"'..' segment in {str(other)!r} cannot be walked") else: raise ValueError(f"{str(self)!r} and {str(other)!r} have different anchors") - parts = ['..'] * step + self._tail[len(path._tail):] - return self.with_segments(*parts) + return self.with_segments(*['..'] * step, *self.parts[len(path.parts):]) def is_relative_to(self, other): """Return True if the path is relative to another path or False. @@ -365,10 +297,17 @@ def is_relative_to(self, other): def parts(self): """An object providing sequence-like access to the components in the filesystem path.""" - if self.drive or self.root: - return (self.drive + self.root,) + tuple(self._tail) + m = self.pathmod + drive, root, rel = m.splitroot(str(self)) + if rel: + if m.altsep: + rel = rel.replace(m.altsep, m.sep) + tail = tuple(rel.split(m.sep)) else: - return tuple(self._tail) + tail = tuple() + if drive or root: + tail = (drive + root,) + tail + return tail def joinpath(self, *pathsegments): """Combine this path with one or several arguments, and return a @@ -432,7 +371,7 @@ def is_absolute(self): def is_reserved(self): """Return True if the path contains one of the special names reserved by the system, if any.""" - if self.pathmod is posixpath or not self._tail: + if self.pathmod is posixpath or not self.name: return False # NOTE: the rules for reserved names seem somewhat complicated @@ -442,7 +381,7 @@ def is_reserved(self): if self.drive.startswith('\\\\'): # UNC paths are never reserved. return False - name = self._tail[-1].partition('.')[0].partition(':')[0].rstrip(' ') + name = self.name.partition('.')[0].partition(':')[0].rstrip(' ') return name.upper() in _WIN_RESERVED_NAMES def match(self, path_pattern, *, case_sensitive=None): @@ -455,9 +394,9 @@ def match(self, path_pattern, *, case_sensitive=None): case_sensitive = _is_case_sensitive(self.pathmod) sep = path_pattern.pathmod.sep pattern_str = str(path_pattern) - if path_pattern.drive or path_pattern.root: + if path_pattern.anchor: pass - elif path_pattern._tail: + elif path_pattern.parts: pattern_str = f'**{sep}{pattern_str}' else: raise ValueError("empty pattern") @@ -729,8 +668,10 @@ def _scandir(self): from contextlib import nullcontext return nullcontext(self.iterdir()) - def _make_child_entry(self, entry): + def _make_child_entry(self, entry, is_dir=False): # Transform an entry yielded from _scandir() into a path object. + if is_dir: + return entry.joinpath('') return entry def _make_child_relpath(self, name): @@ -754,12 +695,12 @@ def glob(self, pattern, *, case_sensitive=None, follow_symlinks=None): kind, including directories) matching the given relative pattern. """ path_pattern = self.with_segments(pattern) - if path_pattern.drive or path_pattern.root: + if path_pattern.anchor: raise NotImplementedError("Non-relative patterns are unsupported") - elif not path_pattern._tail: + elif not path_pattern.parts: raise ValueError("Unacceptable pattern: {!r}".format(pattern)) - pattern_parts = path_pattern._tail.copy() + pattern_parts = list(path_pattern.parts) if pattern[-1] in (self.pathmod.sep, self.pathmod.altsep): # GH-65238: pathlib doesn't preserve trailing slash. Add it back. pattern_parts.append('') @@ -778,7 +719,7 @@ def glob(self, pattern, *, case_sensitive=None, follow_symlinks=None): filter_paths = follow_symlinks is not None and '..' not in pattern_parts deduplicate_paths = False sep = self.pathmod.sep - paths = iter([self] if self.is_dir() else []) + paths = iter([self.joinpath('')] if self.is_dir() else []) part_idx = 0 while part_idx < len(pattern_parts): part = pattern_parts[part_idx] diff --git a/Lib/test/test_pathlib/test_pathlib.py b/Lib/test/test_pathlib/test_pathlib.py index 93fe327a0d3c23..42935d6f9e80ed 100644 --- a/Lib/test/test_pathlib/test_pathlib.py +++ b/Lib/test/test_pathlib/test_pathlib.py @@ -45,6 +45,22 @@ class PurePathTest(test_pathlib_abc.DummyPurePathTest): # Make sure any symbolic links in the base test path are resolved. base = os.path.realpath(TESTFN) + # Keys are canonical paths, values are list of tuples of arguments + # supposed to produce equal paths. + equivalences = { + 'a/b': [ + ('a', 'b'), ('a/', 'b'), ('a', 'b/'), ('a/', 'b/'), + ('a/b/',), ('a//b',), ('a//b//',), + # Empty components get removed. + ('', 'a', 'b'), ('a', '', 'b'), ('a', 'b', ''), + ], + '/b/c/d': [ + ('a', '/b/c', 'd'), ('/a', '/b/c', 'd'), + # Empty components get removed. + ('/', 'b', '', 'c/d'), ('/', '', 'b/c/d'), ('', '/b/c/d'), + ], + } + def test_concrete_class(self): if self.cls is pathlib.PurePath: expected = pathlib.PureWindowsPath if os.name == 'nt' else pathlib.PurePosixPath @@ -95,6 +111,47 @@ def test_constructor_nested(self): self.assertEqual(P(P('a'), P('b'), P('c')), P(FakePath("a/b/c"))) self.assertEqual(P(P('./a:b')), P('./a:b')) + def _check_parse_path(self, raw_path, *expected): + sep = self.pathmod.sep + actual = self.cls._parse_path(raw_path.replace('/', sep)) + self.assertEqual(actual, expected) + if altsep := self.pathmod.altsep: + actual = self.cls._parse_path(raw_path.replace('/', altsep)) + self.assertEqual(actual, expected) + + def test_parse_path_common(self): + check = self._check_parse_path + sep = self.pathmod.sep + check('', '', '', []) + check('a', '', '', ['a']) + check('a/', '', '', ['a']) + check('a/b', '', '', ['a', 'b']) + check('a/b/', '', '', ['a', 'b']) + check('a/b/c/d', '', '', ['a', 'b', 'c', 'd']) + check('a/b//c/d', '', '', ['a', 'b', 'c', 'd']) + check('a/b/c/d', '', '', ['a', 'b', 'c', 'd']) + check('.', '', '', []) + check('././b', '', '', ['b']) + check('a/./b', '', '', ['a', 'b']) + check('a/./.', '', '', ['a']) + check('/a/b', '', sep, ['a', 'b']) + + def test_empty_path(self): + # The empty path points to '.' + p = self.cls('') + self.assertEqual(str(p), '.') + # Special case for the empty path. + self._check_str('.', ('',)) + + def test_parts_interning(self): + P = self.cls + p = P('/usr/bin/foo') + q = P('/usr/local/bin') + # 'usr' + self.assertIs(p.parts[1], q.parts[1]) + # 'bin' + self.assertIs(p.parts[2], q.parts[3]) + def test_join_nested(self): P = self.cls p = P('a/b').joinpath(P('c')) @@ -168,6 +225,24 @@ def test_as_bytes_common(self): P = self.cls self.assertEqual(bytes(P('a/b')), b'a' + sep + b'b') + def test_equivalences(self): + for k, tuples in self.equivalences.items(): + canon = k.replace('/', self.sep) + posix = k.replace(self.sep, '/') + if canon != posix: + tuples = tuples + [ + tuple(part.replace('/', self.sep) for part in t) + for t in tuples + ] + tuples.append((posix, )) + pcanon = self.cls(canon) + for t in tuples: + p = self.cls(*t) + self.assertEqual(p, pcanon, "failed with args {}".format(t)) + self.assertEqual(hash(p), hash(pcanon)) + self.assertEqual(str(p), canon) + self.assertEqual(p.as_posix(), posix) + def test_ordering_common(self): # Ordering is tuple-alike. def assertLess(a, b): @@ -214,6 +289,51 @@ def test_repr_roundtrips(self): self.assertEqual(q, p) self.assertEqual(repr(q), r) + def test_name_empty(self): + P = self.cls + self.assertEqual(P().name, '') + self.assertEqual(P('').name, '') + self.assertEqual(P('.').name, '') + self.assertEqual(P('/a/b/.').name, 'b') + + def test_stem_empty(self): + P = self.cls + self.assertEqual(P().stem, '') + self.assertEqual(P('').stem, '') + self.assertEqual(P('.').stem, '') + + def test_with_name_empty(self): + P = self.cls + self.assertRaises(ValueError, P('').with_name, 'd.xml') + self.assertRaises(ValueError, P('.').with_name, 'd.xml') + self.assertRaises(ValueError, P('/').with_name, 'd.xml') + self.assertRaises(ValueError, P('a/b').with_name, '') + self.assertRaises(ValueError, P('a/b').with_name, '.') + + def test_with_stem_empty(self): + P = self.cls + self.assertRaises(ValueError, P('').with_stem, 'd') + self.assertRaises(ValueError, P('.').with_stem, 'd') + self.assertRaises(ValueError, P('/').with_stem, 'd') + self.assertRaises(ValueError, P('a/b').with_stem, '') + self.assertRaises(ValueError, P('a/b').with_stem, '.') + + def test_with_suffix_empty(self): + # Path doesn't have a "filename" component. + P = self.cls + self.assertRaises(ValueError, P('').with_suffix, '.gz') + self.assertRaises(ValueError, P('.').with_suffix, '.gz') + self.assertRaises(ValueError, P('/').with_suffix, '.gz') + + def test_relative_to_trailing_sep(self): + P = self.cls + p = P('a/b') + self.assertEqual(p.relative_to('a/'), P('b')) + self.assertEqual(p.relative_to('a/', walk_up=True), P('b')) + p = P('/a/b') + self.assertEqual(p.relative_to('/a/'), P('b')) + self.assertEqual(p.relative_to('/a/', walk_up=True), P('b')) + def test_relative_to_several_args(self): P = self.cls p = P('a/b') @@ -221,12 +341,22 @@ def test_relative_to_several_args(self): p.relative_to('a', 'b') p.relative_to('a', 'b', walk_up=True) + def test_is_relative_to_trailing_sep(self): + P = self.cls + self.assertTrue(P('a/b').is_relative_to('a/')) + self.assertTrue(P('/a/b').is_relative_to('/a/')) + def test_is_relative_to_several_args(self): P = self.cls p = P('a/b') with self.assertWarns(DeprecationWarning): p.is_relative_to('a', 'b') + def test_match_empty(self): + P = self.cls + self.assertRaises(ValueError, P('a').match, '') + self.assertRaises(ValueError, P('a').match, '.') + class PurePosixPathTest(PurePathTest): cls = pathlib.PurePosixPath diff --git a/Lib/test/test_pathlib/test_pathlib_abc.py b/Lib/test/test_pathlib/test_pathlib_abc.py index 613b2d06b94a5d..cdc441126d430a 100644 --- a/Lib/test/test_pathlib/test_pathlib_abc.py +++ b/Lib/test/test_pathlib/test_pathlib_abc.py @@ -61,22 +61,6 @@ class DummyPurePathTest(unittest.TestCase): # Use a base path that's unrelated to any real filesystem path. base = f'/this/path/kills/fascists/{TESTFN}' - # Keys are canonical paths, values are list of tuples of arguments - # supposed to produce equal paths. - equivalences = { - 'a/b': [ - ('a', 'b'), ('a/', 'b'), ('a', 'b/'), ('a/', 'b/'), - ('a/b/',), ('a//b',), ('a//b//',), - # Empty components get removed. - ('', 'a', 'b'), ('a', '', 'b'), ('a', 'b', ''), - ], - '/b/c/d': [ - ('a', '/b/c', 'd'), ('/a', '/b/c', 'd'), - # Empty components get removed. - ('/', 'b', '', 'c/d'), ('/', '', 'b/c/d'), ('', '/b/c/d'), - ], - } - def setUp(self): p = self.cls('a') self.pathmod = p.pathmod @@ -132,31 +116,6 @@ def with_segments(self, *pathsegments): for parent in p.parents: self.assertEqual(42, parent.session_id) - def _check_parse_path(self, raw_path, *expected): - sep = self.pathmod.sep - actual = self.cls._parse_path(raw_path.replace('/', sep)) - self.assertEqual(actual, expected) - if altsep := self.pathmod.altsep: - actual = self.cls._parse_path(raw_path.replace('/', altsep)) - self.assertEqual(actual, expected) - - def test_parse_path_common(self): - check = self._check_parse_path - sep = self.pathmod.sep - check('', '', '', []) - check('a', '', '', ['a']) - check('a/', '', '', ['a']) - check('a/b', '', '', ['a', 'b']) - check('a/b/', '', '', ['a', 'b']) - check('a/b/c/d', '', '', ['a', 'b', 'c', 'd']) - check('a/b//c/d', '', '', ['a', 'b', 'c', 'd']) - check('a/b/c/d', '', '', ['a', 'b', 'c', 'd']) - check('.', '', '', []) - check('././b', '', '', ['b']) - check('a/./b', '', '', ['a', 'b']) - check('a/./.', '', '', ['a']) - check('/a/b', '', sep, ['a', 'b']) - def test_join_common(self): P = self.cls p = P('a/b') @@ -192,8 +151,6 @@ def test_str_common(self): # Canonicalized paths roundtrip. for pathstr in ('a', 'a/b', 'a/b/c', '/', '/a/b', '/a/b/c'): self._check_str(pathstr, (pathstr,)) - # Special case for the empty path. - self._check_str('.', ('',)) # Other tests for str() are in test_equivalences(). def test_as_posix_common(self): @@ -218,7 +175,6 @@ def test_eq_common(self): def test_match_empty(self): P = self.cls self.assertRaises(ValueError, P('a').match, '') - self.assertRaises(ValueError, P('a').match, '.') def test_match_common(self): P = self.cls @@ -258,7 +214,6 @@ def test_match_common(self): self.assertTrue(P('a/b/c.py').match('**')) self.assertTrue(P('/a/b/c.py').match('**')) self.assertTrue(P('/a/b/c.py').match('/**')) - self.assertTrue(P('/a/b/c.py').match('**/')) self.assertTrue(P('/a/b/c.py').match('/a/**')) self.assertTrue(P('/a/b/c.py').match('**/*.py')) self.assertTrue(P('/a/b/c.py').match('/**/*.py')) @@ -299,24 +254,6 @@ def test_parts_common(self): parts = p.parts self.assertEqual(parts, (sep, 'a', 'b')) - def test_equivalences(self): - for k, tuples in self.equivalences.items(): - canon = k.replace('/', self.sep) - posix = k.replace(self.sep, '/') - if canon != posix: - tuples = tuples + [ - tuple(part.replace('/', self.sep) for part in t) - for t in tuples - ] - tuples.append((posix, )) - pcanon = self.cls(canon) - for t in tuples: - p = self.cls(*t) - self.assertEqual(p, pcanon, "failed with args {}".format(t)) - self.assertEqual(hash(p), hash(pcanon)) - self.assertEqual(str(p), canon) - self.assertEqual(p.as_posix(), posix) - def test_parent_common(self): # Relative P = self.cls @@ -340,17 +277,17 @@ def test_parents_common(self): self.assertEqual(len(par), 3) self.assertEqual(par[0], P('a/b')) self.assertEqual(par[1], P('a')) - self.assertEqual(par[2], P('.')) - self.assertEqual(par[-1], P('.')) + self.assertEqual(par[2], P()) + self.assertEqual(par[-1], P()) self.assertEqual(par[-2], P('a')) self.assertEqual(par[-3], P('a/b')) self.assertEqual(par[0:1], (P('a/b'),)) self.assertEqual(par[:2], (P('a/b'), P('a'))) self.assertEqual(par[:-1], (P('a/b'), P('a'))) - self.assertEqual(par[1:], (P('a'), P('.'))) - self.assertEqual(par[::2], (P('a/b'), P('.'))) - self.assertEqual(par[::-1], (P('.'), P('a'), P('a/b'))) - self.assertEqual(list(par), [P('a/b'), P('a'), P('.')]) + self.assertEqual(par[1:], (P('a'), P())) + self.assertEqual(par[::2], (P('a/b'), P())) + self.assertEqual(par[::-1], (P(), P('a'), P('a/b'))) + self.assertEqual(list(par), [P('a/b'), P('a'), P()]) with self.assertRaises(IndexError): par[-4] with self.assertRaises(IndexError): @@ -404,8 +341,8 @@ def test_anchor_common(self): def test_name_empty(self): P = self.cls self.assertEqual(P('').name, '') - self.assertEqual(P('.').name, '') - self.assertEqual(P('/a/b/.').name, 'b') + self.assertEqual(P('.').name, '.') + self.assertEqual(P('/a/b/.').name, '.') def test_name_common(self): P = self.cls @@ -456,8 +393,9 @@ def test_suffixes_common(self): def test_stem_empty(self): P = self.cls + self.assertEqual(P().stem, '') self.assertEqual(P('').stem, '') - self.assertEqual(P('.').stem, '') + self.assertEqual(P('.').stem, '.') def test_stem_common(self): P = self.cls @@ -482,11 +420,11 @@ def test_with_name_common(self): def test_with_name_empty(self): P = self.cls - self.assertRaises(ValueError, P('').with_name, 'd.xml') - self.assertRaises(ValueError, P('.').with_name, 'd.xml') - self.assertRaises(ValueError, P('/').with_name, 'd.xml') - self.assertRaises(ValueError, P('a/b').with_name, '') - self.assertRaises(ValueError, P('a/b').with_name, '.') + self.assertEqual(P('').with_name('d.xml'), P('d.xml')) + self.assertEqual(P('.').with_name('d.xml'), P('d.xml')) + self.assertEqual(P('/').with_name('d.xml'), P('/d.xml')) + self.assertEqual(P('a/b').with_name(''), P('a/')) + self.assertEqual(P('a/b').with_name('.'), P('a/.')) def test_with_name_seps(self): P = self.cls @@ -506,11 +444,11 @@ def test_with_stem_common(self): def test_with_stem_empty(self): P = self.cls - self.assertRaises(ValueError, P('').with_stem, 'd') - self.assertRaises(ValueError, P('.').with_stem, 'd') - self.assertRaises(ValueError, P('/').with_stem, 'd') - self.assertRaises(ValueError, P('a/b').with_stem, '') - self.assertRaises(ValueError, P('a/b').with_stem, '.') + self.assertEqual(P('').with_stem('d'), P('d')) + self.assertEqual(P('.').with_stem('d'), P('d')) + self.assertEqual(P('/').with_stem('d'), P('/d')) + self.assertEqual(P('a/b').with_stem(''), P('a/')) + self.assertEqual(P('a/b').with_stem('.'), P('a/.')) def test_with_stem_seps(self): P = self.cls @@ -531,9 +469,9 @@ def test_with_suffix_common(self): def test_with_suffix_empty(self): P = self.cls # Path doesn't have a "filename" component. - self.assertRaises(ValueError, P('').with_suffix, '.gz') - self.assertRaises(ValueError, P('.').with_suffix, '.gz') - self.assertRaises(ValueError, P('/').with_suffix, '.gz') + self.assertEqual(P('').with_suffix('.gz'), P('.gz')) + self.assertEqual(P('.').with_suffix('.gz'), P('..gz')) + self.assertEqual(P('/').with_suffix('.gz'), P('/.gz')) def test_with_suffix_seps(self): P = self.cls @@ -556,14 +494,12 @@ def test_relative_to_common(self): self.assertEqual(p.relative_to(''), P('a/b')) self.assertEqual(p.relative_to(P('a')), P('b')) self.assertEqual(p.relative_to('a'), P('b')) - self.assertEqual(p.relative_to('a/'), P('b')) self.assertEqual(p.relative_to(P('a/b')), P()) self.assertEqual(p.relative_to('a/b'), P()) self.assertEqual(p.relative_to(P(), walk_up=True), P('a/b')) self.assertEqual(p.relative_to('', walk_up=True), P('a/b')) self.assertEqual(p.relative_to(P('a'), walk_up=True), P('b')) self.assertEqual(p.relative_to('a', walk_up=True), P('b')) - self.assertEqual(p.relative_to('a/', walk_up=True), P('b')) self.assertEqual(p.relative_to(P('a/b'), walk_up=True), P()) self.assertEqual(p.relative_to('a/b', walk_up=True), P()) self.assertEqual(p.relative_to(P('a/c'), walk_up=True), P('../b')) @@ -590,14 +526,12 @@ def test_relative_to_common(self): self.assertEqual(p.relative_to('/'), P('a/b')) self.assertEqual(p.relative_to(P('/a')), P('b')) self.assertEqual(p.relative_to('/a'), P('b')) - self.assertEqual(p.relative_to('/a/'), P('b')) self.assertEqual(p.relative_to(P('/a/b')), P()) self.assertEqual(p.relative_to('/a/b'), P()) self.assertEqual(p.relative_to(P('/'), walk_up=True), P('a/b')) self.assertEqual(p.relative_to('/', walk_up=True), P('a/b')) self.assertEqual(p.relative_to(P('/a'), walk_up=True), P('b')) self.assertEqual(p.relative_to('/a', walk_up=True), P('b')) - self.assertEqual(p.relative_to('/a/', walk_up=True), P('b')) self.assertEqual(p.relative_to(P('/a/b'), walk_up=True), P()) self.assertEqual(p.relative_to('/a/b', walk_up=True), P()) self.assertEqual(p.relative_to(P('/a/c'), walk_up=True), P('../b')) @@ -630,7 +564,6 @@ def test_is_relative_to_common(self): self.assertTrue(p.is_relative_to(P())) self.assertTrue(p.is_relative_to('')) self.assertTrue(p.is_relative_to(P('a'))) - self.assertTrue(p.is_relative_to('a/')) self.assertTrue(p.is_relative_to(P('a/b'))) self.assertTrue(p.is_relative_to('a/b')) # Unrelated paths. @@ -643,7 +576,6 @@ def test_is_relative_to_common(self): self.assertTrue(p.is_relative_to('/')) self.assertTrue(p.is_relative_to(P('/a'))) self.assertTrue(p.is_relative_to('/a')) - self.assertTrue(p.is_relative_to('/a/')) self.assertTrue(p.is_relative_to(P('/a/b'))) self.assertTrue(p.is_relative_to('/a/b')) # Unrelated paths. @@ -918,11 +850,6 @@ def test_samefile(self): self.assertRaises(FileNotFoundError, r.samefile, r) self.assertRaises(FileNotFoundError, r.samefile, non_existent) - def test_empty_path(self): - # The empty path points to '.' - p = self.cls('') - self.assertEqual(str(p), '.') - def test_exists(self): P = self.cls p = P(self.base) @@ -1598,15 +1525,6 @@ def test_is_char_device_false(self): self.assertIs((P / 'fileA\udfff').is_char_device(), False) self.assertIs((P / 'fileA\x00').is_char_device(), False) - def test_parts_interning(self): - P = self.cls - p = P('/usr/bin/foo') - q = P('/usr/local/bin') - # 'usr' - self.assertIs(p.parts[1], q.parts[1]) - # 'bin' - self.assertIs(p.parts[2], q.parts[3]) - def _check_complex_symlinks(self, link0_target): if not self.can_symlink: self.skipTest("symlinks required") From 72a2a0dfe8a7f6b13c83e6decfa2cd8cd1d41db9 Mon Sep 17 00:00:00 2001 From: barneygale Date: Mon, 8 Jan 2024 00:16:00 +0000 Subject: [PATCH 3/8] Restore `_load_parts()` --- Lib/pathlib/__init__.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Lib/pathlib/__init__.py b/Lib/pathlib/__init__.py index 784d4beaefbed5..410a5358d672de 100644 --- a/Lib/pathlib/__init__.py +++ b/Lib/pathlib/__init__.py @@ -256,14 +256,23 @@ def _parse_path(cls, path): parsed = [sys.intern(str(x)) for x in rel.split(sep) if x and x != '.'] return drv, root, parsed + def _load_parts(self): + paths = self._raw_paths + if len(paths) == 0: + path = '' + elif len(paths) == 1: + path = paths[0] + else: + path = self.pathmod.join(*paths) + self._drv, self._root, self._tail_cached = self._parse_path(path) + @property def drive(self): """The drive prefix (letter or UNC path), if any.""" try: return self._drv except AttributeError: - path = _abc.PurePathBase.__str__(self) - self._drv, self._root, self._tail_cached = self._parse_path(path) + self._load_parts() return self._drv @property @@ -272,8 +281,7 @@ def root(self): try: return self._root except AttributeError: - path = _abc.PurePathBase.__str__(self) - self._drv, self._root, self._tail_cached = self._parse_path(path) + self._load_parts() return self._root @property @@ -281,8 +289,7 @@ def _tail(self): try: return self._tail_cached except AttributeError: - path = _abc.PurePathBase.__str__(self) - self._drv, self._root, self._tail_cached = self._parse_path(path) + self._load_parts() return self._tail_cached @property From 04399eec9d94c870001a7a92213750512fa5f4ff Mon Sep 17 00:00:00 2001 From: barneygale Date: Mon, 8 Jan 2024 01:07:17 +0000 Subject: [PATCH 4/8] Move misplaced `__eq__()` tests --- Lib/test/test_pathlib/test_pathlib.py | 13 +++++++++++++ Lib/test/test_pathlib/test_pathlib_abc.py | 13 ------------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Lib/test/test_pathlib/test_pathlib.py b/Lib/test/test_pathlib/test_pathlib.py index 42935d6f9e80ed..5737951b221368 100644 --- a/Lib/test/test_pathlib/test_pathlib.py +++ b/Lib/test/test_pathlib/test_pathlib.py @@ -225,6 +225,19 @@ def test_as_bytes_common(self): P = self.cls self.assertEqual(bytes(P('a/b')), b'a' + sep + b'b') + def test_eq_common(self): + P = self.cls + self.assertEqual(P('a/b'), P('a/b')) + self.assertEqual(P('a/b'), P('a', 'b')) + self.assertNotEqual(P('a/b'), P('a')) + self.assertNotEqual(P('a/b'), P('/a/b')) + self.assertNotEqual(P('a/b'), P()) + self.assertNotEqual(P('/a/b'), P('/')) + self.assertNotEqual(P(), P('/')) + self.assertNotEqual(P(), "") + self.assertNotEqual(P(), {}) + self.assertNotEqual(P(), int) + def test_equivalences(self): for k, tuples in self.equivalences.items(): canon = k.replace('/', self.sep) diff --git a/Lib/test/test_pathlib/test_pathlib_abc.py b/Lib/test/test_pathlib/test_pathlib_abc.py index cdc441126d430a..3b10183a0d132c 100644 --- a/Lib/test/test_pathlib/test_pathlib_abc.py +++ b/Lib/test/test_pathlib/test_pathlib_abc.py @@ -159,19 +159,6 @@ def test_as_posix_common(self): self.assertEqual(P(pathstr).as_posix(), pathstr) # Other tests for as_posix() are in test_equivalences(). - def test_eq_common(self): - P = self.cls - self.assertEqual(P('a/b'), P('a/b')) - self.assertEqual(P('a/b'), P('a', 'b')) - self.assertNotEqual(P('a/b'), P('a')) - self.assertNotEqual(P('a/b'), P('/a/b')) - self.assertNotEqual(P('a/b'), P()) - self.assertNotEqual(P('/a/b'), P('/')) - self.assertNotEqual(P(), P('/')) - self.assertNotEqual(P(), "") - self.assertNotEqual(P(), {}) - self.assertNotEqual(P(), int) - def test_match_empty(self): P = self.cls self.assertRaises(ValueError, P('a').match, '') From ee6cc9979169c59d65cdd1a537e45c775bd7463f Mon Sep 17 00:00:00 2001 From: barneygale Date: Mon, 8 Jan 2024 01:16:46 +0000 Subject: [PATCH 5/8] Tweak `parents` expected results slightly --- Lib/test/test_pathlib/test_pathlib_abc.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_pathlib/test_pathlib_abc.py b/Lib/test/test_pathlib/test_pathlib_abc.py index 3b10183a0d132c..0a644f77f55393 100644 --- a/Lib/test/test_pathlib/test_pathlib_abc.py +++ b/Lib/test/test_pathlib/test_pathlib_abc.py @@ -264,17 +264,17 @@ def test_parents_common(self): self.assertEqual(len(par), 3) self.assertEqual(par[0], P('a/b')) self.assertEqual(par[1], P('a')) - self.assertEqual(par[2], P()) - self.assertEqual(par[-1], P()) + self.assertEqual(par[2], P('')) + self.assertEqual(par[-1], P('')) self.assertEqual(par[-2], P('a')) self.assertEqual(par[-3], P('a/b')) self.assertEqual(par[0:1], (P('a/b'),)) self.assertEqual(par[:2], (P('a/b'), P('a'))) self.assertEqual(par[:-1], (P('a/b'), P('a'))) - self.assertEqual(par[1:], (P('a'), P())) - self.assertEqual(par[::2], (P('a/b'), P())) - self.assertEqual(par[::-1], (P(), P('a'), P('a/b'))) - self.assertEqual(list(par), [P('a/b'), P('a'), P()]) + self.assertEqual(par[1:], (P('a'), P(''))) + self.assertEqual(par[::2], (P('a/b'), P(''))) + self.assertEqual(par[::-1], (P(''), P('a'), P('a/b'))) + self.assertEqual(list(par), [P('a/b'), P('a'), P('')]) with self.assertRaises(IndexError): par[-4] with self.assertRaises(IndexError): From 71aa5b7447dac807991edca6a003bf7c23d74896 Mon Sep 17 00:00:00 2001 From: barneygale Date: Mon, 8 Jan 2024 02:39:35 +0000 Subject: [PATCH 6/8] Simplify PurePathBase.parts --- Lib/pathlib/_abc.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py index 3ea2f16ef1d2c2..917584f2ba3379 100644 --- a/Lib/pathlib/_abc.py +++ b/Lib/pathlib/_abc.py @@ -297,17 +297,10 @@ def is_relative_to(self, other): def parts(self): """An object providing sequence-like access to the components in the filesystem path.""" - m = self.pathmod - drive, root, rel = m.splitroot(str(self)) - if rel: - if m.altsep: - rel = rel.replace(m.altsep, m.sep) - tail = tuple(rel.split(m.sep)) - else: - tail = tuple() - if drive or root: - tail = (drive + root,) + tail - return tail + anchor, parts = self._stack + if anchor: + parts.append(anchor) + return tuple(reversed(parts)) def joinpath(self, *pathsegments): """Combine this path with one or several arguments, and return a From 19ba54a1640f53edeee9c8fce286a4aafa7ae8a6 Mon Sep 17 00:00:00 2001 From: barneygale Date: Mon, 8 Jan 2024 05:12:21 +0000 Subject: [PATCH 7/8] Drop use of `altsep`, for now. --- Lib/pathlib/_abc.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py index 917584f2ba3379..d660d627e69f58 100644 --- a/Lib/pathlib/_abc.py +++ b/Lib/pathlib/_abc.py @@ -244,10 +244,10 @@ def stem(self): def with_name(self, name): """Return a new path with the file name changed.""" - m = self.pathmod - if m.sep in name or (m.altsep and m.altsep in name): + dirname = self.pathmod.dirname + if dirname(name): raise ValueError(f"Invalid name {name!r}") - return self.with_segments(m.dirname(str(self)), name) + return self.with_segments(dirname(str(self)), name) def with_stem(self, stem): """Return a new path with the stem changed.""" @@ -694,7 +694,7 @@ def glob(self, pattern, *, case_sensitive=None, follow_symlinks=None): raise ValueError("Unacceptable pattern: {!r}".format(pattern)) pattern_parts = list(path_pattern.parts) - if pattern[-1] in (self.pathmod.sep, self.pathmod.altsep): + if not self.pathmod.basename(pattern): # GH-65238: pathlib doesn't preserve trailing slash. Add it back. pattern_parts.append('') From b66501412571264c3da51bb828f08f866c6a1630 Mon Sep 17 00:00:00 2001 From: barneygale Date: Tue, 9 Jan 2024 21:30:11 +0000 Subject: [PATCH 8/8] Fix `[is_]relative_to()` trailing slash handling --- Lib/pathlib/__init__.py | 22 +++++++++++---- Lib/pathlib/_abc.py | 34 +++++++++++++++++------ Lib/test/test_pathlib/test_pathlib.py | 14 ---------- Lib/test/test_pathlib/test_pathlib_abc.py | 6 ++++ 4 files changed, 48 insertions(+), 28 deletions(-) diff --git a/Lib/pathlib/__init__.py b/Lib/pathlib/__init__.py index 7835a50ad227a3..9d3fcd894164e5 100644 --- a/Lib/pathlib/__init__.py +++ b/Lib/pathlib/__init__.py @@ -11,6 +11,7 @@ import posixpath import sys import warnings +from itertools import chain from _collections_abc import Sequence try: @@ -356,10 +357,19 @@ def relative_to(self, other, /, *_deprecated, walk_up=False): "scheduled for removal in Python 3.14") warnings.warn(msg, DeprecationWarning, stacklevel=2) other = self.with_segments(other, *_deprecated) - path = _abc.PurePathBase.relative_to(self, other, walk_up=walk_up) - path._drv = path._root = '' - path._tail_cached = path._raw_paths.copy() - return path + elif not isinstance(other, PurePath): + other = self.with_segments(other) + for step, path in enumerate(chain([other], other.parents)): + if path == self or path in self.parents: + break + elif not walk_up: + raise ValueError(f"{str(self)!r} is not in the subpath of {str(other)!r}") + elif path.name == '..': + raise ValueError(f"'..' segment in {str(other)!r} cannot be walked") + else: + raise ValueError(f"{str(self)!r} and {str(other)!r} have different anchors") + parts = ['..'] * step + self._tail[len(path._tail):] + return self._from_parsed_parts('', '', parts) def is_relative_to(self, other, /, *_deprecated): """Return True if the path is relative to another path or False. @@ -370,7 +380,9 @@ def is_relative_to(self, other, /, *_deprecated): "scheduled for removal in Python 3.14") warnings.warn(msg, DeprecationWarning, stacklevel=2) other = self.with_segments(other, *_deprecated) - return _abc.PurePathBase.is_relative_to(self, other) + elif not isinstance(other, PurePath): + other = self.with_segments(other) + return other == self or other in self.parents def as_uri(self): """Return the path as a URI.""" diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py index cae57cfe562257..6a1928495c9775 100644 --- a/Lib/pathlib/_abc.py +++ b/Lib/pathlib/_abc.py @@ -2,7 +2,6 @@ import ntpath import posixpath from errno import ENOENT, ENOTDIR, EBADF, ELOOP, EINVAL -from itertools import chain from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO # @@ -272,23 +271,40 @@ def relative_to(self, other, *, walk_up=False): """ if not isinstance(other, PurePathBase): other = self.with_segments(other) - for step, path in enumerate(chain([other], other.parents)): - if path == self or path in self.parents: - break + anchor0, parts0 = self._stack + anchor1, parts1 = other._stack + if anchor0 != anchor1: + raise ValueError(f"{str(self)!r} and {str(other)!r} have different anchors") + while parts0 and parts1 and parts0[-1] == parts1[-1]: + parts0.pop() + parts1.pop() + for part in parts1: + if not part or part == '.': + pass elif not walk_up: raise ValueError(f"{str(self)!r} is not in the subpath of {str(other)!r}") - elif path.name == '..': + elif part == '..': raise ValueError(f"'..' segment in {str(other)!r} cannot be walked") - else: - raise ValueError(f"{str(self)!r} and {str(other)!r} have different anchors") - return self.with_segments(*['..'] * step, *self.parts[len(path.parts):]) + else: + parts0.append('..') + return self.with_segments('', *reversed(parts0)) def is_relative_to(self, other): """Return True if the path is relative to another path or False. """ if not isinstance(other, PurePathBase): other = self.with_segments(other) - return other == self or other in self.parents + anchor0, parts0 = self._stack + anchor1, parts1 = other._stack + if anchor0 != anchor1: + return False + while parts0 and parts1 and parts0[-1] == parts1[-1]: + parts0.pop() + parts1.pop() + for part in parts1: + if part and part != '.': + return False + return True @property def parts(self): diff --git a/Lib/test/test_pathlib/test_pathlib.py b/Lib/test/test_pathlib/test_pathlib.py index 5737951b221368..fe943f5f550c18 100644 --- a/Lib/test/test_pathlib/test_pathlib.py +++ b/Lib/test/test_pathlib/test_pathlib.py @@ -338,15 +338,6 @@ def test_with_suffix_empty(self): self.assertRaises(ValueError, P('.').with_suffix, '.gz') self.assertRaises(ValueError, P('/').with_suffix, '.gz') - def test_relative_to_trailing_sep(self): - P = self.cls - p = P('a/b') - self.assertEqual(p.relative_to('a/'), P('b')) - self.assertEqual(p.relative_to('a/', walk_up=True), P('b')) - p = P('/a/b') - self.assertEqual(p.relative_to('/a/'), P('b')) - self.assertEqual(p.relative_to('/a/', walk_up=True), P('b')) - def test_relative_to_several_args(self): P = self.cls p = P('a/b') @@ -354,11 +345,6 @@ def test_relative_to_several_args(self): p.relative_to('a', 'b') p.relative_to('a', 'b', walk_up=True) - def test_is_relative_to_trailing_sep(self): - P = self.cls - self.assertTrue(P('a/b').is_relative_to('a/')) - self.assertTrue(P('/a/b').is_relative_to('/a/')) - def test_is_relative_to_several_args(self): P = self.cls p = P('a/b') diff --git a/Lib/test/test_pathlib/test_pathlib_abc.py b/Lib/test/test_pathlib/test_pathlib_abc.py index 639f7ce01e5b1c..05fe6fb413c9d0 100644 --- a/Lib/test/test_pathlib/test_pathlib_abc.py +++ b/Lib/test/test_pathlib/test_pathlib_abc.py @@ -483,12 +483,14 @@ def test_relative_to_common(self): self.assertEqual(p.relative_to(''), P('a/b')) self.assertEqual(p.relative_to(P('a')), P('b')) self.assertEqual(p.relative_to('a'), P('b')) + self.assertEqual(p.relative_to('a/'), P('b')) self.assertEqual(p.relative_to(P('a/b')), P()) self.assertEqual(p.relative_to('a/b'), P()) self.assertEqual(p.relative_to(P(), walk_up=True), P('a/b')) self.assertEqual(p.relative_to('', walk_up=True), P('a/b')) self.assertEqual(p.relative_to(P('a'), walk_up=True), P('b')) self.assertEqual(p.relative_to('a', walk_up=True), P('b')) + self.assertEqual(p.relative_to('a/', walk_up=True), P('b')) self.assertEqual(p.relative_to(P('a/b'), walk_up=True), P()) self.assertEqual(p.relative_to('a/b', walk_up=True), P()) self.assertEqual(p.relative_to(P('a/c'), walk_up=True), P('../b')) @@ -515,12 +517,14 @@ def test_relative_to_common(self): self.assertEqual(p.relative_to('/'), P('a/b')) self.assertEqual(p.relative_to(P('/a')), P('b')) self.assertEqual(p.relative_to('/a'), P('b')) + self.assertEqual(p.relative_to('/a/'), P('b')) self.assertEqual(p.relative_to(P('/a/b')), P()) self.assertEqual(p.relative_to('/a/b'), P()) self.assertEqual(p.relative_to(P('/'), walk_up=True), P('a/b')) self.assertEqual(p.relative_to('/', walk_up=True), P('a/b')) self.assertEqual(p.relative_to(P('/a'), walk_up=True), P('b')) self.assertEqual(p.relative_to('/a', walk_up=True), P('b')) + self.assertEqual(p.relative_to('/a/', walk_up=True), P('b')) self.assertEqual(p.relative_to(P('/a/b'), walk_up=True), P()) self.assertEqual(p.relative_to('/a/b', walk_up=True), P()) self.assertEqual(p.relative_to(P('/a/c'), walk_up=True), P('../b')) @@ -553,6 +557,7 @@ def test_is_relative_to_common(self): self.assertTrue(p.is_relative_to(P())) self.assertTrue(p.is_relative_to('')) self.assertTrue(p.is_relative_to(P('a'))) + self.assertTrue(p.is_relative_to('a/')) self.assertTrue(p.is_relative_to(P('a/b'))) self.assertTrue(p.is_relative_to('a/b')) # Unrelated paths. @@ -565,6 +570,7 @@ def test_is_relative_to_common(self): self.assertTrue(p.is_relative_to('/')) self.assertTrue(p.is_relative_to(P('/a'))) self.assertTrue(p.is_relative_to('/a')) + self.assertTrue(p.is_relative_to('/a/')) self.assertTrue(p.is_relative_to(P('/a/b'))) self.assertTrue(p.is_relative_to('/a/b')) # Unrelated paths.