blob: 606b8ce020368939488a2ce59ec201b5a224e90e [file] [log] [blame]
Walter Dörwaldc3502462003-02-03 23:03:49 +00001# -*- coding: iso-8859-1 -*-
2import unittest, test.test_support
3import sys, cStringIO
4
5class SysModuleTest(unittest.TestCase):
6
7 def test_original_displayhook(self):
8 import __builtin__
9 savestdout = sys.stdout
10 out = cStringIO.StringIO()
11 sys.stdout = out
12
13 dh = sys.__displayhook__
14
15 self.assertRaises(TypeError, dh)
16 if hasattr(__builtin__, "_"):
17 del __builtin__._
18
19 dh(None)
20 self.assertEqual(out.getvalue(), "")
21 self.assert_(not hasattr(__builtin__, "_"))
22 dh(42)
23 self.assertEqual(out.getvalue(), "42\n")
24 self.assertEqual(__builtin__._, 42)
25
26 del sys.stdout
27 self.assertRaises(RuntimeError, dh, 42)
28
29 sys.stdout = savestdout
30
31 def test_lost_displayhook(self):
32 olddisplayhook = sys.displayhook
33 del sys.displayhook
34 code = compile("42", "<string>", "single")
35 self.assertRaises(RuntimeError, eval, code)
36 sys.displayhook = olddisplayhook
37
38 def test_custom_displayhook(self):
39 olddisplayhook = sys.displayhook
40 def baddisplayhook(obj):
41 raise ValueError
42 sys.displayhook = baddisplayhook
43 code = compile("42", "<string>", "single")
44 self.assertRaises(ValueError, eval, code)
45 sys.displayhook = olddisplayhook
46
47 def test_original_excepthook(self):
48 savestderr = sys.stderr
49 err = cStringIO.StringIO()
50 sys.stderr = err
51
52 eh = sys.__excepthook__
53
54 self.assertRaises(TypeError, eh)
55 try:
56 raise ValueError(42)
57 except ValueError, exc:
58 eh(*sys.exc_info())
59
60 sys.stderr = savestderr
61 self.assert_(err.getvalue().endswith("ValueError: 42\n"))
62
Walter Dörwalde7028ac2003-02-03 23:05:27 +000063 # FIXME: testing the code for a lost or replaced excepthook in
Walter Dörwaldc3502462003-02-03 23:03:49 +000064 # Python/pythonrun.c::PyErr_PrintEx() is tricky.
65
66 def test_exit(self):
67 self.assertRaises(TypeError, sys.exit, 42, 42)
68
69 # call without argument
70 try:
71 sys.exit(0)
72 except SystemExit, exc:
73 self.assertEquals(exc.code, 0)
74 except:
75 self.fail("wrong exception")
76 else:
77 self.fail("no exception")
78
79 # call with tuple argument with one entry
80 # entry will be unpacked
81 try:
82 sys.exit(42)
83 except SystemExit, exc:
84 self.assertEquals(exc.code, 42)
85 except:
86 self.fail("wrong exception")
87 else:
88 self.fail("no exception")
89
90 # call with integer argument
91 try:
92 sys.exit((42,))
93 except SystemExit, exc:
94 self.assertEquals(exc.code, 42)
95 except:
96 self.fail("wrong exception")
97 else:
98 self.fail("no exception")
99
100 # call with string argument
101 try:
102 sys.exit("exit")
103 except SystemExit, exc:
104 self.assertEquals(exc.code, "exit")
105 except:
106 self.fail("wrong exception")
107 else:
108 self.fail("no exception")
109
110 # call with tuple argument with two entries
111 try:
112 sys.exit((17, 23))
113 except SystemExit, exc:
114 self.assertEquals(exc.code, (17, 23))
115 except:
116 self.fail("wrong exception")
117 else:
118 self.fail("no exception")
119
120 def test_getdefaultencoding(self):
121 if test.test_support.have_unicode:
122 self.assertRaises(TypeError, sys.getdefaultencoding, 42)
123 # can't check more than the type, as the user might have changed it
124 self.assert_(isinstance(sys.getdefaultencoding(), str))
125
126 # testing sys.settrace() is done in test_trace.py
127 # testing sys.setprofile() is done in test_profile.py
128
129 def test_setcheckinterval(self):
130 self.assertRaises(TypeError, sys.setcheckinterval)
131 sys.setcheckinterval(120)
132 sys.setcheckinterval(100)
133
134 def test_recursionlimit(self):
135 self.assertRaises(TypeError, sys.getrecursionlimit, 42)
136 oldlimit = sys.getrecursionlimit()
137 self.assertRaises(TypeError, sys.setrecursionlimit)
138 self.assertRaises(ValueError, sys.setrecursionlimit, -42)
139 sys.setrecursionlimit(10000)
140 self.assertEqual(sys.getrecursionlimit(), 10000)
141 sys.setrecursionlimit(oldlimit)
142
143 def test_getwindowsversion(self):
144 if hasattr(sys, "getwindowsversion"):
145 v = sys.getwindowsversion()
146 self.assert_(isinstance(v, tuple))
147 self.assertEqual(len(v), 5)
148 self.assert_(isinstance(v[0], int))
149 self.assert_(isinstance(v[1], int))
150 self.assert_(isinstance(v[2], int))
151 self.assert_(isinstance(v[3], int))
152 self.assert_(isinstance(v[4], str))
153
154 def test_dlopenflags(self):
155 if hasattr(sys, "setdlopenflags"):
156 self.assert_(hasattr(sys, "getdlopenflags"))
157 self.assertRaises(TypeError, sys.getdlopenflags, 42)
158 oldflags = sys.getdlopenflags()
159 self.assertRaises(TypeError, sys.setdlopenflags)
160 sys.setdlopenflags(oldflags+1)
161 self.assertEqual(sys.getdlopenflags(), oldflags+1)
162 sys.setdlopenflags(oldflags)
163
164 def test_refcount(self):
165 self.assertRaises(TypeError, sys.getrefcount)
166 c = sys.getrefcount(None)
167 n = None
168 self.assertEqual(sys.getrefcount(None), c+1)
169 del n
170 self.assertEqual(sys.getrefcount(None), c)
171 if hasattr(sys, "gettotalrefcount"):
172 self.assert_(isinstance(sys.gettotalrefcount(), int))
173
174 def test_getframe(self):
175 self.assertRaises(TypeError, sys._getframe, 42, 42)
Neal Norwitzeb2a5ef2003-02-18 15:22:10 +0000176 self.assertRaises(ValueError, sys._getframe, 2000000000)
Walter Dörwaldc3502462003-02-03 23:03:49 +0000177 self.assert_(
178 SysModuleTest.test_getframe.im_func.func_code \
179 is sys._getframe().f_code
180 )
181
182 def test_attributes(self):
183 self.assert_(isinstance(sys.api_version, int))
184 self.assert_(isinstance(sys.argv, list))
185 self.assert_(sys.byteorder in ("little", "big"))
186 self.assert_(isinstance(sys.builtin_module_names, tuple))
187 self.assert_(isinstance(sys.copyright, basestring))
188 self.assert_(isinstance(sys.exec_prefix, basestring))
189 self.assert_(isinstance(sys.executable, basestring))
190 self.assert_(isinstance(sys.hexversion, int))
191 self.assert_(isinstance(sys.maxint, int))
192 self.assert_(isinstance(sys.maxunicode, int))
193 self.assert_(isinstance(sys.platform, basestring))
194 self.assert_(isinstance(sys.prefix, basestring))
195 self.assert_(isinstance(sys.version, basestring))
196 vi = sys.version_info
197 self.assert_(isinstance(vi, tuple))
198 self.assertEqual(len(vi), 5)
199 self.assert_(isinstance(vi[0], int))
200 self.assert_(isinstance(vi[1], int))
201 self.assert_(isinstance(vi[2], int))
202 self.assert_(vi[3] in ("alpha", "beta", "candidate", "final"))
203 self.assert_(isinstance(vi[4], int))
204
205def test_main():
206 suite = unittest.TestSuite()
207 suite.addTest(unittest.makeSuite(SysModuleTest))
208 test.test_support.run_suite(suite)
209
210if __name__ == "__main__":
211 test_main()