Fix #14772: Return the destination from some shutil functions.
diff --git a/Lib/shutil.py b/Lib/shutil.py
index ce60c3b..46398ef 100644
--- a/Lib/shutil.py
+++ b/Lib/shutil.py
@@ -109,6 +109,7 @@
with open(src, 'rb') as fsrc:
with open(dst, 'wb') as fdst:
copyfileobj(fsrc, fdst)
+ return dst
def copymode(src, dst, symlinks=False):
"""Copy mode bits from src to dst.
@@ -197,7 +198,7 @@
pass
def copy(src, dst, symlinks=False):
- """Copy data and mode bits ("cp src dst").
+ """Copy data and mode bits ("cp src dst"). Return the file's destination.
The destination may be a directory.
@@ -209,9 +210,11 @@
dst = os.path.join(dst, os.path.basename(src))
copyfile(src, dst, symlinks=symlinks)
copymode(src, dst, symlinks=symlinks)
+ return dst
def copy2(src, dst, symlinks=False):
- """Copy data and all stat info ("cp -p src dst").
+ """Copy data and all stat info ("cp -p src dst"). Return the file's
+ destination."
The destination may be a directory.
@@ -224,6 +227,7 @@
copyfile(src, dst, symlinks=symlinks)
copystat(src, dst, symlinks=symlinks)
_copyxattr(src, dst, symlinks=symlinks)
+ return dst
def ignore_patterns(*patterns):
"""Function that can be used as copytree() ignore parameter.
@@ -322,6 +326,7 @@
errors.extend((src, dst, str(why)))
if errors:
raise Error(errors)
+ return dst
def rmtree(path, ignore_errors=False, onerror=None):
"""Recursively delete a directory tree.
@@ -379,7 +384,8 @@
def move(src, dst):
"""Recursively move a file or directory to another location. This is
- similar to the Unix "mv" command.
+ similar to the Unix "mv" command. Return the file or directory's
+ destination.
If the destination is a directory or a symlink to a directory, the source
is moved inside the directory. The destination path must not already
@@ -423,6 +429,7 @@
else:
copy2(src, real_dst)
os.unlink(src)
+ return real_dst
def _destinsrc(src, dst):
src = abspath(src)
diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py
index 3b4e381..1181520 100644
--- a/Lib/test/test_shutil.py
+++ b/Lib/test/test_shutil.py
@@ -1095,6 +1095,38 @@
shutil.chown(dirname, user, group)
check_chown(dirname, uid, gid)
+ def test_copy_return_value(self):
+ # copy and copy2 both return their destination path.
+ for fn in (shutil.copy, shutil.copy2):
+ src_dir = self.mkdtemp()
+ dst_dir = self.mkdtemp()
+ src = os.path.join(src_dir, 'foo')
+ write_file(src, 'foo')
+ rv = fn(src, dst_dir)
+ self.assertEqual(rv, os.path.join(dst_dir, 'foo'))
+ rv = fn(src, os.path.join(dst_dir, 'bar'))
+ self.assertEqual(rv, os.path.join(dst_dir, 'bar'))
+
+ def test_copyfile_return_value(self):
+ # copytree returns its destination path.
+ src_dir = self.mkdtemp()
+ dst_dir = self.mkdtemp()
+ dst_file = os.path.join(dst_dir, 'bar')
+ src_file = os.path.join(src_dir, 'foo')
+ write_file(src_file, 'foo')
+ rv = shutil.copyfile(src_file, dst_file)
+ self.assertTrue(os.path.exists(rv))
+ self.assertEqual(read_file(src_file), read_file(dst_file))
+
+ def test_copytree_return_value(self):
+ # copytree returns its destination path.
+ src_dir = self.mkdtemp()
+ dst_dir = src_dir + "dest"
+ src = os.path.join(src_dir, 'foo')
+ write_file(src, 'foo')
+ rv = shutil.copytree(src_dir, dst_dir)
+ self.assertEqual(['foo'], os.listdir(rv))
+
class TestMove(unittest.TestCase):
@@ -1251,6 +1283,15 @@
self.assertTrue(os.path.islink(dst_link))
self.assertTrue(os.path.samefile(src, dst_link))
+ def test_move_return_value(self):
+ rv = shutil.move(self.src_file, self.dst_dir)
+ self.assertEqual(rv,
+ os.path.join(self.dst_dir, os.path.basename(self.src_file)))
+
+ def test_move_as_rename_return_value(self):
+ rv = shutil.move(self.src_file, os.path.join(self.dst_dir, 'bar'))
+ self.assertEqual(rv, os.path.join(self.dst_dir, 'bar'))
+
class TestCopyFile(unittest.TestCase):