Skip to content

Commit 248173c

Browse files
authored
bpo-43219: shutil.copyfile, raise a less confusing exception instead of IsADirectoryError (pythonGH-27049)
Fixes the misleading IsADirectoryError to be FileNotFoundError.
1 parent f24777c commit 248173c

File tree

3 files changed

+42
-21
lines changed

3 files changed

+42
-21
lines changed

Lib/shutil.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -253,28 +253,36 @@ def copyfile(src, dst, *, follow_symlinks=True):
253253
if not follow_symlinks and _islink(src):
254254
os.symlink(os.readlink(src), dst)
255255
else:
256-
with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
257-
# macOS
258-
if _HAS_FCOPYFILE:
259-
try:
260-
_fastcopy_fcopyfile(fsrc, fdst, posix._COPYFILE_DATA)
261-
return dst
262-
except _GiveupOnFastCopy:
263-
pass
264-
# Linux
265-
elif _USE_CP_SENDFILE:
266-
try:
267-
_fastcopy_sendfile(fsrc, fdst)
256+
try:
257+
with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
258+
# macOS
259+
if _HAS_FCOPYFILE:
260+
try:
261+
_fastcopy_fcopyfile(fsrc, fdst, posix._COPYFILE_DATA)
262+
return dst
263+
except _GiveupOnFastCopy:
264+
pass
265+
# Linux
266+
elif _USE_CP_SENDFILE:
267+
try:
268+
_fastcopy_sendfile(fsrc, fdst)
269+
return dst
270+
except _GiveupOnFastCopy:
271+
pass
272+
# Windows, see:
273+
# https://github.com/python/cpython/pull/7160#discussion_r195405230
274+
elif _WINDOWS and file_size > 0:
275+
_copyfileobj_readinto(fsrc, fdst, min(file_size, COPY_BUFSIZE))
268276
return dst
269-
except _GiveupOnFastCopy:
270-
pass
271-
# Windows, see:
272-
# https://github.com/python/cpython/pull/7160#discussion_r195405230
273-
elif _WINDOWS and file_size > 0:
274-
_copyfileobj_readinto(fsrc, fdst, min(file_size, COPY_BUFSIZE))
275-
return dst
276-
277-
copyfileobj(fsrc, fdst)
277+
278+
copyfileobj(fsrc, fdst)
279+
280+
# Issue 43219, raise a less confusing exception
281+
except IsADirectoryError as e:
282+
if os.path.exists(dst):
283+
raise
284+
else:
285+
raise FileNotFoundError(f'Directory does not exist: {dst}') from e
278286

279287
return dst
280288

Lib/test/test_shutil.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,15 @@ def test_copyfile_same_file(self):
12491249
# Make sure file is not corrupted.
12501250
self.assertEqual(read_file(src_file), 'foo')
12511251

1252+
@unittest.skipIf(MACOS or _winapi, 'On MACOS and Windows the errors are not confusing (though different)')
1253+
def test_copyfile_nonexistent_dir(self):
1254+
# Issue 43219
1255+
src_dir = self.mkdtemp()
1256+
src_file = os.path.join(src_dir, 'foo')
1257+
dst = os.path.join(src_dir, 'does_not_exist/')
1258+
write_file(src_file, 'foo')
1259+
self.assertRaises(FileNotFoundError, shutil.copyfile, src_file, dst)
1260+
12521261

12531262
class TestArchives(BaseTest, unittest.TestCase):
12541263

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Update :func:`shutil.copyfile` to raise :exc:`FileNotFoundError` instead of
2+
confusing :exc:`IsADirectoryError` when a path ending with a
3+
:const:`os.path.sep` does not exist; :func:`shutil.copy` and
4+
:func:`shutil.copy2` are also affected.

0 commit comments

Comments
 (0)