diff options
author | Nick Coghlan <ncoghlan@gmail.com> | 2010-12-12 15:24:21 (GMT) |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2010-12-12 15:24:21 (GMT) |
commit | 6b22f3fa172c61c76077265cbf742bbcb67d7af2 (patch) | |
tree | 73cb339eca119bb37a20b93ae26431d923fcbfbd /Lib/test | |
parent | 0e65cf0b6ab397f3f67c2b516af7b9fba05604bc (diff) | |
download | cpython-6b22f3fa172c61c76077265cbf742bbcb67d7af2.zip cpython-6b22f3fa172c61c76077265cbf742bbcb67d7af2.tar.gz cpython-6b22f3fa172c61c76077265cbf742bbcb67d7af2.tar.bz2 |
Issue #10188 (partial resolution): tidy up some behaviour in the new tempfile.TemporaryDirectory context manager
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/support.py | 3 | ||||
-rw-r--r-- | Lib/test/test_tempfile.py | 49 |
2 files changed, 46 insertions, 6 deletions
diff --git a/Lib/test/support.py b/Lib/test/support.py index 2fd019a..27efd37 100644 --- a/Lib/test/support.py +++ b/Lib/test/support.py @@ -874,6 +874,9 @@ def captured_output(stream_name): def captured_stdout(): return captured_output("stdout") +def captured_stderr(): + return captured_output("stderr") + def captured_stdin(): return captured_output("stdin") diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py index 802db1f..eebf78f 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -925,6 +925,13 @@ class test_TemporaryDirectory(TC): f.write(b"Hello world!") return tmp + def test_mkdtemp_failure(self): + # Check no additional exception if mkdtemp fails + # Previously would raise AttributeError instead + # (noted as part of Issue #10888) + #with self.assertRaises(os.error): + tempfile.TemporaryDirectory(prefix="[]<>?*!:") + def test_explicit_cleanup(self): # A TemporaryDirectory is deleted when cleaned up dir = tempfile.mkdtemp() @@ -955,20 +962,50 @@ class test_TemporaryDirectory(TC): def test_del_on_shutdown(self): # A TemporaryDirectory may be cleaned up during shutdown # Make sure it works with the relevant modules nulled out - dir = tempfile.mkdtemp() - try: + with self.do_create() as dir: d = self.do_create(dir=dir) # Mimic the nulling out of modules that # occurs during system shutdown modules = [os, os.path] if has_stat: modules.append(stat) - with NulledModules(*modules): - d.cleanup() + # Currently broken, so suppress the warning + # that is otherwise emitted on stdout + with support.captured_stderr() as err: + with NulledModules(*modules): + d.cleanup() + # Currently broken, so stop spurious exception by + # indicating the object has already been closed + d._closed = True + # And this assert will fail, as expected by the + # unittest decorator... self.assertFalse(os.path.exists(d.name), "TemporaryDirectory %s exists after cleanup" % d.name) - finally: - os.rmdir(dir) + + def test_warnings_on_cleanup(self): + # Two kinds of warning on shutdown + # Issue 10888: may write to stderr if modules are nulled out + # ResourceWarning will be triggered by __del__ + with self.do_create() as dir: + d = self.do_create(dir=dir) + + #Check for the Issue 10888 message + modules = [os, os.path] + if has_stat: + modules.append(stat) + with support.captured_stderr() as err: + with NulledModules(*modules): + d.cleanup() + message = err.getvalue() + self.assertIn("while cleaning up", message) + self.assertIn(d.name, message) + + # Check for the resource warning + with support.check_warnings(('Implicitly', ResourceWarning), quiet=False): + warnings.filterwarnings("always", category=ResourceWarning) + d.__del__() + self.assertFalse(os.path.exists(d.name), + "TemporaryDirectory %s exists after __del__" % d.name) def test_multiple_close(self): # Can be cleaned-up many times without error |