From f51738b10e83d0adf404ca989970a5b44700d685 Mon Sep 17 00:00:00 2001 From: Ronald Oussoren Date: Fri, 6 May 2011 10:23:04 +0200 Subject: [PATCH] Fix for issue 10684: Folders get deleted when trying to change case with shutil.move (case insensitive file systems only) --- Lib/shutil.py | 8 +++++++- Lib/test/test_shutil.py | 18 ++++++++++++++++++ Misc/NEWS | 4 ++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Lib/shutil.py b/Lib/shutil.py index 4862ae6a13e..781840dbcdd 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -260,12 +260,18 @@ def move(src, dst): """ real_dst = dst if os.path.isdir(dst): + if _samefile(src, dst): + # We might be on a case insensitive filesystem, + # perform the rename anyway. + os.rename(src, dst) + return + real_dst = os.path.join(dst, _basename(src)) if os.path.exists(real_dst): raise Error("Destination path '%s' already exists" % real_dst) try: os.rename(src, real_dst) - except OSError: + except OSError as exc: if os.path.isdir(src): if _destinsrc(src, dst): raise Error("Cannot move a directory '%s' into itself '%s'." % (src, dst)) diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py index becd91c56fe..85473513d16 100644 --- a/Lib/test/test_shutil.py +++ b/Lib/test/test_shutil.py @@ -522,6 +522,24 @@ class TestCopyFile(unittest.TestCase): self.assertTrue(srcfile._exited_with[0] is None) self.assertTrue(srcfile._raised) + def test_move_dir_caseinsensitive(self): + # Renames a folder to the same name + # but a different case. + + self.src_dir = tempfile.mkdtemp() + dst_dir = os.path.join( + os.path.dirname(self.src_dir), + os.path.basename(self.src_dir).upper()) + self.assertNotEqual(self.src_dir, dst_dir) + + try: + shutil.move(self.src_dir, dst_dir) + self.assertTrue(os.path.isdir(dst_dir)) + finally: + if os.path.exists(dst_dir): + os.rmdir(dst_dir) + + def test_main(): support.run_unittest(TestShutil, TestMove, TestCopyFile) diff --git a/Misc/NEWS b/Misc/NEWS index 887861261fd..1c6fedca905 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -66,6 +66,10 @@ Core and Builtins Library ------- +- Issue #10684: shutil.move used to delete a folder on case insensitive + filesystems when the source and destination name where the same except + for the case. + - Issue #11277: mmap.mmap() calls fcntl(fd, F_FULLFSYNC) on Mac OS X to get around a mmap bug with sparse files. Patch written by Steffen Daode Nurpmeso.