]> granicus.if.org Git - python/commitdiff
Merged revisions 69100 via svnmerge from
authorAntoine Pitrou <solipsis@pitrou.net>
Thu, 29 Jan 2009 20:26:59 +0000 (20:26 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Thu, 29 Jan 2009 20:26:59 +0000 (20:26 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r69100 | antoine.pitrou | 2009-01-29 21:19:34 +0100 (jeu., 29 janv. 2009) | 5 lines

  Issue #2047: shutil.move() could believe that its destination path was
  inside its source path if it began with the same letters (e.g. "src" vs.
  "src.new").
........

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

index 9a5f78a50dedc6fb73aecefad084b245c0bf24f5..d884d0ac998b4a6a34a5b7cf0453e3bf11b3a759 100644 (file)
@@ -265,4 +265,10 @@ def move(src, dst):
             os.unlink(src)
 
 def destinsrc(src, dst):
-    return abspath(dst).startswith(abspath(src))
+    src = abspath(src)
+    dst = abspath(dst)
+    if not src.endswith(os.path.sep):
+        src += os.path.sep
+    if not dst.endswith(os.path.sep):
+        dst += os.path.sep
+    return dst.startswith(src)
index ad60a44bb32ba91fb60ce5483bcf6f32637c679c..c7dd1b3fd7e275c40dc2815a6a180e805dcc9ac8 100644 (file)
@@ -340,7 +340,29 @@ class TestMove(unittest.TestCase):
         dst = os.path.join(self.src_dir, "bar")
         self.assertRaises(shutil.Error, shutil.move, self.src_dir, dst)
 
+    def test_destinsrc_false_negative(self):
+        os.mkdir(TESTFN)
+        try:
+            for src, dst in [('srcdir', 'srcdir/dest')]:
+                src = os.path.join(TESTFN, src)
+                dst = os.path.join(TESTFN, dst)
+                self.assert_(shutil.destinsrc(src, dst),
+                             msg='destinsrc() wrongly concluded that '
+                             'dst (%s) is not in src (%s)' % (dst, src))
+        finally:
+            shutil.rmtree(TESTFN, ignore_errors=True)
 
+    def test_destinsrc_false_positive(self):
+        os.mkdir(TESTFN)
+        try:
+            for src, dst in [('srcdir', 'src/dest'), ('srcdir', 'srcdir.new')]:
+                src = os.path.join(TESTFN, src)
+                dst = os.path.join(TESTFN, dst)
+                self.failIf(shutil.destinsrc(src, dst),
+                            msg='destinsrc() wrongly concluded that '
+                            'dst (%s) is in src (%s)' % (dst, src))
+        finally:
+            shutil.rmtree(TESTFN, ignore_errors=True)
 
 def test_main():
     support.run_unittest(TestShutil, TestMove)
index 3a7fec51d3ba91f9d857da9b20992f8bcf41befe..c2b4ed8be6fd76f2ce254cf37b64abb394bd06d8 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -147,6 +147,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #2047: shutil.move() could believe that its destination path was
+  inside its source path if it began with the same letters (e.g. "src" vs.
+  "src.new").
+
 - Added the ttk module. See issue #2983: Ttk support for Tkinter.
 
 - Removed isSequenceType(), isMappingType, and isNumberType() from the