@@ -1910,6 +1910,87 @@ def hook(path, *, dir_fd=None):
19101910 self .assertTrue (os .path .exists (target_file ))
19111911 self .assertEqual (os .stat (target_file ).st_mode , target_mode )
19121912
1913+ def _make_unopenable_subdir (self ):
1914+ # Create the fullowing structure:
1915+ # work/ - top-level
1916+ # <d>/ - tempdir to clean up (random name)
1917+ # sub/ - an unopenable dir
1918+ # subsub/
1919+ # file
1920+ # Return:
1921+ # - `work` name (str)
1922+ # - `d` (TemporaryDirectory)
1923+ # - `subsub` stat result
1924+ work = self .enterContext (os_helper .temp_dir ())
1925+ d = tempfile .TemporaryDirectory (dir = work )
1926+ sub = os .path .join (d .name , 'sub' )
1927+ subsub = os .path .join (sub , 'subsub' )
1928+ os .mkdir (sub )
1929+ os .mkdir (subsub )
1930+ subsub_stat = os .lstat (subsub )
1931+ with open (os .path .join (subsub , 'file' ), 'w' ) as f :
1932+ f .write ("rm me pls" )
1933+ os .chmod (sub , 0 )
1934+ # If the test fails, make sure everything can still be removed.
1935+ self .addCleanup (d .cleanup )
1936+ @self .addCleanup
1937+ def _clean ():
1938+ if os .path .isdir (sub ):
1939+ os .chmod (sub , 0o700 )
1940+ return work , d , subsub_stat
1941+
1942+ @os_helper .skip_unless_working_chmod
1943+ @support .requires_non_root_user
1944+ @unittest .skipUnless (tempfile ._rmtree_use_dir_fd
1945+ and shutil .rmtree .avoids_symlink_attacks ,
1946+ 'requires the fd based cleanup' )
1947+ def test_cleanup_nested_toplevel_error (self ):
1948+ # rmtree() reports errors relative to the correct parent directory
1949+ work , d , subsub_stat = self ._make_unopenable_subdir ()
1950+ # An unrelated directory with the same name; should not be touched
1951+ unrelated = os .path .join (work , 'sub' )
1952+ os .mkdir (unrelated )
1953+
1954+ rmdir = os .rmdir
1955+ failed_path = None
1956+ def failing_rmdir (path , * , dir_fd = None ):
1957+ nonlocal failed_path
1958+ if path == 'sub' and dir_fd is not None and failed_path is None :
1959+ failed_path = path
1960+ raise PermissionError (errno .EPERM , 'simulated' , path )
1961+ return rmdir (path , dir_fd = dir_fd )
1962+ with os_helper .change_cwd (work ), mock .patch ('os.rmdir' , failing_rmdir ):
1963+ d .cleanup ()
1964+ self .assertEqual (failed_path , 'sub' )
1965+ self .assertFalse (os .path .exists (d .name ))
1966+ self .assertTrue (os .path .exists (unrelated ))
1967+
1968+ @os_helper .skip_unless_working_chmod
1969+ @support .requires_non_root_user
1970+ @unittest .skipUnless (tempfile ._rmtree_use_dir_fd
1971+ and shutil .rmtree .avoids_symlink_attacks ,
1972+ 'requires the fd based cleanup' )
1973+ def test_cleanup_nested_scandir_error (self ):
1974+ # A scandir() error is reported relative to the parent directory.
1975+ work , d , subsub_stat = self ._make_unopenable_subdir ()
1976+
1977+ scandir = os .scandir
1978+ failed = False
1979+ def failing_scandir (path = '.' ):
1980+ nonlocal failed
1981+ if (
1982+ not failed
1983+ and isinstance (path , int )
1984+ and os .path .samestat (os .fstat (path ), subsub_stat )
1985+ ):
1986+ failed = True
1987+ raise PermissionError (errno .EACCES , 'simulated' )
1988+ return scandir (path )
1989+ with os_helper .change_cwd (work ), mock .patch ('os.scandir' , failing_scandir ):
1990+ d .cleanup ()
1991+ self .assertTrue (failed )
1992+ self .assertFalse (os .path .exists (d .name ))
1993+
19131994 @support .cpython_only
19141995 def test_del_on_collection (self ):
19151996 # A TemporaryDirectory is deleted when garbage collected
0 commit comments