]> granicus.if.org Git - python/commitdiff
shutil.move() will raise an exception when trying to move a directory into
authorBrett Cannon <bcannon@gmail.com>
Sat, 19 Jun 2004 21:11:35 +0000 (21:11 +0000)
committerBrett Cannon <bcannon@gmail.com>
Sat, 19 Jun 2004 21:11:35 +0000 (21:11 +0000)
itself.

Closes bug #919012  .  Thanks Johannes Gijsbers.

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

index 5341786d528d6487be7d282fdcc6bdd1f90cdcc9..10b7a277674a3e34cf3e4e0e8bcb14e62b9dc27f 100644 (file)
@@ -8,6 +8,7 @@ import os
 import sys
 import stat
 import exceptions
+from os.path import abspath
 
 __all__ = ["copyfileobj","copyfile","copymode","copystat","copy","copy2",
            "copytree","move","rmtree","Error"]
@@ -164,8 +165,13 @@ def move(src, dst):
         os.rename(src, dst)
     except OSError:
         if os.path.isdir(src):
+            if destinsrc(src, dst):
+                raise Error, "Cannot move a directory '%s' into itself '%s'." % (src, dst)
             copytree(src, dst, symlinks=True)
             rmtree(src)
         else:
             copy2(src,dst)
             os.unlink(src)
+
+def destinsrc(src, dst):
+    return abspath(dst).startswith(abspath(src))
index 3a39ec9056f4c0fa23e5a934cc44471d0a72314f..05f34d84665689252c85122dee1495bc178e8df9 100644 (file)
@@ -3,6 +3,8 @@
 import unittest
 import shutil
 import tempfile
+import os
+import os.path
 from test import test_support
 
 class TestShutil(unittest.TestCase):
@@ -12,11 +14,21 @@ class TestShutil(unittest.TestCase):
         self.assertRaises(OSError, shutil.rmtree, filename)
         self.assertEqual(shutil.rmtree(filename, True), None)
 
+    def test_dont_move_dir_in_itself(self):
+        src_dir = tempfile.mkdtemp()
+        try:
+            dst = os.path.join(src_dir, 'foo')
+            self.assertRaises(shutil.Error, shutil.move, src_dir, dst)
+        finally:
+            try:
+                os.rmdir(src_dir)
+            except:
+                pass
+
+
 
-\f
 def test_main():
     test_support.run_unittest(TestShutil)
 
-
 if __name__ == '__main__':
     test_main()
index 61f857b46ca0e73dc03611bb6a7205e990561df6..3347c6f92d0623da403e73bb6f1c42d0b976e613 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -205,6 +205,7 @@ Christos Georgiou
 Ben Gertzfield
 Dinu Gherman
 Jonathan Giddy
+Johannes Gijsbers
 Michael Gilfix
 Chris Gonnerman
 David Goodger
index 6d6dfe9bbaadb1ff05ccefba3de339aae96b9f70..b3096ca1ebe600662c1bbc62d8bc2948bf6f7bc8 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -340,6 +340,9 @@ Extension modules
 Library
 -------
 
+- Bug #919012: shutil.move() will not try to move a directory into itself.
+  Thanks Johannes Gijsbers.
+
 - Bug #934282: pydoc.stripid() is now case-insensitive.  Thanks Robin Becker.
 
 - Bug #823209:  cmath.log() now takes an optional base argument so that its