diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2021-07-10 04:07:35 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-10 04:07:35 (GMT) |
commit | 1577259cc51d0d46ad676798ce0a130039acf956 (patch) | |
tree | aa44697b7460159a142820809dc942ebdb510e6b | |
parent | bff66d0c3f3f39b81f9a8dc7a6159a3e373a7bf0 (diff) | |
download | cpython-1577259cc51d0d46ad676798ce0a130039acf956.zip cpython-1577259cc51d0d46ad676798ce0a130039acf956.tar.gz cpython-1577259cc51d0d46ad676798ce0a130039acf956.tar.bz2 |
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>
-rw-r--r-- | Lib/shutil.py | 50 | ||||
-rw-r--r-- | Lib/test/test_shutil.py | 9 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2021-07-09-07-14-37.bpo-41928.Q1jMrr.rst | 4 |
3 files changed, 42 insertions, 21 deletions
diff --git a/Lib/shutil.py b/Lib/shutil.py index 33f2ca9..d749b84 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -251,28 +251,36 @@ def copyfile(src, dst, *, follow_symlinks=True): if not follow_symlinks and _islink(src): os.symlink(os.readlink(src), dst) else: - with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst: - # macOS - if _HAS_FCOPYFILE: - try: - _fastcopy_fcopyfile(fsrc, fdst, posix._COPYFILE_DATA) - return dst - except _GiveupOnFastCopy: - pass - # Linux - elif _USE_CP_SENDFILE: - try: - _fastcopy_sendfile(fsrc, fdst) + try: + with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst: + # macOS + if _HAS_FCOPYFILE: + try: + _fastcopy_fcopyfile(fsrc, fdst, posix._COPYFILE_DATA) + return dst + except _GiveupOnFastCopy: + pass + # Linux + elif _USE_CP_SENDFILE: + try: + _fastcopy_sendfile(fsrc, fdst) + return dst + except _GiveupOnFastCopy: + pass + # Windows, see: + # https://github.com/python/cpython/pull/7160#discussion_r195405230 + elif _WINDOWS and file_size > 0: + _copyfileobj_readinto(fsrc, fdst, min(file_size, COPY_BUFSIZE)) return dst - except _GiveupOnFastCopy: - pass - # Windows, see: - # https://github.com/python/cpython/pull/7160#discussion_r195405230 - elif _WINDOWS and file_size > 0: - _copyfileobj_readinto(fsrc, fdst, min(file_size, COPY_BUFSIZE)) - return dst - - copyfileobj(fsrc, fdst) + + copyfileobj(fsrc, fdst) + + # Issue 43219, raise a less confusing exception + except IsADirectoryError as e: + if os.path.exists(dst): + raise + else: + raise FileNotFoundError(f'Directory does not exist: {dst}') from e return dst 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 @@ class TestCopy(BaseTest, unittest.TestCase): # 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): diff --git a/Misc/NEWS.d/next/Library/2021-07-09-07-14-37.bpo-41928.Q1jMrr.rst b/Misc/NEWS.d/next/Library/2021-07-09-07-14-37.bpo-41928.Q1jMrr.rst new file mode 100644 index 0000000..e6bd758 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-07-09-07-14-37.bpo-41928.Q1jMrr.rst @@ -0,0 +1,4 @@ +Update :func:`shutil.copyfile` to raise :exc:`FileNotFoundError` instead of +confusing :exc:`IsADirectoryError` when a path ending with a +:const:`os.path.sep` does not exist; :func:`shutil.copy` and +:func:`shutil.copy2` are also affected. |