1
# -*- coding: iso-8859-1 -*-
2
import unittest, test.test_support
5
class SysModuleTest(unittest.TestCase):
7
def test_original_displayhook(self):
9
savestdout = sys.stdout
10
out = cStringIO.StringIO()
13
dh = sys.__displayhook__
15
self.assertRaises(TypeError, dh)
16
if hasattr(__builtin__, "_"):
20
self.assertEqual(out.getvalue(), "")
21
self.assert_(not hasattr(__builtin__, "_"))
23
self.assertEqual(out.getvalue(), "42\n")
24
self.assertEqual(__builtin__._, 42)
27
self.assertRaises(RuntimeError, dh, 42)
29
sys.stdout = savestdout
31
def test_lost_displayhook(self):
32
olddisplayhook = sys.displayhook
34
code = compile("42", "<string>", "single")
35
self.assertRaises(RuntimeError, eval, code)
36
sys.displayhook = olddisplayhook
38
def test_custom_displayhook(self):
39
olddisplayhook = sys.displayhook
40
def baddisplayhook(obj):
42
sys.displayhook = baddisplayhook
43
code = compile("42", "<string>", "single")
44
self.assertRaises(ValueError, eval, code)
45
sys.displayhook = olddisplayhook
47
def test_original_excepthook(self):
48
savestderr = sys.stderr
49
err = cStringIO.StringIO()
52
eh = sys.__excepthook__
54
self.assertRaises(TypeError, eh)
57
except ValueError, exc:
60
sys.stderr = savestderr
61
self.assert_(err.getvalue().endswith("ValueError: 42\n"))
63
# FIXME: testing the code for a lost or replaced excepthook in
64
# Python/pythonrun.c::PyErr_PrintEx() is tricky.
66
def test_exc_clear(self):
67
self.assertRaises(TypeError, sys.exc_clear, 42)
69
# Verify that exc_info is present and matches exc, then clear it, and
70
# check that it worked.
72
typ, value, traceback = sys.exc_info()
73
self.assert_(typ is not None)
74
self.assert_(value is exc)
75
self.assert_(traceback is not None)
79
typ, value, traceback = sys.exc_info()
80
self.assert_(typ is None)
81
self.assert_(value is None)
82
self.assert_(traceback is None)
87
except ValueError, exc:
90
# Raise an exception and check that it can be cleared
93
# Verify that a frame currently handling an exception is
94
# unaffected by calling exc_clear in a nested frame.
97
except ValueError, exc:
98
typ1, value1, traceback1 = sys.exc_info()
100
typ2, value2, traceback2 = sys.exc_info()
102
self.assert_(typ1 is typ2)
103
self.assert_(value1 is exc)
104
self.assert_(value1 is value2)
105
self.assert_(traceback1 is traceback2)
107
# Check that an exception can be cleared outside of an except block
111
self.assertRaises(TypeError, sys.exit, 42, 42)
113
# call without argument
116
except SystemExit, exc:
117
self.assertEquals(exc.code, 0)
119
self.fail("wrong exception")
121
self.fail("no exception")
123
# call with tuple argument with one entry
124
# entry will be unpacked
127
except SystemExit, exc:
128
self.assertEquals(exc.code, 42)
130
self.fail("wrong exception")
132
self.fail("no exception")
134
# call with integer argument
137
except SystemExit, exc:
138
self.assertEquals(exc.code, 42)
140
self.fail("wrong exception")
142
self.fail("no exception")
144
# call with string argument
147
except SystemExit, exc:
148
self.assertEquals(exc.code, "exit")
150
self.fail("wrong exception")
152
self.fail("no exception")
154
# call with tuple argument with two entries
157
except SystemExit, exc:
158
self.assertEquals(exc.code, (17, 23))
160
self.fail("wrong exception")
162
self.fail("no exception")
164
def test_getdefaultencoding(self):
165
if test.test_support.have_unicode:
166
self.assertRaises(TypeError, sys.getdefaultencoding, 42)
167
# can't check more than the type, as the user might have changed it
168
self.assert_(isinstance(sys.getdefaultencoding(), str))
170
# testing sys.settrace() is done in test_trace.py
171
# testing sys.setprofile() is done in test_profile.py
173
def test_setcheckinterval(self):
174
self.assertRaises(TypeError, sys.setcheckinterval)
175
orig = sys.getcheckinterval()
176
for n in 0, 100, 120, orig: # orig last to restore starting state
177
sys.setcheckinterval(n)
178
self.assertEquals(sys.getcheckinterval(), n)
180
def test_recursionlimit(self):
181
self.assertRaises(TypeError, sys.getrecursionlimit, 42)
182
oldlimit = sys.getrecursionlimit()
183
self.assertRaises(TypeError, sys.setrecursionlimit)
184
self.assertRaises(ValueError, sys.setrecursionlimit, -42)
185
sys.setrecursionlimit(10000)
186
self.assertEqual(sys.getrecursionlimit(), 10000)
187
sys.setrecursionlimit(oldlimit)
189
def test_getwindowsversion(self):
190
if hasattr(sys, "getwindowsversion"):
191
v = sys.getwindowsversion()
192
self.assert_(isinstance(v, tuple))
193
self.assertEqual(len(v), 5)
194
self.assert_(isinstance(v[0], int))
195
self.assert_(isinstance(v[1], int))
196
self.assert_(isinstance(v[2], int))
197
self.assert_(isinstance(v[3], int))
198
self.assert_(isinstance(v[4], str))
200
def test_dlopenflags(self):
201
if hasattr(sys, "setdlopenflags"):
202
self.assert_(hasattr(sys, "getdlopenflags"))
203
self.assertRaises(TypeError, sys.getdlopenflags, 42)
204
oldflags = sys.getdlopenflags()
205
self.assertRaises(TypeError, sys.setdlopenflags)
206
sys.setdlopenflags(oldflags+1)
207
self.assertEqual(sys.getdlopenflags(), oldflags+1)
208
sys.setdlopenflags(oldflags)
210
def test_refcount(self):
211
self.assertRaises(TypeError, sys.getrefcount)
212
c = sys.getrefcount(None)
214
self.assertEqual(sys.getrefcount(None), c+1)
216
self.assertEqual(sys.getrefcount(None), c)
217
if hasattr(sys, "gettotalrefcount"):
218
self.assert_(isinstance(sys.gettotalrefcount(), int))
220
def test_getframe(self):
221
self.assertRaises(TypeError, sys._getframe, 42, 42)
222
self.assertRaises(ValueError, sys._getframe, 2000000000)
224
SysModuleTest.test_getframe.im_func.func_code \
225
is sys._getframe().f_code
228
def test_attributes(self):
229
self.assert_(isinstance(sys.api_version, int))
230
self.assert_(isinstance(sys.argv, list))
231
self.assert_(sys.byteorder in ("little", "big"))
232
self.assert_(isinstance(sys.builtin_module_names, tuple))
233
self.assert_(isinstance(sys.copyright, basestring))
234
self.assert_(isinstance(sys.exec_prefix, basestring))
235
self.assert_(isinstance(sys.executable, basestring))
236
self.assert_(isinstance(sys.hexversion, int))
237
self.assert_(isinstance(sys.maxint, int))
238
self.assert_(isinstance(sys.maxunicode, int))
239
self.assert_(isinstance(sys.platform, basestring))
240
self.assert_(isinstance(sys.prefix, basestring))
241
self.assert_(isinstance(sys.version, basestring))
242
vi = sys.version_info
243
self.assert_(isinstance(vi, tuple))
244
self.assertEqual(len(vi), 5)
245
self.assert_(isinstance(vi[0], int))
246
self.assert_(isinstance(vi[1], int))
247
self.assert_(isinstance(vi[2], int))
248
self.assert_(vi[3] in ("alpha", "beta", "candidate", "final"))
249
self.assert_(isinstance(vi[4], int))
252
test.test_support.run_unittest(SysModuleTest)
254
if __name__ == "__main__":