New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
shutil.rmtree fails on symlink, after deleting contents #46010
Comments
When using rmtree with a symlink to a directory as path, it will first Expected behaviour: The function should only raise an exception. If the current behaviour is indeed wanted, it should a least be documented. |
Please try to include stack trace in bug reports. I reproduced the error marvin:tmp$ ls -l dirlink testdir testdir: shutil.rmtree('dirlink') produces: ---------- Traceback (most recent call last):
File "t.py", line 4, in <module>
shutil.rmtree('dirlink')
File "/localhome/raghu/localwork/cpython/python/Lib/shutil.py", line
194, in rmtree
onerror(os.rmdir, path, sys.exc_info())
File "/localhome/raghu/localwork/cpython/python/Lib/shutil.py", line
192, in rmtree
os.rmdir(path)
OSError: [Errno 20] Not a directory: 'dirlink' While we are removing the contents of the target directory as expected, |
This is not what I expected at all. I expected the function to fail, The current behavior is in my opinion inconsistent; if the symlink is As I said, if the current behavior is apparently expected, it should be |
I agree with Tesiph, more useful behavior would be to raise an error |
I am ok with disallowing symlinks in rmtree() because if it were to be The attached patch would raise error if symbolic link is passed to |
Thanks for the patch. I think it should raise IOError, not ValueError, |
Index: Lib/shutil.py --- Lib/shutil.py (revision 59581)
+++ Lib/shutil.py (working copy)
@@ -156,6 +156,16 @@
elif onerror is None:
def onerror(*args):
raise
+
+ try:
+ if os.path.islink(path):
+ if ignore_errors:
+ return
+ else:
+ raise IOError('path can not be symbolic link')
+ except IOError, err:
+ onerror(os.path.islink, path, sys.exc_info())
+
names = []
try:
names = os.listdir(path) How does this look? The error handling is slightly different for this On Dec 20, 2007 12:43 PM, Guido van Rossum <report@bugs.python.org> wrote:
|
Committed a variant of the last patch in r60139; it now raises OSError |
If rmtree() always returns in case of symbolic links (as it is |
Guido explicitly said "it should raise IOError, not ValueError, |
Sure. I am ok with using 'onerror'. The point I am trying to make is |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: