Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 884afd9

Browse files
committed
Issue #21775: shutil.copytree(): fix crash when copying to VFAT
An exception handler assumed that that OSError objects always have a 'winerror' attribute. That is not the case, so the exception handler itself raised AttributeError when run on Linux (and, presumably, any other non-Windows OS). Patch by Greg Ward.
1 parent 8b1cbd2 commit 884afd9

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

Lib/shutil.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ def copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2,
337337
copystat(src, dst)
338338
except OSError as why:
339339
# Copying file access times may fail on Windows
340-
if why.winerror is None:
340+
if getattr(why, 'winerror', None) is None:
341341
errors.append((src, dst, str(why)))
342342
if errors:
343343
raise Error(errors)

Lib/test/test_shutil.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Copyright (C) 2003 Python Software Foundation
22

33
import unittest
4+
import unittest.mock
45
import shutil
56
import tempfile
67
import sys
@@ -758,6 +759,20 @@ def test_copytree_retains_permissions(self):
758759
self.assertEqual(os.stat(restrictive_subdir).st_mode,
759760
os.stat(restrictive_subdir_dst).st_mode)
760761

762+
@unittest.mock.patch('os.chmod')
763+
def test_copytree_winerror(self, mock_patch):
764+
# When copying to VFAT, copystat() raises OSError. On Windows, the
765+
# exception object has a meaningful 'winerror' attribute, but not
766+
# on other operating systems. Do not assume 'winerror' is set.
767+
src_dir = tempfile.mkdtemp()
768+
dst_dir = os.path.join(tempfile.mkdtemp(), 'destination')
769+
self.addCleanup(shutil.rmtree, src_dir)
770+
self.addCleanup(shutil.rmtree, os.path.dirname(dst_dir))
771+
772+
mock_patch.side_effect = PermissionError('ka-boom')
773+
with self.assertRaises(shutil.Error):
774+
shutil.copytree(src_dir, dst_dir)
775+
761776
@unittest.skipIf(os.name == 'nt', 'temporarily disabled on Windows')
762777
@unittest.skipUnless(hasattr(os, 'link'), 'requires os.link')
763778
def test_dont_copy_file_onto_link_to_itself(self):

Misc/NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ Core and Builtins
3939
Library
4040
-------
4141

42+
- Issue #21775: shutil.copytree(): fix crash when copying to VFAT. An exception
43+
handler assumed that that OSError objects always have a 'winerror' attribute.
44+
That is not the case, so the exception handler itself raised AttributeError
45+
when run on Linux (and, presumably, any other non-Windows OS).
46+
Patch by Greg Ward.
47+
4248
- Issue #1218234: Fix inspect.getsource() to load updated source of
4349
reloaded module. Initial patch by Berker Peksag.
4450

0 commit comments

Comments
 (0)