|
2 | 2 | import ntpath
|
3 | 3 | import posixpath
|
4 | 4 | import sys
|
5 |
| -from _collections_abc import Sequence |
6 | 5 | from errno import ENOENT, ENOTDIR, EBADF, ELOOP, EINVAL
|
7 | 6 | from itertools import chain
|
8 | 7 | from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO
|
@@ -138,35 +137,6 @@ class UnsupportedOperation(NotImplementedError):
|
138 | 137 | pass
|
139 | 138 |
|
140 | 139 |
|
141 |
| -class _PathParents(Sequence): |
142 |
| - """This object provides sequence-like access to the logical ancestors |
143 |
| - of a path. Don't try to construct it yourself.""" |
144 |
| - __slots__ = ('_path', '_drv', '_root', '_tail') |
145 |
| - |
146 |
| - def __init__(self, path): |
147 |
| - self._path = path |
148 |
| - self._drv = path.drive |
149 |
| - self._root = path.root |
150 |
| - self._tail = path._tail |
151 |
| - |
152 |
| - def __len__(self): |
153 |
| - return len(self._tail) |
154 |
| - |
155 |
| - def __getitem__(self, idx): |
156 |
| - if isinstance(idx, slice): |
157 |
| - return tuple(self[i] for i in range(*idx.indices(len(self)))) |
158 |
| - |
159 |
| - if idx >= len(self) or idx < -len(self): |
160 |
| - raise IndexError(idx) |
161 |
| - if idx < 0: |
162 |
| - idx += len(self) |
163 |
| - return self._path._from_parsed_parts(self._drv, self._root, |
164 |
| - self._tail[:-idx - 1]) |
165 |
| - |
166 |
| - def __repr__(self): |
167 |
| - return "<{}.parents>".format(type(self._path).__name__) |
168 |
| - |
169 |
| - |
170 | 140 | class PurePathBase:
|
171 | 141 | """Base class for pure path objects.
|
172 | 142 |
|
@@ -442,21 +412,26 @@ def __rtruediv__(self, key):
|
442 | 412 | @property
|
443 | 413 | def parent(self):
|
444 | 414 | """The logical parent of the path."""
|
445 |
| - drv = self.drive |
446 |
| - root = self.root |
447 |
| - tail = self._tail |
448 |
| - if not tail: |
449 |
| - return self |
450 |
| - path = self._from_parsed_parts(drv, root, tail[:-1]) |
451 |
| - path._resolving = self._resolving |
452 |
| - return path |
| 415 | + path = str(self) |
| 416 | + parent = self.pathmod.dirname(path) |
| 417 | + if path != parent: |
| 418 | + parent = self.with_segments(parent) |
| 419 | + parent._resolving = self._resolving |
| 420 | + return parent |
| 421 | + return self |
453 | 422 |
|
454 | 423 | @property
|
455 | 424 | def parents(self):
|
456 | 425 | """A sequence of this path's logical parents."""
|
457 |
| - # The value of this property should not be cached on the path object, |
458 |
| - # as doing so would introduce a reference cycle. |
459 |
| - return _PathParents(self) |
| 426 | + dirname = self.pathmod.dirname |
| 427 | + path = str(self) |
| 428 | + parent = dirname(path) |
| 429 | + parents = [] |
| 430 | + while path != parent: |
| 431 | + parents.append(self.with_segments(parent)) |
| 432 | + path = parent |
| 433 | + parent = dirname(path) |
| 434 | + return tuple(parents) |
460 | 435 |
|
461 | 436 | def is_absolute(self):
|
462 | 437 | """True if the path is absolute (has both a root and, if applicable,
|
|
0 commit comments