Skip to content

Commit b8024c7

Browse files
committed
tempfile tests: try not to leave NOUNLINK files behind
During development, it becomes tiresome to have to manually clean up these files in case of unrelated TemporaryDirectory breakage.
1 parent 0563be2 commit b8024c7

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

Lib/test/test_tempfile.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,17 +1826,28 @@ def test_modes(self):
18261826

18271827
@unittest.skipUnless(hasattr(os, 'chflags'), 'requires os.lchflags')
18281828
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):
18341831
for name in files:
18351832
os.chflags(os.path.join(root, name), flags)
18361833
os.chflags(root, flags)
1837-
d.cleanup()
1838-
self.assertFalse(os.path.exists(d.name))
18391834

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
18401851

18411852
if __name__ == "__main__":
18421853
unittest.main()

0 commit comments

Comments
 (0)