bpo-43219: shutil.copyfile, raise a less confusing exception instead of IsADirectoryError (GH-27049)


Fixes the misleading IsADirectoryError to be FileNotFoundError.
(cherry picked from commit 248173cc0483a9ad9261353302f1234cf9eb2ebe)

Co-authored-by: andrei kulakov <andrei.avk@gmail.com>
diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py
index 493c7e5..e257425 100644
--- a/Lib/test/test_shutil.py
+++ b/Lib/test/test_shutil.py
@@ -1249,6 +1249,15 @@ def test_copyfile_same_file(self):
         # Make sure file is not corrupted.
         self.assertEqual(read_file(src_file), 'foo')
 
+    @unittest.skipIf(MACOS or _winapi, 'On MACOS and Windows the errors are not confusing (though different)')
+    def test_copyfile_nonexistent_dir(self):
+        # Issue 43219
+        src_dir = self.mkdtemp()
+        src_file = os.path.join(src_dir, 'foo')
+        dst = os.path.join(src_dir, 'does_not_exist/')
+        write_file(src_file, 'foo')
+        self.assertRaises(FileNotFoundError, shutil.copyfile, src_file, dst)
+
 
 class TestArchives(BaseTest, unittest.TestCase):