[ python-Bugs-1525866 ] Bug in shutil.copytree on Windows

SourceForge.net noreply at sourceforge.net
Fri Jul 28 16:09:13 CEST 2006


Bugs item #1525866, was opened at 2006-07-20 13:00
Message generated for change (Comment added) made by mjfoord
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1525866&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: Python 2.5
Status: Open
Resolution: None
Priority: 5
Submitted By: Mike Foord (mjfoord)
Assigned to: Nobody/Anonymous (nobody)
Summary: Bug in shutil.copytree on Windows

Initial Comment:
The problem is that the call to 'copystat(src, dst)'
was added to the shutil.copytree function, in svn
r38363 probably.  It will fail always on Windows, since
os.utime does not work on directories.

I guess that a patch similar to this one should fix it:

Index: shutil.py
===================================================================
--- shutil.py	(Revision 50710)
+++ shutil.py	(Arbeitskopie)
@@ -127,7 +127,12 @@
         # continue with other files
         except Error, err:
             errors.extend(err.args[0])
-    copystat(src, dst)
+    try:
+        copystat(src, dst)
+    except WindowsError:
+        pass
+    except OSError, err:
+        errors.extend(err.args[0])
     if errors:
         raise Error, errors

----------------------------------------------------------------------

>Comment By: Mike Foord (mjfoord)
Date: 2006-07-28 14:09

Message:
Logged In: YES 
user_id=1123892

The following should work as a test method for shutil.copytree

(Passes on my box against a patched version of shutil)

    def test_copytree_simple(self):
        src_dir = tempfile.mkdtemp()
        dst_dir = os.path.join(tempfile.mkdtemp(),
'destination')
        open(os.path.join(src_dir, 'test.txt'),
'w').write('123')
        os.mkdir(os.path.join(src_dir, 'test_dir'))
        open(os.path.join(src_dir, 'test_dir', 'test.txt'),
'w').write('456')
        #
        def testStat(src, dst):
            st_src = os.stat(src)
            st_dst = os.stat(dst)
            if hasattr(os, 'utime'):
                self.assertEqual((st_src.st_atime,
st_src.st_mtime), (st_dst.st_atime, st_dst.st_mtime))
            if hasattr(os, 'chmod'):
                # Should be equal anyway, should we change
permissions on one of the source files ?
               
self.assertEqual(stat.S_IMODE(st_src.st_mode),
stat.S_IMODE(st_dst.st_mode))
        #
        try:
            shutil.copytree(src_dir, dst_dir)
           
self.assertTrue(os.path.isfile(os.path.join(dst_dir,
'test.txt')))
           
self.assertTrue(os.path.isdir(os.path.join(dst_dir,
'test_dir')))
           
self.assertTrue(os.path.isfile(os.path.join(dst_dir,
'test_dir', 'test.txt')))
            self.assertEqual(open(os.path.join(dst_dir,
'test.txt')).read(), '123')
            self.assertEqual(open(os.path.join(dst_dir,
'test_dir', 'test.txt')).read(), '456')
           
        finally:
            try:
                os.remove(os.path.join(src_dir, 'test.txt'))
                os.remove(os.path.join(dst_dir, 'test.txt'))
                os.remove(os.path.join(src_dir, 'test_dir',
'test.txt'))
                os.remove(os.path.join(dst_dir, 'test_dir',
'test.txt'))
                os.removedirs(src_dir)
                os.removedirs(dst_dir)
            except:
                pass

Can turn the above into a patch tonight if needed.

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2006-07-20 16:14

Message:
Logged In: YES 
user_id=21627

Can you also come up with a patch to the test suite?

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1525866&group_id=5470


More information about the Python-bugs-list mailing list