@@ -475,7 +475,7 @@ def relative_to(self, other, *, walk_up=False):
475
475
The *walk_up* parameter controls whether `..` may be used to resolve
476
476
the path.
477
477
"""
478
- if not isinstance (other , PurePath ):
478
+ if not hasattr (other , 'with_segments' ):
479
479
other = self .with_segments (other )
480
480
for step , path in enumerate (chain ([other ], other .parents )):
481
481
if path == self or path in self .parents :
@@ -492,7 +492,7 @@ def relative_to(self, other, *, walk_up=False):
492
492
def is_relative_to (self , other ):
493
493
"""Return True if the path is relative to another path or False.
494
494
"""
495
- if not isinstance (other , PurePath ):
495
+ if not hasattr (other , 'with_segments' ):
496
496
other = self .with_segments (other )
497
497
return other == self or other in self .parents
498
498
@@ -545,7 +545,7 @@ def full_match(self, pattern, *, case_sensitive=None):
545
545
Return True if this path matches the given glob-style pattern. The
546
546
pattern is matched against the entire path.
547
547
"""
548
- if not isinstance (pattern , PurePath ):
548
+ if not hasattr (pattern , 'with_segments' ):
549
549
pattern = self .with_segments (pattern )
550
550
if case_sensitive is None :
551
551
case_sensitive = self .parser is posixpath
@@ -564,7 +564,7 @@ def match(self, path_pattern, *, case_sensitive=None):
564
564
is matched. The recursive wildcard '**' is *not* supported by this
565
565
method.
566
566
"""
567
- if not isinstance (path_pattern , PurePath ):
567
+ if not hasattr (path_pattern , 'with_segments' ):
568
568
path_pattern = self .with_segments (path_pattern )
569
569
if case_sensitive is None :
570
570
case_sensitive = self .parser is posixpath
@@ -1064,7 +1064,9 @@ def rename(self, target):
1064
1064
Returns the new Path instance pointing to the target path.
1065
1065
"""
1066
1066
os .rename (self , target )
1067
- return self .with_segments (target )
1067
+ if not hasattr (target , 'with_segments' ):
1068
+ target = self .with_segments (target )
1069
+ return target
1068
1070
1069
1071
def replace (self , target ):
1070
1072
"""
@@ -1077,7 +1079,9 @@ def replace(self, target):
1077
1079
Returns the new Path instance pointing to the target path.
1078
1080
"""
1079
1081
os .replace (self , target )
1080
- return self .with_segments (target )
1082
+ if not hasattr (target , 'with_segments' ):
1083
+ target = self .with_segments (target )
1084
+ return target
1081
1085
1082
1086
_copy_writer = property (LocalCopyWriter )
1083
1087
@@ -1086,7 +1090,7 @@ def copy(self, target, follow_symlinks=True, dirs_exist_ok=False,
1086
1090
"""
1087
1091
Recursively copy this file or directory tree to the given destination.
1088
1092
"""
1089
- if not hasattr (target , '_copy_writer ' ):
1093
+ if not hasattr (target , 'with_segments ' ):
1090
1094
target = self .with_segments (target )
1091
1095
1092
1096
# Delegate to the target path's CopyWriter object.
@@ -1104,7 +1108,7 @@ def copy_into(self, target_dir, *, follow_symlinks=True,
1104
1108
name = self .name
1105
1109
if not name :
1106
1110
raise ValueError (f"{ self !r} has an empty name" )
1107
- elif hasattr (target_dir , '_copy_writer ' ):
1111
+ elif hasattr (target_dir , 'with_segments ' ):
1108
1112
target = target_dir / name
1109
1113
else :
1110
1114
target = self .with_segments (target_dir , name )
@@ -1118,16 +1122,13 @@ def move(self, target):
1118
1122
"""
1119
1123
# Use os.replace() if the target is os.PathLike and on the same FS.
1120
1124
try :
1121
- target_str = os . fspath (target )
1125
+ target = self . with_segments (target )
1122
1126
except TypeError :
1123
1127
pass
1124
1128
else :
1125
- if not hasattr (target , '_copy_writer' ):
1126
- target = self .with_segments (target_str )
1127
1129
ensure_different_files (self , target )
1128
1130
try :
1129
- os .replace (self , target_str )
1130
- return target
1131
+ return self .replace (target )
1131
1132
except OSError as err :
1132
1133
if err .errno != EXDEV :
1133
1134
raise
@@ -1143,7 +1144,7 @@ def move_into(self, target_dir):
1143
1144
name = self .name
1144
1145
if not name :
1145
1146
raise ValueError (f"{ self !r} has an empty name" )
1146
- elif hasattr (target_dir , '_copy_writer ' ):
1147
+ elif hasattr (target_dir , 'with_segments ' ):
1147
1148
target = target_dir / name
1148
1149
else :
1149
1150
target = self .with_segments (target_dir , name )
0 commit comments