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.
This commit is contained in:
Berker Peksag 2014-12-10 02:51:36 +02:00
commit 5de4a3cfc5
3 changed files with 22 additions and 1 deletions

View File

@ -343,7 +343,7 @@ def copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2,
copystat(src, dst) copystat(src, dst)
except OSError as why: except OSError as why:
# Copying file access times may fail on Windows # Copying file access times may fail on Windows
if why.winerror is None: if getattr(why, 'winerror', None) is None:
errors.append((src, dst, str(why))) errors.append((src, dst, str(why)))
if errors: if errors:
raise Error(errors) raise Error(errors)

View File

@ -1,6 +1,7 @@
# Copyright (C) 2003 Python Software Foundation # Copyright (C) 2003 Python Software Foundation
import unittest import unittest
import unittest.mock
import shutil import shutil
import tempfile import tempfile
import sys import sys
@ -764,6 +765,20 @@ class TestShutil(unittest.TestCase):
self.assertEqual(os.stat(restrictive_subdir).st_mode, self.assertEqual(os.stat(restrictive_subdir).st_mode,
os.stat(restrictive_subdir_dst).st_mode) os.stat(restrictive_subdir_dst).st_mode)
@unittest.mock.patch('os.chmod')
def test_copytree_winerror(self, mock_patch):
# When copying to VFAT, copystat() raises OSError. On Windows, the
# exception object has a meaningful 'winerror' attribute, but not
# on other operating systems. Do not assume 'winerror' is set.
src_dir = tempfile.mkdtemp()
dst_dir = os.path.join(tempfile.mkdtemp(), 'destination')
self.addCleanup(shutil.rmtree, src_dir)
self.addCleanup(shutil.rmtree, os.path.dirname(dst_dir))
mock_patch.side_effect = PermissionError('ka-boom')
with self.assertRaises(shutil.Error):
shutil.copytree(src_dir, dst_dir)
@unittest.skipIf(os.name == 'nt', 'temporarily disabled on Windows') @unittest.skipIf(os.name == 'nt', 'temporarily disabled on Windows')
@unittest.skipUnless(hasattr(os, 'link'), 'requires os.link') @unittest.skipUnless(hasattr(os, 'link'), 'requires os.link')
def test_dont_copy_file_onto_link_to_itself(self): def test_dont_copy_file_onto_link_to_itself(self):

View File

@ -194,6 +194,12 @@ Core and Builtins
Library Library
------- -------
- 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.
- Issue #1218234: Fix inspect.getsource() to load updated source of - Issue #1218234: Fix inspect.getsource() to load updated source of
reloaded module. Initial patch by Berker Peksag. reloaded module. Initial patch by Berker Peksag.