-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
gh-102931: Use explicit exception when shutil._copytree should group exceptions #128309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
533be29
d70fcb7
eb41edf
3d03a5f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1099,6 +1099,39 @@ 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() | ||
ZeroIntensity marked this conversation as resolved.
Show resolved
Hide 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") | ||
| self._assert_are_the_same_file_is_raised(src_dir, src_dir) | ||
|
|
||
| @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")) | ||
| self._assert_are_the_same_file_is_raised(src_dir, target_dir) | ||
|
|
||
| def _assert_are_the_same_file_is_raised(self, src_dir, target_dir): | ||
| try: | ||
| shutil.copytree(src_dir, target_dir, dirs_exist_ok=True) | ||
| self.fail("shutil.Error should have been raised") | ||
| except Error as error: | ||
|
Comment on lines
+1124
to
+1127
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh right. Then just assertRaises |
||
| self.assertEqual(len(error.args[0]), 1) | ||
| if sys.platform == "win32": | ||
| self.assertIn("it is being used by another process", error.args[0][0][2]) | ||
| else: | ||
| self.assertIn("are the same file", error.args[0][0][2]) | ||
|
|
||
|
|
||
| class TestCopy(BaseTest, unittest.TestCase): | ||
|
|
||
| ### shutil.copymode | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Make exception from :func:`shutil.copytree` readable when a | ||
| :exc:`shutil.SameFileError` is raised. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.