#1669: don't allow shutil.rmtree() to be called on a symlink.
diff --git a/Lib/shutil.py b/Lib/shutil.py
index f00f9b7..0a40c57 100644
--- a/Lib/shutil.py
+++ b/Lib/shutil.py
@@ -156,6 +156,14 @@
elif onerror is None:
def onerror(*args):
raise
+ try:
+ if os.path.islink(path):
+ # symlinks to directories are forbidden, see bug #1669
+ raise OSError("Cannot call rmtree on a symbolic link")
+ except OSError:
+ onerror(os.path.islink, path, sys.exc_info())
+ # can't continue even if onerror hook returns
+ return
names = []
try:
names = os.listdir(path)
diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py
index ee15d0e..b63031e 100644
--- a/Lib/test/test_shutil.py
+++ b/Lib/test/test_shutil.py
@@ -149,6 +149,20 @@
except OSError:
pass
+ def test_rmtree_on_symlink(self):
+ # bug 1669.
+ os.mkdir(TESTFN)
+ try:
+ src = os.path.join(TESTFN, 'cheese')
+ dst = os.path.join(TESTFN, 'shop')
+ os.mkdir(src)
+ os.symlink(src, dst)
+ self.assertRaises(OSError, shutil.rmtree, dst)
+ finally:
+ shutil.rmtree(TESTFN, ignore_errors=True)
+
+
+
def test_main():
test_support.run_unittest(TestShutil)