Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 1 | # -*- coding: iso-8859-1 -*- |
| 2 | import unittest, test.test_support |
| 3 | import sys, cStringIO |
| 4 | |
| 5 | class 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örwald | e7028ac | 2003-02-03 23:05:27 +0000 | [diff] [blame] | 63 | # FIXME: testing the code for a lost or replaced excepthook in |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 64 | # Python/pythonrun.c::PyErr_PrintEx() is tricky. |
| 65 | |
Guido van Rossum | 46d3dc3 | 2003-03-01 03:20:41 +0000 | [diff] [blame] | 66 | def test_exc_clear(self): |
| 67 | self.assertRaises(TypeError, sys.exc_clear, 42) |
| 68 | |
| 69 | # Verify that exc_info is present and matches exc, then clear it, and |
| 70 | # check that it worked. |
| 71 | def clear_check(exc): |
Guido van Rossum | d6473d1 | 2003-03-01 03:25:41 +0000 | [diff] [blame] | 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) |
Guido van Rossum | 46d3dc3 | 2003-03-01 03:20:41 +0000 | [diff] [blame] | 76 | |
Guido van Rossum | d6473d1 | 2003-03-01 03:25:41 +0000 | [diff] [blame] | 77 | sys.exc_clear() |
Guido van Rossum | 46d3dc3 | 2003-03-01 03:20:41 +0000 | [diff] [blame] | 78 | |
Guido van Rossum | d6473d1 | 2003-03-01 03:25:41 +0000 | [diff] [blame] | 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) |
Guido van Rossum | 46d3dc3 | 2003-03-01 03:20:41 +0000 | [diff] [blame] | 83 | |
| 84 | def clear(): |
Guido van Rossum | d6473d1 | 2003-03-01 03:25:41 +0000 | [diff] [blame] | 85 | try: |
| 86 | raise ValueError, 42 |
| 87 | except ValueError, exc: |
| 88 | clear_check(exc) |
Guido van Rossum | 46d3dc3 | 2003-03-01 03:20:41 +0000 | [diff] [blame] | 89 | |
| 90 | # Raise an exception and check that it can be cleared |
| 91 | clear() |
| 92 | |
| 93 | # Verify that a frame currently handling an exception is |
| 94 | # unaffected by calling exc_clear in a nested frame. |
| 95 | try: |
Guido van Rossum | d6473d1 | 2003-03-01 03:25:41 +0000 | [diff] [blame] | 96 | raise ValueError, 13 |
Guido van Rossum | 46d3dc3 | 2003-03-01 03:20:41 +0000 | [diff] [blame] | 97 | except ValueError, exc: |
Guido van Rossum | d6473d1 | 2003-03-01 03:25:41 +0000 | [diff] [blame] | 98 | typ1, value1, traceback1 = sys.exc_info() |
| 99 | clear() |
| 100 | typ2, value2, traceback2 = sys.exc_info() |
Guido van Rossum | 46d3dc3 | 2003-03-01 03:20:41 +0000 | [diff] [blame] | 101 | |
Guido van Rossum | d6473d1 | 2003-03-01 03:25:41 +0000 | [diff] [blame] | 102 | self.assert_(typ1 is typ2) |
| 103 | self.assert_(value1 is exc) |
| 104 | self.assert_(value1 is value2) |
| 105 | self.assert_(traceback1 is traceback2) |
Guido van Rossum | 46d3dc3 | 2003-03-01 03:20:41 +0000 | [diff] [blame] | 106 | |
| 107 | # Check that an exception can be cleared outside of an except block |
| 108 | clear_check(exc) |
| 109 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 110 | def test_exit(self): |
| 111 | self.assertRaises(TypeError, sys.exit, 42, 42) |
| 112 | |
| 113 | # call without argument |
| 114 | try: |
| 115 | sys.exit(0) |
| 116 | except SystemExit, exc: |
| 117 | self.assertEquals(exc.code, 0) |
| 118 | except: |
| 119 | self.fail("wrong exception") |
| 120 | else: |
| 121 | self.fail("no exception") |
| 122 | |
| 123 | # call with tuple argument with one entry |
| 124 | # entry will be unpacked |
| 125 | try: |
| 126 | sys.exit(42) |
| 127 | except SystemExit, exc: |
| 128 | self.assertEquals(exc.code, 42) |
| 129 | except: |
| 130 | self.fail("wrong exception") |
| 131 | else: |
| 132 | self.fail("no exception") |
| 133 | |
| 134 | # call with integer argument |
| 135 | try: |
| 136 | sys.exit((42,)) |
| 137 | except SystemExit, exc: |
| 138 | self.assertEquals(exc.code, 42) |
| 139 | except: |
| 140 | self.fail("wrong exception") |
| 141 | else: |
| 142 | self.fail("no exception") |
| 143 | |
| 144 | # call with string argument |
| 145 | try: |
| 146 | sys.exit("exit") |
| 147 | except SystemExit, exc: |
| 148 | self.assertEquals(exc.code, "exit") |
| 149 | except: |
| 150 | self.fail("wrong exception") |
| 151 | else: |
| 152 | self.fail("no exception") |
| 153 | |
| 154 | # call with tuple argument with two entries |
| 155 | try: |
| 156 | sys.exit((17, 23)) |
| 157 | except SystemExit, exc: |
| 158 | self.assertEquals(exc.code, (17, 23)) |
| 159 | except: |
| 160 | self.fail("wrong exception") |
| 161 | else: |
| 162 | self.fail("no exception") |
| 163 | |
Michael W. Hudson | f058858 | 2005-02-15 15:26:11 +0000 | [diff] [blame] | 164 | # test that the exit machinery handles SystemExits properly |
| 165 | import subprocess |
| 166 | # both unnormalized... |
| 167 | rc = subprocess.call([sys.executable, "-c", |
| 168 | "raise SystemExit, 46"]) |
| 169 | self.assertEqual(rc, 46) |
| 170 | # ... and normalized |
| 171 | rc = subprocess.call([sys.executable, "-c", |
| 172 | "raise SystemExit(47)"]) |
| 173 | self.assertEqual(rc, 47) |
Tim Peters | f0db38d | 2005-02-15 21:50:12 +0000 | [diff] [blame] | 174 | |
| 175 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 176 | def test_getdefaultencoding(self): |
| 177 | if test.test_support.have_unicode: |
| 178 | self.assertRaises(TypeError, sys.getdefaultencoding, 42) |
| 179 | # can't check more than the type, as the user might have changed it |
| 180 | self.assert_(isinstance(sys.getdefaultencoding(), str)) |
| 181 | |
| 182 | # testing sys.settrace() is done in test_trace.py |
| 183 | # testing sys.setprofile() is done in test_profile.py |
| 184 | |
| 185 | def test_setcheckinterval(self): |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 186 | self.assertRaises(TypeError, sys.setcheckinterval) |
Tim Peters | e5e065b | 2003-07-06 18:36:54 +0000 | [diff] [blame] | 187 | orig = sys.getcheckinterval() |
| 188 | for n in 0, 100, 120, orig: # orig last to restore starting state |
| 189 | sys.setcheckinterval(n) |
| 190 | self.assertEquals(sys.getcheckinterval(), n) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 191 | |
| 192 | def test_recursionlimit(self): |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 193 | self.assertRaises(TypeError, sys.getrecursionlimit, 42) |
| 194 | oldlimit = sys.getrecursionlimit() |
| 195 | self.assertRaises(TypeError, sys.setrecursionlimit) |
| 196 | self.assertRaises(ValueError, sys.setrecursionlimit, -42) |
| 197 | sys.setrecursionlimit(10000) |
| 198 | self.assertEqual(sys.getrecursionlimit(), 10000) |
| 199 | sys.setrecursionlimit(oldlimit) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 200 | |
| 201 | def test_getwindowsversion(self): |
| 202 | if hasattr(sys, "getwindowsversion"): |
| 203 | v = sys.getwindowsversion() |
| 204 | self.assert_(isinstance(v, tuple)) |
| 205 | self.assertEqual(len(v), 5) |
| 206 | self.assert_(isinstance(v[0], int)) |
| 207 | self.assert_(isinstance(v[1], int)) |
| 208 | self.assert_(isinstance(v[2], int)) |
| 209 | self.assert_(isinstance(v[3], int)) |
| 210 | self.assert_(isinstance(v[4], str)) |
| 211 | |
| 212 | def test_dlopenflags(self): |
| 213 | if hasattr(sys, "setdlopenflags"): |
| 214 | self.assert_(hasattr(sys, "getdlopenflags")) |
| 215 | self.assertRaises(TypeError, sys.getdlopenflags, 42) |
| 216 | oldflags = sys.getdlopenflags() |
| 217 | self.assertRaises(TypeError, sys.setdlopenflags) |
| 218 | sys.setdlopenflags(oldflags+1) |
| 219 | self.assertEqual(sys.getdlopenflags(), oldflags+1) |
| 220 | sys.setdlopenflags(oldflags) |
| 221 | |
| 222 | def test_refcount(self): |
| 223 | self.assertRaises(TypeError, sys.getrefcount) |
| 224 | c = sys.getrefcount(None) |
| 225 | n = None |
| 226 | self.assertEqual(sys.getrefcount(None), c+1) |
| 227 | del n |
| 228 | self.assertEqual(sys.getrefcount(None), c) |
| 229 | if hasattr(sys, "gettotalrefcount"): |
| 230 | self.assert_(isinstance(sys.gettotalrefcount(), int)) |
| 231 | |
| 232 | def test_getframe(self): |
| 233 | self.assertRaises(TypeError, sys._getframe, 42, 42) |
Neal Norwitz | eb2a5ef | 2003-02-18 15:22:10 +0000 | [diff] [blame] | 234 | self.assertRaises(ValueError, sys._getframe, 2000000000) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 235 | self.assert_( |
| 236 | SysModuleTest.test_getframe.im_func.func_code \ |
| 237 | is sys._getframe().f_code |
| 238 | ) |
| 239 | |
| 240 | def test_attributes(self): |
| 241 | self.assert_(isinstance(sys.api_version, int)) |
| 242 | self.assert_(isinstance(sys.argv, list)) |
| 243 | self.assert_(sys.byteorder in ("little", "big")) |
| 244 | self.assert_(isinstance(sys.builtin_module_names, tuple)) |
| 245 | self.assert_(isinstance(sys.copyright, basestring)) |
| 246 | self.assert_(isinstance(sys.exec_prefix, basestring)) |
| 247 | self.assert_(isinstance(sys.executable, basestring)) |
| 248 | self.assert_(isinstance(sys.hexversion, int)) |
| 249 | self.assert_(isinstance(sys.maxint, int)) |
Walter Dörwald | 4e41a4b | 2005-08-03 17:09:04 +0000 | [diff] [blame^] | 250 | if test.test_support.have_unicode: |
| 251 | self.assert_(isinstance(sys.maxunicode, int)) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 252 | self.assert_(isinstance(sys.platform, basestring)) |
| 253 | self.assert_(isinstance(sys.prefix, basestring)) |
| 254 | self.assert_(isinstance(sys.version, basestring)) |
| 255 | vi = sys.version_info |
| 256 | self.assert_(isinstance(vi, tuple)) |
| 257 | self.assertEqual(len(vi), 5) |
| 258 | self.assert_(isinstance(vi[0], int)) |
| 259 | self.assert_(isinstance(vi[1], int)) |
| 260 | self.assert_(isinstance(vi[2], int)) |
| 261 | self.assert_(vi[3] in ("alpha", "beta", "candidate", "final")) |
| 262 | self.assert_(isinstance(vi[4], int)) |
| 263 | |
| 264 | def test_main(): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 265 | test.test_support.run_unittest(SysModuleTest) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 266 | |
| 267 | if __name__ == "__main__": |
| 268 | test_main() |