]> granicus.if.org Git - python/commitdiff
Issue #14662: Prevent shutil failures on OS X when destination does not
authorNed Deily <nad@acm.org>
Fri, 11 May 2012 00:21:23 +0000 (17:21 -0700)
committerNed Deily <nad@acm.org>
Fri, 11 May 2012 00:21:23 +0000 (17:21 -0700)
support chflag operations.  (Patch by Hynek Schlawack)

Lib/shutil.py
Lib/test/test_shutil.py
Misc/NEWS

index d1b1af3246af27de78aaf30a78f6ba78b42eb940..2d7a506b2ea2bedce111f03bce39436146094257 100644 (file)
@@ -118,8 +118,10 @@ def copystat(src, dst):
         try:
             os.chflags(dst, st.st_flags)
         except OSError as why:
-            if (not hasattr(errno, 'EOPNOTSUPP') or
-                why.errno != errno.EOPNOTSUPP):
+            for err in 'EOPNOTSUPP', 'ENOTSUP':
+                if hasattr(errno, err) and why.errno == getattr(errno, err):
+                    break
+            else:
                 raise
 
 def copy(src, dst):
index e2310e26f3ce60b54b75acdc450c24c35d893b5c..1a016b90ddf113341f3c89caa1def89d1860e140 100644 (file)
@@ -8,6 +8,7 @@ import stat
 import os
 import os.path
 import functools
+import errno
 from test import support
 from test.support import TESTFN
 from os.path import splitdrive
@@ -307,6 +308,35 @@ class TestShutil(unittest.TestCase):
         finally:
             shutil.rmtree(TESTFN, ignore_errors=True)
 
+    @unittest.skipUnless(hasattr(os, 'chflags') and
+                         hasattr(errno, 'EOPNOTSUPP') and
+                         hasattr(errno, 'ENOTSUP'),
+                         "requires os.chflags, EOPNOTSUPP & ENOTSUP")
+    def test_copystat_handles_harmless_chflags_errors(self):
+        tmpdir = self.mkdtemp()
+        file1 = os.path.join(tmpdir, 'file1')
+        file2 = os.path.join(tmpdir, 'file2')
+        self.write_file(file1, 'xxx')
+        self.write_file(file2, 'xxx')
+
+        def make_chflags_raiser(err):
+            ex = OSError()
+
+            def _chflags_raiser(path, flags):
+                ex.errno = err
+                raise ex
+            return _chflags_raiser
+        old_chflags = os.chflags
+        try:
+            for err in errno.EOPNOTSUPP, errno.ENOTSUP:
+                os.chflags = make_chflags_raiser(err)
+                shutil.copystat(file1, file2)
+            # assert others errors break it
+            os.chflags = make_chflags_raiser(errno.EOPNOTSUPP + errno.ENOTSUP)
+            self.assertRaises(OSError, shutil.copystat, file1, file2)
+        finally:
+            os.chflags = old_chflags
+
     @support.skip_unless_symlink
     def test_dont_copy_file_onto_symlink_to_itself(self):
         # bug 851123.
index 42fc741933053ef42c00cc9733ac1e4c7d5dfbab..10b5453b03fc932ad8308f52fb263a4c8ecfb85d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -63,6 +63,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #14662: Prevent shutil failures on OS X when destination does not
+  support chflag operations.  Patch by Hynek Schlawack.
+
 - Issue #14157: Fix time.strptime failing without a year on February 29th.
   Patch by Hynek Schlawack.