Index: shutil.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/shutil.py,v retrieving revision 1.28 diff -u -r1.28 shutil.py --- shutil.py 23 Feb 2003 21:36:32 -0000 1.28 +++ shutil.py 5 Jun 2004 14:04:25 -0000 @@ -8,6 +8,7 @@ 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 @@ os.rename(src, dst) except OSError: if os.path.isdir(src): + if is_destination_in_source(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 is_destination_in_source(self, src, dst): + return abspath(dst).startswith(abspath(src)) Index: test/test_shutil.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/test/test_shutil.py,v retrieving revision 1.2 diff -u -r1.2 test_shutil.py --- test/test_shutil.py 1 May 2003 17:45:49 -0000 1.2 +++ test/test_shutil.py 5 Jun 2004 14:04:26 -0000 @@ -3,6 +3,7 @@ import unittest import shutil import tempfile +from os import path from test import test_support class TestShutil(unittest.TestCase): @@ -12,11 +13,14 @@ 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() + dst = path.join(src_dir, 'foo') + self.assertRaises(shutil.Error, shutil.move, src_dir, dst) def test_main(): test_support.run_unittest(TestShutil) - if __name__ == '__main__': test_main()