Skip to content

Commit 89e05e4

Browse files
committed
Simplify _abc checking
1 parent 7194a7e commit 89e05e4

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

Lib/pathlib/_abc.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
from pathlib._os import magic_open, CopyWriter
1818

1919

20-
def _explode_path(path):
20+
def _explode_path(path, parser):
2121
"""
2222
Split the path into a 2-tuple (anchor, parts), where *anchor* is the
2323
uppermost parent of the path (equivalent to path.parents[-1]), and
2424
*parts* is a reversed list of parts following the anchor.
2525
"""
26-
split = path.parser.split
26+
split = parser.split
2727
path = str(path)
2828
parent, name = split(path)
2929
names = []
@@ -68,7 +68,7 @@ def __str__(self):
6868
@property
6969
def anchor(self):
7070
"""The concatenation of the drive and root, or ''."""
71-
return _explode_path(self)[0]
71+
return _explode_path(self, self.parser)[0]
7272

7373
@property
7474
def name(self):
@@ -140,7 +140,7 @@ def with_suffix(self, suffix):
140140
def parts(self):
141141
"""An object providing sequence-like access to the
142142
components in the filesystem path."""
143-
anchor, parts = _explode_path(self)
143+
anchor, parts = _explode_path(self, self.parser)
144144
if anchor:
145145
parts.append(anchor)
146146
return tuple(reversed(parts))
@@ -192,12 +192,12 @@ def full_match(self, pattern, *, case_sensitive=None):
192192
Return True if this path matches the given glob-style pattern. The
193193
pattern is matched against the entire path.
194194
"""
195-
if not hasattr(pattern, 'with_segments'):
196-
pattern = self.with_segments(pattern)
197195
if case_sensitive is None:
198196
case_sensitive = self.parser.normcase('Aa') == 'Aa'
199-
globber = _PathGlobber(pattern.parser.sep, case_sensitive, recursive=True)
200-
match = globber.compile(str(pattern))
197+
sep = self.parser.sep
198+
anchor, parts = _explode_path(pattern, self.parser)
199+
globber = _PathGlobber(sep, case_sensitive, recursive=True)
200+
match = globber.compile(anchor + sep.join(reversed(parts)))
201201
return match(str(self)) is not None
202202

203203

@@ -286,9 +286,7 @@ def glob(self, pattern, *, case_sensitive=None, recurse_symlinks=True):
286286
"""Iterate over this subtree and yield all existing files (of any
287287
kind, including directories) matching the given relative pattern.
288288
"""
289-
if not hasattr(pattern, 'with_segments'):
290-
pattern = self.with_segments(pattern)
291-
anchor, parts = _explode_path(pattern)
289+
anchor, parts = _explode_path(pattern, self.parser)
292290
if anchor:
293291
raise NotImplementedError("Non-relative patterns are unsupported")
294292
case_sensitive_default = self.parser.normcase('Aa') == 'Aa'
@@ -348,9 +346,6 @@ def copy(self, target, follow_symlinks=True, dirs_exist_ok=False,
348346
"""
349347
Recursively copy this file or directory tree to the given destination.
350348
"""
351-
if not hasattr(target, 'with_segments'):
352-
target = self.with_segments(target)
353-
354349
# Delegate to the target path's CopyWriter object.
355350
try:
356351
create = target._copy_writer._create
@@ -366,10 +361,8 @@ def copy_into(self, target_dir, *, follow_symlinks=True,
366361
name = self.name
367362
if not name:
368363
raise ValueError(f"{self!r} has an empty name")
369-
elif hasattr(target_dir, 'with_segments'):
370-
target = target_dir / name
371364
else:
372-
target = self.with_segments(target_dir, name)
365+
target = target_dir / name
373366
return self.copy(target, follow_symlinks=follow_symlinks,
374367
dirs_exist_ok=dirs_exist_ok,
375368
preserve_metadata=preserve_metadata)

0 commit comments

Comments
 (0)