This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author gward
Recipients gward, serhiy.storchaka, vstinner
Date 2014-11-21.01:37:29
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
> Would it be possible to write a unit test, maybe using unittest.mock to 
> mock most parts?

Good idea! Turns out this was quite straightforward. The test patch is:

--- a/Lib/test/
+++ b/Lib/test/
@@ -1,6 +1,7 @@
 # Copyright (C) 2003 Python Software Foundation
 import unittest
+import unittest.mock
 import shutil
 import tempfile
 import sys
@@ -758,6 +759,20 @@
+    @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( == 'nt', 'temporarily disabled on Windows')
     @unittest.skipUnless(hasattr(os, 'link'), 'requires')
     def test_dont_copy_file_onto_link_to_itself(self):

When run without the bug fix, this reproduces my original failure nicely:

$ ./python Lib/test/
ERROR: test_copytree_winerror (__main__.TestShutil)
Traceback (most recent call last):
  File "/data/src/cpython/3.4/Lib/", line 337, in copytree
    copystat(src, dst)
  File "/data/src/cpython/3.4/Lib/", line 191, in copystat
    lookup("chmod")(dst, mode, follow_symlinks=follow)
  File "/data/src/cpython/3.4/Lib/unittest/", line 896, in __call__
    return _mock_self._mock_call(*args, **kwargs)
  File "/data/src/cpython/3.4/Lib/unittest/", line 952, in _mock_call
    raise effect
PermissionError: ka-boom

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/data/src/cpython/3.4/Lib/unittest/", line 1136, in patched
    return func(*args, **keywargs)
  File "Lib/test/", line 774, in test_copytree_winerror
    shutil.copytree(src_dir, dst_dir)
  File "/data/src/cpython/3.4/Lib/", line 340, in copytree
    if why.winerror is None:
AttributeError: 'PermissionError' object has no attribute 'winerror'

Ran 89 tests in 0.095s

FAILED (errors=1, skipped=5)

Excellent! No need for root privs, and the bug is quite obvious.

Apply my getattr() fix, and the test passes.

I'll commit on branch 3.4 and merge to default.
Date User Action Args
2014-11-21 01:37:31gwardsetrecipients: + gward, vstinner, serhiy.storchaka
2014-11-21 01:37:31gwardsetmessageid: <>
2014-11-21 01:37:30gwardlinkissue21775 messages
2014-11-21 01:37:29gwardcreate