summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2014-06-11 18:40:13 (GMT)
committerR David Murray <rdmurray@bitdance.com>2014-06-11 18:40:13 (GMT)
commit6ffface4293f20e504de6a7ca012c482a203409d (patch)
tree06d09d0290dfdaa7c0c990a060dc958710126cdc /Lib
parent6fe56a329dd427f2a5363b43ad24f04785d2091d (diff)
downloadcpython-6ffface4293f20e504de6a7ca012c482a203409d.zip
cpython-6ffface4293f20e504de6a7ca012c482a203409d.tar.gz
cpython-6ffface4293f20e504de6a7ca012c482a203409d.tar.bz2
#19840: Add copy_function to shutil.move.
Patch by Claudiu Popa.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/shutil.py15
-rw-r--r--Lib/test/test_shutil.py18
2 files changed, 29 insertions, 4 deletions
diff --git a/Lib/shutil.py b/Lib/shutil.py
index 508a368..b59017b 100644
--- a/Lib/shutil.py
+++ b/Lib/shutil.py
@@ -486,7 +486,7 @@ def _basename(path):
sep = os.path.sep + (os.path.altsep or '')
return os.path.basename(path.rstrip(sep))
-def move(src, dst):
+def move(src, dst, copy_function=copy2):
"""Recursively move a file or directory to another location. This is
similar to the Unix "mv" command. Return the file or directory's
destination.
@@ -503,6 +503,11 @@ def move(src, dst):
recreated under the new name if os.rename() fails because of cross
filesystem renames.
+ The optional `copy_function` argument is a callable that will be used
+ to copy the source or it will be delegated to `copytree`.
+ By default, copy2() is used, but any function that supports the same
+ signature (like copy()) can be used.
+
A lot more could be done here... A look at a mv.c shows a lot of
the issues this implementation glosses over.
@@ -527,11 +532,13 @@ def move(src, dst):
os.unlink(src)
elif os.path.isdir(src):
if _destinsrc(src, dst):
- raise Error("Cannot move a directory '%s' into itself '%s'." % (src, dst))
- copytree(src, real_dst, symlinks=True)
+ raise Error("Cannot move a directory '%s' into itself"
+ " '%s'." % (src, dst))
+ copytree(src, real_dst, copy_function=copy_function,
+ symlinks=True)
rmtree(src)
else:
- copy2(src, real_dst)
+ copy_function(src, real_dst)
os.unlink(src)
return real_dst
diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py
index a483fe1..098d58e 100644
--- a/Lib/test/test_shutil.py
+++ b/Lib/test/test_shutil.py
@@ -1592,6 +1592,24 @@ class TestMove(unittest.TestCase):
rv = shutil.move(self.src_file, os.path.join(self.dst_dir, 'bar'))
self.assertEqual(rv, os.path.join(self.dst_dir, 'bar'))
+ @mock_rename
+ def test_move_file_special_function(self):
+ moved = []
+ def _copy(src, dst):
+ moved.append((src, dst))
+ shutil.move(self.src_file, self.dst_dir, copy_function=_copy)
+ self.assertEqual(len(moved), 1)
+
+ @mock_rename
+ def test_move_dir_special_function(self):
+ moved = []
+ def _copy(src, dst):
+ moved.append((src, dst))
+ support.create_empty_file(os.path.join(self.src_dir, 'child'))
+ support.create_empty_file(os.path.join(self.src_dir, 'child1'))
+ shutil.move(self.src_dir, self.dst_dir, copy_function=_copy)
+ self.assertEqual(len(moved), 3)
+
class TestCopyFile(unittest.TestCase):