]> granicus.if.org Git - python/commitdiff
Issue #12464: tempfile.TemporaryDirectory.cleanup() should not follow symlinks:
authorCharles-François Natali <neologix@free.fr>
Fri, 29 Jul 2011 16:59:24 +0000 (18:59 +0200)
committerCharles-François Natali <neologix@free.fr>
Fri, 29 Jul 2011 16:59:24 +0000 (18:59 +0200)
fix it. Patch by Petri Lehtinen.

Lib/tempfile.py
Lib/test/test_tempfile.py
Misc/ACKS
Misc/NEWS

index b28d91f87ebc5803a5c9ffa1f53eca2bbdd225c9..48b77a87e21620bdee9a12c0b8a402b050a2a863 100644 (file)
@@ -661,6 +661,7 @@ class TemporaryDirectory(object):
     _listdir = staticmethod(_os.listdir)
     _path_join = staticmethod(_os.path.join)
     _isdir = staticmethod(_os.path.isdir)
+    _islink = staticmethod(_os.path.islink)
     _remove = staticmethod(_os.remove)
     _rmdir = staticmethod(_os.rmdir)
     _os_error = _os.error
@@ -672,7 +673,7 @@ class TemporaryDirectory(object):
         for name in self._listdir(path):
             fullname = self._path_join(path, name)
             try:
-                isdir = self._isdir(fullname)
+                isdir = self._isdir(fullname) and not self._islink(fullname)
             except self._os_error:
                 isdir = False
             if isdir:
index 2d298857b201fa785aee2229cfc28e436f2867f7..f7f5bdadbd38e935ad2f80602d3dbf8de084c816 100644 (file)
@@ -964,6 +964,27 @@ class test_TemporaryDirectory(TC):
         finally:
             os.rmdir(dir)
 
+    @support.skip_unless_symlink
+    def test_cleanup_with_symlink_to_a_directory(self):
+        # cleanup() should not follow symlinks to directories (issue #12464)
+        d1 = self.do_create()
+        d2 = self.do_create()
+
+        # Symlink d1/foo -> d2
+        os.symlink(d2.name, os.path.join(d1.name, "foo"))
+
+        # This call to cleanup() should not follow the "foo" symlink
+        d1.cleanup()
+
+        self.assertFalse(os.path.exists(d1.name),
+                         "TemporaryDirectory %s exists after cleanup" % d1.name)
+        self.assertTrue(os.path.exists(d2.name),
+                        "Directory pointed to by a symlink was deleted")
+        self.assertEqual(os.listdir(d2.name), ['test.txt'],
+                         "Contents of the directory pointed to by a symlink "
+                         "were deleted")
+        d2.cleanup()
+
     @support.cpython_only
     def test_del_on_collection(self):
         # A TemporaryDirectory is deleted when garbage collected
index 820206acf4f5b50b30ea26ee911cdefceb2233ce..db451d71904804a565706f9d2f70568e4cad85a8 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -526,6 +526,7 @@ Vincent Legoll
 Kip Lehman
 Joerg Lehmann
 Robert Lehmann
+Petri Lehtinen
 Luke Kenneth Casson Leighton
 Marc-Andre Lemburg
 John Lenton
index 1021df73d572d3b36062c14a9ca21915d76048e9..b080316f20ba1456a633f1f9b02da41dbed987cd 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -41,6 +41,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #12464: tempfile.TemporaryDirectory.cleanup() should not follow
+  symlinks: fix it. Patch by Petri Lehtinen.
+
 - Issue #8887: "pydoc somebuiltin.somemethod" (or help('somebuiltin.somemethod')
   in Python code) now finds the doc of the method.