8000 bpo-45234: Fix FileNotFound exception raised instead of IsADirectoryE… · python/cpython@b7eac52 · GitHub
[go: up one dir, main page]

Skip to content

Commit b7eac52

Browse files
authored
bpo-45234: Fix FileNotFound exception raised instead of IsADirectoryError in shutil.copyfile() (GH-28421)
This was a regression from fixing BPO-43219.
1 parent 86f2837 commit b7eac52

File tree

3 files changed

+73
-29
lines changed

3 files changed

+73
-29
lines changed

Lib/shutil.py

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

287288
return dst
288289

Lib/test/test_shutil.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,28 @@ def test_copy_return_value(self):
11511151
rv = fn(src, os.path.join(dst_dir, 'bar'))
11521152
self.assertEqual(rv, os.path.join(dst_dir, 'bar'))
11531153

1154+
def test_copy_dir(self):
1155+
self._test_copy_dir(shutil.copy)
1156+
1157+
def test_copy2_dir(self):
1158+
self._test_copy_dir(shutil.copy2)
1159+
1160+
def _test_copy_dir(self, copy_func):
1161+
src_dir = self.mkdtemp()
1162+
src_file = os.path.join(src_dir, 'foo')
1163+
dir2 = self.mkdtemp()
1164+
dst = os.path.join(src_dir, 'does_not_exist/')
1165+
write_file(src_file, 'foo')
1166+
if sys.platform == "win32":
1167+
err = PermissionError
1168+
else:
1169+
err = IsADirectoryError
1170+
self.assertRaises(err, copy_func, dir2, src_dir)
1171+
1172+
# raise *err* because of src rather than FileNotFoundError because of dst
1173+
self.assertRaises(err, copy_func, dir2, dst)
1174+
copy_func(src_file, dir2) # should not raise exceptions
1175+
11541176
### shutil.copyfile
11551177

11561178
@os_helper.skip_unless_symlink
@@ -1259,6 +1281,24 @@ def test_copyfile_nonexistent_dir(self):
12591281
write_file(src_file, 'foo')
12601282
self.assertRaises(File 685C NotFoundError, shutil.copyfile, src_file, dst)
12611283

1284+
def test_copyfile_copy_dir(self):
1285+
# Issue 45234
1286+
# test copy() and copyfile() raising proper exceptions when src and/or
1287+
# dst are directories
1288+
src_dir = self.mkdtemp()
1289+
src_file = os.path.join(src_dir, 'foo')
1290+
dir2 = self.mkdtemp()
1291+
dst = os.path.join(src_dir, 'does_not_exist/')
1292+
write_file(src_file, 'foo')
1293+
if sys.platform == "win32":
1294+
err = PermissionError
1295+
else:
1296+
err = IsADirectoryError
1297+
1298+
self.assertRaises(err, shutil.copyfile, src_dir, dst)
1299+
self.assertRaises(err, shutil.copyfile, src_file, src_dir)
1300+
self.assertRaises(err, shutil.copyfile, dir2, src_dir)
1301+
12621302

12631303
class TestArchives(BaseTest, unittest.TestCase):
12641304

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed a regression in :func:`~shutil.copyfile`, :func:`~shutil.copy`,
2+
:func:`~shutil.copy2` raising :exc:`FileNotFoundError` when source is a
3+
directory, which should raise :exc:`IsADirectoryError`

0 commit comments

Comments
 (0)
0