Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Lib/shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,10 +611,14 @@ def _copytree(entries, src, dst, symlinks, ignore, copy_function,
else:
# Will raise a SpecialFileError for unsupported file types
copy_function(srcobj, dstname)
# catch the Error from the recursive copytree so that we can
# continue with other files
# catch the Error from the recursive copytree (or any other
# shutil.Error that copytree raises, e.g. SameFileError)
# so that we can continue with other files
except Error as err:
errors.extend(err.args[0])
if err.args and isinstance(err.args[0], list):
errors.extend(err.args[0])
else:
errors.append((srcname, dstname, str(err)))
except OSError as why:
errors.append((srcname, dstname, str(why)))
try:
Expand Down
37 changes: 37 additions & 0 deletions Lib/test/test_shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,43 @@ def test_copytree_subdirectory(self):
rv = shutil.copytree(src_dir, dst_dir)
self.assertEqual(['pol'], os.listdir(rv))

def test_copytree_to_itself_gives_sensible_error_message(self):
base_dir = self.mkdtemp()
Comment thread
ZeroIntensity marked this conversation as resolved.
Resolved
self.addCleanup(shutil.rmtree, base_dir, ignore_errors=True)
src_dir = os.path.join(base_dir, "src")
os.makedirs(src_dir)
create_file((src_dir, "somefilename"), "somecontent")
with self._assert_are_the_same_file_is_raised():
shutil.copytree(src_dir, src_dir, dirs_exist_ok=True)

@os_helper.skip_unless_symlink
def test_copytree_to_backpointing_symlink_gives_sensible_error_message(self):
base_dir = self.mkdtemp()
self.addCleanup(shutil.rmtree, base_dir, ignore_errors=True)
src_dir = os.path.join(base_dir, "src")
target_dir = os.path.join(base_dir, "target")
os.makedirs(src_dir)
os.makedirs(target_dir)
some_file = os.path.join(src_dir, "somefilename")
create_file(some_file, "somecontent")
os.symlink(some_file, os.path.join(target_dir, "somefilename"))
with self._assert_are_the_same_file_is_raised():
shutil.copytree(src_dir, target_dir, dirs_exist_ok=True)

@contextlib.contextmanager
def _assert_are_the_same_file_is_raised(self):
with self.assertRaises(Error) as cm:
yield

errors = cm.exception.args[0]
self.assertEqual(len(errors), 1)
_, _, why = errors[0]
if sys.platform == "win32":
self.assertIn("it is being used by another process", why)
else:
self.assertIn("are the same file", why)


class TestCopy(BaseTest, unittest.TestCase):

### shutil.copymode
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :func:`shutil.copytree` splitting the message of a non-list
:exc:`shutil.Error`, such as :exc:`shutil.SameFileError`,
into single characters in the collected error list.
Loading