Skip to content

Commit fd7a311

Browse files
committed
Address reviewer comments from ZeroIntensity
1 parent 3573851 commit fd7a311

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

Lib/shutil.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -524,16 +524,16 @@ def _copytree(entries, src, dst, symlinks, ignore, copy_function,
524524
else:
525525
copy_function(srcobj, dstname)
526526
elif srcentry.is_dir():
527+
# Will raise shutil.Error containing multiple exceptions gathered
528+
# from recursion
527529
copytree(srcobj, dstname, symlinks, ignore, copy_function,
528530
ignore_dangling_symlinks, dirs_exist_ok)
529531
else:
530-
# Will raise a SpecialFileError for unsupported file types
532+
# Can raise SpecialFileError or SameFileError
531533
copy_function(srcobj, dstname)
532534
except SameFileError as err:
533535
errors.append((srcname, dstname, str(err)))
534536
except Error as err:
535-
# catch the Error from the recursive copytree so that we can
536-
# continue with other files
537537
errors.extend(err.args[0])
538538
except OSError as why:
539539
errors.append((srcname, dstname, str(why)))

Lib/test/test_shutil.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,33 +1103,36 @@ def test_copytree_subdirectory(self):
11031103

11041104
def test_copytree_to_itself_gives_sensible_error_message(self):
11051105
base_dir = self.mkdtemp()
1106+
self.addCleanup(shutil.rmtree, base_dir, ignore_errors=True)
11061107
src_dir = os.path.join(base_dir, "src")
11071108
os.makedirs(src_dir)
11081109
create_file((src_dir, "somefilename"), "somecontent")
1109-
_assert_are_the_same_file_is_raised(src_dir, src_dir)
1110+
self._assert_are_the_same_file_is_raised(src_dir, src_dir)
11101111

11111112
@os_helper.skip_unless_symlink
11121113
def test_copytree_to_backpointing_symlink_gives_sensible_error_message(self):
11131114
base_dir = self.mkdtemp()
1115+
self.addCleanup(shutil.rmtree, base_dir, ignore_errors=True)
11141116
src_dir = os.path.join(base_dir, "src")
11151117
target_dir = os.path.join(base_dir, "target")
11161118
os.makedirs(src_dir)
11171119
os.makedirs(target_dir)
11181120
some_file = os.path.join(src_dir, "somefilename")
11191121
create_file(some_file, "somecontent")
11201122
os.symlink(some_file, os.path.join(target_dir, "somefilename"))
1121-
_assert_are_the_same_file_is_raised(src_dir, target_dir)
1123+
self._assert_are_the_same_file_is_raised(src_dir, target_dir)
1124+
1125+
def _assert_are_the_same_file_is_raised(self, src_dir, target_dir):
1126+
try:
1127+
shutil.copytree(src_dir, target_dir, dirs_exist_ok=True)
1128+
self.fail("shutil.Error should have been raised")
1129+
except Error as error:
1130+
self.assertEqual(len(error.args[0]), 1)
1131+
if sys.platform == "win32":
1132+
self.assertIn("it is being used by another process", error.args[0][0][2])
1133+
else:
1134+
self.assertIn("are the same file", error.args[0][0][2])
11221135

1123-
def _assert_are_the_same_file_is_raised(src_dir, target_dir):
1124-
try:
1125-
shutil.copytree(src_dir, target_dir, dirs_exist_ok=True)
1126-
raise Exception("copytree did not raise")
1127-
except Error as error:
1128-
assert len(error.args[0]) == 1
1129-
if sys.platform == "win32":
1130-
assert "it is being used by another process" in error.args[0][0][2]
1131-
else:
1132-
assert "are the same file" in error.args[0][0][2]
11331136

11341137
class TestCopy(BaseTest, unittest.TestCase):
11351138

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Make exception from copy_directory readable when SameFileError is raised.
1+
Make exception from :func:`shutil.copy_directory` readable when a
2+
:exc:`shutil.SameFileError` is raised.

0 commit comments

Comments
 (0)