[Python-checkins] cpython (merge 3.4 -> default): Issue #21775: shutil.copytree(): fix crash when copying to VFAT

berker.peksag python-checkins at python.org
Wed Dec 10 01:51:31 CET 2014


https://hg.python.org/cpython/rev/7d5754af95a9
changeset:   93800:7d5754af95a9
parent:      93798:8c337b4a8811
parent:      93799:50517a4d7cce
user:        Berker Peksag <berker.peksag at gmail.com>
date:        Wed Dec 10 02:51:36 2014 +0200
summary:
  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.

files:
  Lib/shutil.py           |   2 +-
  Lib/test/test_shutil.py |  15 +++++++++++++++
  Misc/NEWS               |   6 ++++++
  3 files changed, 22 insertions(+), 1 deletions(-)


diff --git a/Lib/shutil.py b/Lib/shutil.py
--- a/Lib/shutil.py
+++ b/Lib/shutil.py
@@ -343,7 +343,7 @@
         copystat(src, dst)
     except OSError as why:
         # 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)))
     if errors:
         raise Error(errors)
diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py
--- a/Lib/test/test_shutil.py
+++ b/Lib/test/test_shutil.py
@@ -1,6 +1,7 @@
 # Copyright (C) 2003 Python Software Foundation
 
 import unittest
+import unittest.mock
 import shutil
 import tempfile
 import sys
@@ -764,6 +765,20 @@
         self.assertEqual(os.stat(restrictive_subdir).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.skipUnless(hasattr(os, 'link'), 'requires os.link')
     def test_dont_copy_file_onto_link_to_itself(self):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -194,6 +194,12 @@
 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
   reloaded module. Initial patch by Berker Peksag.
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list