@@ -1826,17 +1826,28 @@ def test_modes(self):
1826
1826
1827
1827
@unittest .skipUnless (hasattr (os , 'chflags' ), 'requires os.lchflags' )
1828
1828
def test_flags (self ):
1829
- flags = stat .UF_IMMUTABLE | stat .UF_NOUNLINK
1830
- d = self .do_create (recurse = 3 , dirs = 2 , files = 2 )
1831
- with d :
1832
- # Change files and directories flags recursively.
1833
- for root , dirs , files in os .walk (d .name , topdown = False ):
1829
+ def chflags_recursively (name , flags ):
1830
+ for root , dirs , files in os .walk (name , topdown = False ):
1834
1831
for name in files :
1835
1832
os .chflags (os .path .join (root , name ), flags )
1836
1833
os .chflags (root , flags )
1837
- d .cleanup ()
1838
- self .assertFalse (os .path .exists (d .name ))
1839
1834
1835
+ d = self .do_create (recurse = 3 , dirs = 2 , files = 2 )
1836
+ try :
1837
+ with d :
1838
+ chflags_recursively (d .name , stat .UF_IMMUTABLE | stat .UF_NOUNLINK )
1839
+ d .cleanup ()
1840
+
1841
+ self .assertFalse (os .path .exists (d .name ))
1842
+
1843
+ finally :
1844
+ # Even if test fails, make a best-effort attempt at not leaving
1845
+ # behind NOUNLINK files, because they require manual removal,
1846
+ # which becomes tiresome during development.
1847
+ try :
1848
+ chflags_recursively (d .name , 0 )
1849
+ except Exception :
1850
+ pass # we tried
1840
1851
1841
1852
if __name__ == "__main__" :
1842
1853
unittest .main ()
0 commit comments