@@ -409,7 +409,7 @@ def _check_copy(self, src_path, dst_path, overwrite=False):
409409 if self .gettype (src_path ) is not ResourceType .file :
410410 raise errors .FileExpected (src_path )
411411 # check dst_path does not exist if we are not overwriting
412- if not overwrite and self .exists (_dst_path ):
412+ if not overwrite and _src_path != _dst_path and self .exists (_dst_path ):
413413 raise errors .DestinationExists (dst_path )
414414 # check parent dir of _dst_path exists and is a directory
415415 if self .gettype (dirname (dst_path )) is not ResourceType .directory :
@@ -440,6 +440,9 @@ def copy(self, src_path, dst_path, overwrite=False, preserve_time=False):
440440 self .getsyspath (_src_path ),
441441 self .getsyspath (_dst_path ),
442442 )
443+ # exit early if we copy the file onto itself
444+ if overwrite and _src_sys == _dst_sys :
445+ return
443446 # attempt using sendfile
444447 try :
445448 # initialise variables to pass to sendfile
@@ -467,7 +470,14 @@ def copy(self, src_path, dst_path, overwrite=False, preserve_time=False):
467470 # type: (Text, Text, bool, bool) -> None
468471 with self ._lock :
469472 _src_path , _dst_path = self ._check_copy (src_path , dst_path , overwrite )
470- shutil .copy2 (self .getsyspath (_src_path ), self .getsyspath (_dst_path ))
473+ _src_sys , _dst_sys = (
474+ self .getsyspath (_src_path ),
475+ self .getsyspath (_dst_path ),
476+ )
477+ # exit early if we copy the file onto itself
478+ if overwrite and _src_sys == _dst_sys :
479+ return
480+ shutil .copy2 (_src_sys , _dst_sys )
471481
472482 # --- Backport of os.scandir for Python < 3.5 ------------
473483
0 commit comments