]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-35652: shutil.copytree(copy_function=...) erroneously pass DirEntry instead of...
authorGiampaolo Rodola <g.rodola@gmail.com>
Tue, 26 Feb 2019 11:04:41 +0000 (12:04 +0100)
committerGitHub <noreply@github.com>
Tue, 26 Feb 2019 11:04:41 +0000 (12:04 +0100)
Lib/shutil.py
Lib/test/test_shutil.py
Misc/NEWS.d/next/Library/2019-02-26-11-34-44.bpo-35652.6KRJu_.rst [new file with mode: 0644]

index 1f98a348d7e629fa331acc4991fd19c2975f7213..9b50c2a9833a3c31b40c61617eec97432b60c24f 100644 (file)
@@ -472,7 +472,7 @@ def _copytree(entries, src, dst, symlinks, ignore, copy_function,
                          dirs_exist_ok=dirs_exist_ok)
             else:
                 # Will raise a SpecialFileError for unsupported file types
-                copy_function(srcentry, dstname)
+                copy_function(srcobj, dstname)
         # catch the Error from the recursive copytree so that we can
         # continue with other files
         except Error as err:
index ceafaeda1dc2300a3b32f71416ca6cf6f1eded17..678a190bcf5ee03ab2a42165f9a8258e3a90bddc 100644 (file)
@@ -859,6 +859,24 @@ class TestShutil(unittest.TestCase):
         with self.assertRaises(shutil.Error):
             shutil.copytree(src_dir, dst_dir)
 
+    def test_copytree_custom_copy_function(self):
+        # See: https://bugs.python.org/issue35648
+        def custom_cpfun(a, b):
+            flag.append(None)
+            self.assertIsInstance(a, str)
+            self.assertIsInstance(b, str)
+            self.assertEqual(a, os.path.join(src, 'foo'))
+            self.assertEqual(b, os.path.join(dst, 'foo'))
+
+        flag = []
+        src = tempfile.mkdtemp()
+        dst = tempfile.mktemp()
+        self.addCleanup(shutil.rmtree, src)
+        with open(os.path.join(src, 'foo'), 'w') as f:
+            f.close()
+        shutil.copytree(src, dst, copy_function=custom_cpfun)
+        self.assertEqual(len(flag), 1)
+
     @unittest.skipIf(os.name == 'nt', 'temporarily disabled on Windows')
     @unittest.skipUnless(hasattr(os, 'link'), 'requires os.link')
     def test_dont_copy_file_onto_link_to_itself(self):
diff --git a/Misc/NEWS.d/next/Library/2019-02-26-11-34-44.bpo-35652.6KRJu_.rst b/Misc/NEWS.d/next/Library/2019-02-26-11-34-44.bpo-35652.6KRJu_.rst
new file mode 100644 (file)
index 0000000..c247e17
--- /dev/null
@@ -0,0 +1,2 @@
+shutil.copytree(copy_function=...) erroneously pass DirEntry instead of a
+path string.