cpython: 26da299ca88e (original) (raw)
Mercurial > cpython
changeset 69869:26da299ca88e 3.1
Fix for issue 10684: Folders get deleted when trying to change case with shutil.move (case insensitive file systems only) [#10684]
Ronald Oussoren ronaldoussoren@mac.com | |
---|---|
date | Fri, 06 May 2011 10:23:04 +0200 |
parents | 3aa51217492c |
children | f5aa0e52dd81 252451fef13f |
files | Lib/shutil.py Lib/test/test_shutil.py Misc/NEWS |
diffstat | 3 files changed, 29 insertions(+), 1 deletions(-)[+] [-] Lib/shutil.py 8 Lib/test/test_shutil.py 18 Misc/NEWS 4 |
line wrap: on
line diff
--- 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):[](#l1.7)
# We might be on a case insensitive filesystem,[](#l1.8)
# perform the rename anyway.[](#l1.9)
os.rename(src, dst)[](#l1.10)
return[](#l1.11)
+ 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 as exc: if os.path.isdir(src): if _destinsrc(src, dst): raise Error("Cannot move a directory '%s' into itself '%s'." % (src, dst))
--- 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[](#l2.8)
# but a different case.[](#l2.9)
self.src_dir = tempfile.mkdtemp()[](#l2.11)
dst_dir = os.path.join([](#l2.12)
os.path.dirname(self.src_dir),[](#l2.13)
os.path.basename(self.src_dir).upper())[](#l2.14)
self.assertNotEqual(self.src_dir, dst_dir)[](#l2.15)
try:[](#l2.17)
shutil.move(self.src_dir, dst_dir)[](#l2.18)
self.assertTrue(os.path.isdir(dst_dir))[](#l2.19)
finally:[](#l2.20)
if os.path.exists(dst_dir):[](#l2.21)
os.rmdir(dst_dir)[](#l2.22)
+ + def test_main(): support.run_unittest(TestShutil, TestMove, TestCopyFile)
--- 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