Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit37bd893

Browse files
authored
GH-113528: Deoptimisepathlib._abc.PurePathBase.parent (#113530)
Replace use of `_from_parsed_parts()` with `with_segments()`, and moveassignments to `_drv`, `_root`, _tail_cached` and `_str` slots into`PurePath`.
1 parent1e914ad commit37bd893

File tree

2 files changed

+63
-42
lines changed

2 files changed

+63
-42
lines changed

‎Lib/pathlib/__init__.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
importposixpath
1212
importsys
1313
importwarnings
14+
from_collections_abcimportSequence
1415

1516
try:
1617
importpwd
@@ -31,6 +32,35 @@
3132
]
3233

3334

35+
class_PathParents(Sequence):
36+
"""This object provides sequence-like access to the logical ancestors
37+
of a path. Don't try to construct it yourself."""
38+
__slots__= ('_path','_drv','_root','_tail')
39+
40+
def__init__(self,path):
41+
self._path=path
42+
self._drv=path.drive
43+
self._root=path.root
44+
self._tail=path._tail
45+
46+
def__len__(self):
47+
returnlen(self._tail)
48+
49+
def__getitem__(self,idx):
50+
ifisinstance(idx,slice):
51+
returntuple(self[i]foriinrange(*idx.indices(len(self))))
52+
53+
ifidx>=len(self)oridx<-len(self):
54+
raiseIndexError(idx)
55+
ifidx<0:
56+
idx+=len(self)
57+
returnself._path._from_parsed_parts(self._drv,self._root,
58+
self._tail[:-idx-1])
59+
60+
def__repr__(self):
61+
return"<{}.parents>".format(type(self._path).__name__)
62+
63+
3464
UnsupportedOperation=_abc.UnsupportedOperation
3565

3666

@@ -95,7 +125,6 @@ def __init__(self, *args):
95125
paths.append(path)
96126
# Avoid calling super().__init__, as an optimisation
97127
self._raw_paths=paths
98-
self._resolving=False
99128

100129
def__reduce__(self):
101130
# Using the parts tuple helps share interned path parts
@@ -166,6 +195,23 @@ def __ge__(self, other):
166195
returnNotImplemented
167196
returnself._parts_normcase>=other._parts_normcase
168197

198+
@property
199+
defparent(self):
200+
"""The logical parent of the path."""
201+
drv=self.drive
202+
root=self.root
203+
tail=self._tail
204+
ifnottail:
205+
returnself
206+
returnself._from_parsed_parts(drv,root,tail[:-1])
207+
208+
@property
209+
defparents(self):
210+
"""A sequence of this path's logical parents."""
211+
# The value of this property should not be cached on the path object,
212+
# as doing so would introduce a reference cycle.
213+
return_PathParents(self)
214+
169215
@property
170216
defname(self):
171217
"""The final path component, if any."""

‎Lib/pathlib/_abc.py

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
importntpath
33
importposixpath
44
importsys
5-
from_collections_abcimportSequence
65
fromerrnoimportENOENT,ENOTDIR,EBADF,ELOOP,EINVAL
76
fromitertoolsimportchain
87
fromstatimportS_ISDIR,S_ISLNK,S_ISREG,S_ISSOCK,S_ISBLK,S_ISCHR,S_ISFIFO
@@ -138,35 +137,6 @@ class UnsupportedOperation(NotImplementedError):
138137
pass
139138

140139

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-
returnlen(self._tail)
154-
155-
def__getitem__(self,idx):
156-
ifisinstance(idx,slice):
157-
returntuple(self[i]foriinrange(*idx.indices(len(self))))
158-
159-
ifidx>=len(self)oridx<-len(self):
160-
raiseIndexError(idx)
161-
ifidx<0:
162-
idx+=len(self)
163-
returnself._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-
170140
classPurePathBase:
171141
"""Base class for pure path objects.
172142
@@ -442,21 +412,26 @@ def __rtruediv__(self, key):
442412
@property
443413
defparent(self):
444414
"""The logical parent of the path."""
445-
drv=self.drive
446-
root=self.root
447-
tail=self._tail
448-
ifnottail:
449-
returnself
450-
path=self._from_parsed_parts(drv,root,tail[:-1])
451-
path._resolving=self._resolving
452-
returnpath
415+
path=str(self)
416+
parent=self.pathmod.dirname(path)
417+
ifpath!=parent:
418+
parent=self.with_segments(parent)
419+
parent._resolving=self._resolving
420+
returnparent
421+
returnself
453422

454423
@property
455424
defparents(self):
456425
"""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+
whilepath!=parent:
431+
parents.append(self.with_segments(parent))
432+
path=parent
433+
parent=dirname(path)
434+
returntuple(parents)
460435

461436
defis_absolute(self):
462437
"""True if the path is absolute (has both a root and, if applicable,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp