bpo-43153: Don't mask PermissionError with NotADirectoryError during tempdirectory cleanup (GH-29940)

Co-authored-by: andrei kulakov <andrei.avk@gmail.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Ken Jin 2023-12-05 21:30:59 +08:00 committed by GitHub
parent 5aa317e4ca
commit 8cdfee1bb9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 2 deletions

View file

@ -1641,6 +1641,17 @@ class TestTemporaryDirectory(BaseTestCase):
temp_path.exists(),
f"TemporaryDirectory {temp_path!s} exists after cleanup")
@unittest.skipUnless(os.name == "nt", "Only on Windows.")
def test_explicit_cleanup_correct_error(self):
with tempfile.TemporaryDirectory() as working_dir:
temp_dir = self.do_create(dir=working_dir)
with open(os.path.join(temp_dir.name, "example.txt"), 'wb'):
# Previously raised NotADirectoryError on some OSes
# (e.g. Windows). See bpo-43153.
with self.assertRaises(PermissionError):
temp_dir.cleanup()
@os_helper.skip_unless_symlink
def test_cleanup_with_symlink_to_a_directory(self):
# cleanup() should not follow symlinks to directories (issue #12464)