summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_sys.py
diff options
context:
space:
mode:
authorChristian Heimes <christian@cheimes.de>2007-11-07 16:14:12 (GMT)
committerChristian Heimes <christian@cheimes.de>2007-11-07 16:14:12 (GMT)
commitef181a7619a84639fed79d3071703636f39c3485 (patch)
treeb3a2599fac401160fa230afd7e4d6704c6c9bd06 /Lib/test/test_sys.py
parent27da812498ca99ad12a4a8f146be6fc96df5bacc (diff)
downloadcpython-ef181a7619a84639fed79d3071703636f39c3485.zip
cpython-ef181a7619a84639fed79d3071703636f39c3485.tar.gz
cpython-ef181a7619a84639fed79d3071703636f39c3485.tar.bz2
Reset original sys.stdout, stderr and displayhook even in the case of an error
Diffstat (limited to 'Lib/test/test_sys.py')
-rw-r--r--Lib/test/test_sys.py19
1 files changed, 10 insertions, 9 deletions
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
index ad7082e..dfad172 100644
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -4,9 +4,18 @@ import sys, io
class SysModuleTest(unittest.TestCase):
+ def setUp(self):
+ self.orig_stdout = sys.stdout
+ self.orig_stderr = sys.stderr
+ self.orig_displayhook = sys.displayhook
+
+ def tearDown(self):
+ sys.stdout = self.orig_stdout
+ sys.stderr = self.orig_stderr
+ sys.displayhook = self.orig_displayhook
+
def test_original_displayhook(self):
import __builtin__
- savestdout = sys.stdout
out = io.StringIO()
sys.stdout = out
@@ -26,26 +35,19 @@ class SysModuleTest(unittest.TestCase):
del sys.stdout
self.assertRaises(RuntimeError, dh, 42)
- sys.stdout = savestdout
-
def test_lost_displayhook(self):
- olddisplayhook = sys.displayhook
del sys.displayhook
code = compile("42", "<string>", "single")
self.assertRaises(RuntimeError, eval, code)
- sys.displayhook = olddisplayhook
def test_custom_displayhook(self):
- olddisplayhook = sys.displayhook
def baddisplayhook(obj):
raise ValueError
sys.displayhook = baddisplayhook
code = compile("42", "<string>", "single")
self.assertRaises(ValueError, eval, code)
- sys.displayhook = olddisplayhook
def test_original_excepthook(self):
- savestderr = sys.stderr
err = io.StringIO()
sys.stderr = err
@@ -57,7 +59,6 @@ class SysModuleTest(unittest.TestCase):
except ValueError as exc:
eh(*sys.exc_info())
- sys.stderr = savestderr
self.assert_(err.getvalue().endswith("ValueError: 42\n"))
# FIXME: testing the code for a lost or replaced excepthook in