Skip to content

Commit b3d2d73

Browse files
committed
fix memoryfs
1 parent 0e3ed72 commit b3d2d73

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

fs/base.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,13 +423,18 @@ def copy(
423423
424424
"""
425425
with self._lock:
426-
if not overwrite and self.exists(dst_path):
426+
_src_path = self.validatepath(src_path)
427+
_dst_path = self.validatepath(dst_path)
428+
if not overwrite and self.exists(_dst_path):
427429
raise errors.DestinationExists(dst_path)
428-
with closing(self.open(src_path, "rb")) as read_file:
430+
if overwrite and _src_path == _dst_path:
431+
# exit early when copying a file onto itself
432+
return
433+
with closing(self.open(_src_path, "rb")) as read_file:
429434
# FIXME(@althonos): typing complains because open return IO
430-
self.upload(dst_path, read_file) # type: ignore
435+
self.upload(_dst_path, read_file) # type: ignore
431436
if preserve_time:
432-
copy_modified_time(self, src_path, self, dst_path)
437+
copy_modified_time(self, _src_path, self, _dst_path)
433438

434439
def copydir(
435440
self,

fs/memoryfs.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,12 @@ def move(self, src_path, dst_path, overwrite=False, preserve_time=False):
462462
elif not overwrite and dst_name in dst_dir_entry:
463463
raise errors.DestinationExists(dst_path)
464464

465+
# handle moving a file onto itself
466+
if src_dir == dst_dir and src_name == dst_name:
467+
if overwrite:
468+
return
469+
raise errors.DestinationExists(dst_path)
470+
465471
# move the entry from the src folder to the dst folder
466472
dst_dir_entry.set_entry(dst_name, src_entry)
467473
src_dir_entry.remove_entry(src_name)

0 commit comments

Comments
 (0)