mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Bug #1048941: shutil.rmtree error handling was always broken
Rewrite rmtree again, this time without os.walk(). Error handling had been broken since Python 2.3, and the os.walk() version inherited this.
This commit is contained in:
parent
57341c37c9
commit
ef5ffc4765
2 changed files with 61 additions and 30 deletions
|
@ -1,8 +1,10 @@
|
|||
|
||||
# Copyright (C) 2003 Python Software Foundation
|
||||
|
||||
import unittest
|
||||
import shutil
|
||||
import tempfile
|
||||
import stat
|
||||
import os
|
||||
import os.path
|
||||
from test import test_support
|
||||
|
@ -13,8 +15,32 @@ class TestShutil(unittest.TestCase):
|
|||
# filename is guaranteed not to exist
|
||||
filename = tempfile.mktemp()
|
||||
self.assertRaises(OSError, shutil.rmtree, filename)
|
||||
self.assertEqual(shutil.rmtree(filename, True), None)
|
||||
shutil.rmtree(filename, False, lambda func, arg, exc: None)
|
||||
|
||||
if hasattr(os, 'chmod'):
|
||||
def test_on_error(self):
|
||||
self.errorState = 0
|
||||
os.mkdir(TESTFN)
|
||||
f = open(os.path.join(TESTFN, 'a'), 'w')
|
||||
f.close()
|
||||
# Make TESTFN unwritable.
|
||||
os.chmod(TESTFN, stat.S_IRUSR)
|
||||
|
||||
shutil.rmtree(TESTFN, onerror=self.check_args_to_onerror)
|
||||
|
||||
# Make TESTFN writable again.
|
||||
os.chmod(TESTFN, stat.S_IRWXU)
|
||||
shutil.rmtree(TESTFN)
|
||||
|
||||
def check_args_to_onerror(self, func, arg, exc):
|
||||
if self.errorState == 0:
|
||||
self.assertEqual(func, os.remove)
|
||||
self.assertEqual(arg, os.path.join(TESTFN, 'a'))
|
||||
self.assertEqual(exc[0], OSError)
|
||||
self.errorState = 1
|
||||
else:
|
||||
self.assertEqual(func, os.rmdir)
|
||||
self.assertEqual(arg, TESTFN)
|
||||
self.assertEqual(exc[0], OSError)
|
||||
|
||||
def test_rmtree_dont_delete_file(self):
|
||||
# When called on a file instead of a directory, don't delete it.
|
||||
|
@ -63,7 +89,6 @@ class TestShutil(unittest.TestCase):
|
|||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
def test_main():
|
||||
test_support.run_unittest(TestShutil)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue