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) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 57 | except ValueError as exc: |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 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 |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 87 | except ValueError as exc: |
Guido van Rossum | d6473d1 | 2003-03-01 03:25:41 +0000 | [diff] [blame] | 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 | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 97 | except ValueError as 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 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 107 | def test_exit(self): |
| 108 | self.assertRaises(TypeError, sys.exit, 42, 42) |
| 109 | |
| 110 | # call without argument |
| 111 | try: |
| 112 | sys.exit(0) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 113 | except SystemExit as exc: |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 114 | self.assertEquals(exc.code, 0) |
| 115 | except: |
| 116 | self.fail("wrong exception") |
| 117 | else: |
| 118 | self.fail("no exception") |
| 119 | |
| 120 | # call with tuple argument with one entry |
| 121 | # entry will be unpacked |
| 122 | try: |
| 123 | sys.exit(42) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 124 | except SystemExit as exc: |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 125 | self.assertEquals(exc.code, 42) |
| 126 | except: |
| 127 | self.fail("wrong exception") |
| 128 | else: |
| 129 | self.fail("no exception") |
| 130 | |
| 131 | # call with integer argument |
| 132 | try: |
| 133 | sys.exit((42,)) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 134 | except SystemExit as exc: |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 135 | self.assertEquals(exc.code, 42) |
| 136 | except: |
| 137 | self.fail("wrong exception") |
| 138 | else: |
| 139 | self.fail("no exception") |
| 140 | |
| 141 | # call with string argument |
| 142 | try: |
| 143 | sys.exit("exit") |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 144 | except SystemExit as exc: |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 145 | self.assertEquals(exc.code, "exit") |
| 146 | except: |
| 147 | self.fail("wrong exception") |
| 148 | else: |
| 149 | self.fail("no exception") |
| 150 | |
| 151 | # call with tuple argument with two entries |
| 152 | try: |
| 153 | sys.exit((17, 23)) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 154 | except SystemExit as exc: |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 155 | self.assertEquals(exc.code, (17, 23)) |
| 156 | except: |
| 157 | self.fail("wrong exception") |
| 158 | else: |
| 159 | self.fail("no exception") |
| 160 | |
Michael W. Hudson | f058858 | 2005-02-15 15:26:11 +0000 | [diff] [blame] | 161 | # test that the exit machinery handles SystemExits properly |
| 162 | import subprocess |
| 163 | # both unnormalized... |
| 164 | rc = subprocess.call([sys.executable, "-c", |
| 165 | "raise SystemExit, 46"]) |
| 166 | self.assertEqual(rc, 46) |
| 167 | # ... and normalized |
| 168 | rc = subprocess.call([sys.executable, "-c", |
| 169 | "raise SystemExit(47)"]) |
| 170 | self.assertEqual(rc, 47) |
Tim Peters | f0db38d | 2005-02-15 21:50:12 +0000 | [diff] [blame] | 171 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 172 | def test_getdefaultencoding(self): |
Walter Dörwald | 4aeaa96 | 2007-05-22 16:13:46 +0000 | [diff] [blame^] | 173 | self.assertRaises(TypeError, sys.getdefaultencoding, 42) |
| 174 | # can't check more than the type, as the user might have changed it |
| 175 | self.assert_(isinstance(sys.getdefaultencoding(), basestring)) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 176 | |
| 177 | # testing sys.settrace() is done in test_trace.py |
| 178 | # testing sys.setprofile() is done in test_profile.py |
| 179 | |
| 180 | def test_setcheckinterval(self): |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 181 | self.assertRaises(TypeError, sys.setcheckinterval) |
Tim Peters | e5e065b | 2003-07-06 18:36:54 +0000 | [diff] [blame] | 182 | orig = sys.getcheckinterval() |
| 183 | for n in 0, 100, 120, orig: # orig last to restore starting state |
| 184 | sys.setcheckinterval(n) |
| 185 | self.assertEquals(sys.getcheckinterval(), n) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 186 | |
| 187 | def test_recursionlimit(self): |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 188 | self.assertRaises(TypeError, sys.getrecursionlimit, 42) |
| 189 | oldlimit = sys.getrecursionlimit() |
| 190 | self.assertRaises(TypeError, sys.setrecursionlimit) |
| 191 | self.assertRaises(ValueError, sys.setrecursionlimit, -42) |
| 192 | sys.setrecursionlimit(10000) |
| 193 | self.assertEqual(sys.getrecursionlimit(), 10000) |
| 194 | sys.setrecursionlimit(oldlimit) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 195 | |
| 196 | def test_getwindowsversion(self): |
| 197 | if hasattr(sys, "getwindowsversion"): |
| 198 | v = sys.getwindowsversion() |
| 199 | self.assert_(isinstance(v, tuple)) |
| 200 | self.assertEqual(len(v), 5) |
| 201 | self.assert_(isinstance(v[0], int)) |
| 202 | self.assert_(isinstance(v[1], int)) |
| 203 | self.assert_(isinstance(v[2], int)) |
| 204 | self.assert_(isinstance(v[3], int)) |
| 205 | self.assert_(isinstance(v[4], str)) |
| 206 | |
| 207 | def test_dlopenflags(self): |
| 208 | if hasattr(sys, "setdlopenflags"): |
| 209 | self.assert_(hasattr(sys, "getdlopenflags")) |
| 210 | self.assertRaises(TypeError, sys.getdlopenflags, 42) |
| 211 | oldflags = sys.getdlopenflags() |
| 212 | self.assertRaises(TypeError, sys.setdlopenflags) |
| 213 | sys.setdlopenflags(oldflags+1) |
| 214 | self.assertEqual(sys.getdlopenflags(), oldflags+1) |
| 215 | sys.setdlopenflags(oldflags) |
| 216 | |
| 217 | def test_refcount(self): |
| 218 | self.assertRaises(TypeError, sys.getrefcount) |
| 219 | c = sys.getrefcount(None) |
| 220 | n = None |
| 221 | self.assertEqual(sys.getrefcount(None), c+1) |
| 222 | del n |
| 223 | self.assertEqual(sys.getrefcount(None), c) |
| 224 | if hasattr(sys, "gettotalrefcount"): |
| 225 | self.assert_(isinstance(sys.gettotalrefcount(), int)) |
| 226 | |
| 227 | def test_getframe(self): |
| 228 | self.assertRaises(TypeError, sys._getframe, 42, 42) |
Neal Norwitz | eb2a5ef | 2003-02-18 15:22:10 +0000 | [diff] [blame] | 229 | self.assertRaises(ValueError, sys._getframe, 2000000000) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 230 | self.assert_( |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 231 | SysModuleTest.test_getframe.im_func.__code__ \ |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 232 | is sys._getframe().f_code |
| 233 | ) |
| 234 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 235 | # sys._current_frames() is a CPython-only gimmick. |
| 236 | def test_current_frames(self): |
| 237 | have_threads = True |
| 238 | try: |
| 239 | import thread |
| 240 | except ImportError: |
| 241 | have_threads = False |
| 242 | |
| 243 | if have_threads: |
| 244 | self.current_frames_with_threads() |
| 245 | else: |
| 246 | self.current_frames_without_threads() |
| 247 | |
| 248 | # Test sys._current_frames() in a WITH_THREADS build. |
| 249 | def current_frames_with_threads(self): |
| 250 | import threading, thread |
| 251 | import traceback |
| 252 | |
| 253 | # Spawn a thread that blocks at a known place. Then the main |
| 254 | # thread does sys._current_frames(), and verifies that the frames |
| 255 | # returned make sense. |
| 256 | entered_g = threading.Event() |
| 257 | leave_g = threading.Event() |
| 258 | thread_info = [] # the thread's id |
| 259 | |
| 260 | def f123(): |
| 261 | g456() |
| 262 | |
| 263 | def g456(): |
| 264 | thread_info.append(thread.get_ident()) |
| 265 | entered_g.set() |
| 266 | leave_g.wait() |
| 267 | |
| 268 | t = threading.Thread(target=f123) |
| 269 | t.start() |
| 270 | entered_g.wait() |
| 271 | |
| 272 | # At this point, t has finished its entered_g.set(), although it's |
| 273 | # impossible to guess whether it's still on that line or has moved on |
| 274 | # to its leave_g.wait(). |
| 275 | self.assertEqual(len(thread_info), 1) |
| 276 | thread_id = thread_info[0] |
| 277 | |
| 278 | d = sys._current_frames() |
| 279 | |
| 280 | main_id = thread.get_ident() |
| 281 | self.assert_(main_id in d) |
| 282 | self.assert_(thread_id in d) |
| 283 | |
| 284 | # Verify that the captured main-thread frame is _this_ frame. |
| 285 | frame = d.pop(main_id) |
| 286 | self.assert_(frame is sys._getframe()) |
| 287 | |
| 288 | # Verify that the captured thread frame is blocked in g456, called |
| 289 | # from f123. This is a litte tricky, since various bits of |
| 290 | # threading.py are also in the thread's call stack. |
| 291 | frame = d.pop(thread_id) |
| 292 | stack = traceback.extract_stack(frame) |
| 293 | for i, (filename, lineno, funcname, sourceline) in enumerate(stack): |
| 294 | if funcname == "f123": |
| 295 | break |
| 296 | else: |
| 297 | self.fail("didn't find f123() on thread's call stack") |
| 298 | |
| 299 | self.assertEqual(sourceline, "g456()") |
| 300 | |
| 301 | # And the next record must be for g456(). |
| 302 | filename, lineno, funcname, sourceline = stack[i+1] |
| 303 | self.assertEqual(funcname, "g456") |
| 304 | self.assert_(sourceline in ["leave_g.wait()", "entered_g.set()"]) |
| 305 | |
| 306 | # Reap the spawned thread. |
| 307 | leave_g.set() |
| 308 | t.join() |
| 309 | |
| 310 | # Test sys._current_frames() when thread support doesn't exist. |
| 311 | def current_frames_without_threads(self): |
| 312 | # Not much happens here: there is only one thread, with artificial |
| 313 | # "thread id" 0. |
| 314 | d = sys._current_frames() |
| 315 | self.assertEqual(len(d), 1) |
| 316 | self.assert_(0 in d) |
| 317 | self.assert_(d[0] is sys._getframe()) |
| 318 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 319 | def test_attributes(self): |
| 320 | self.assert_(isinstance(sys.api_version, int)) |
| 321 | self.assert_(isinstance(sys.argv, list)) |
| 322 | self.assert_(sys.byteorder in ("little", "big")) |
| 323 | self.assert_(isinstance(sys.builtin_module_names, tuple)) |
| 324 | self.assert_(isinstance(sys.copyright, basestring)) |
| 325 | self.assert_(isinstance(sys.exec_prefix, basestring)) |
| 326 | self.assert_(isinstance(sys.executable, basestring)) |
| 327 | self.assert_(isinstance(sys.hexversion, int)) |
| 328 | self.assert_(isinstance(sys.maxint, int)) |
Walter Dörwald | 4aeaa96 | 2007-05-22 16:13:46 +0000 | [diff] [blame^] | 329 | self.assert_(isinstance(sys.maxunicode, int)) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 330 | self.assert_(isinstance(sys.platform, basestring)) |
| 331 | self.assert_(isinstance(sys.prefix, basestring)) |
| 332 | self.assert_(isinstance(sys.version, basestring)) |
| 333 | vi = sys.version_info |
| 334 | self.assert_(isinstance(vi, tuple)) |
| 335 | self.assertEqual(len(vi), 5) |
| 336 | self.assert_(isinstance(vi[0], int)) |
| 337 | self.assert_(isinstance(vi[1], int)) |
| 338 | self.assert_(isinstance(vi[2], int)) |
| 339 | self.assert_(vi[3] in ("alpha", "beta", "candidate", "final")) |
| 340 | self.assert_(isinstance(vi[4], int)) |
| 341 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 342 | def test_43581(self): |
| 343 | # Can't use sys.stdout, as this is a cStringIO object when |
| 344 | # the test runs under regrtest. |
| 345 | self.assert_(sys.__stdout__.encoding == sys.__stderr__.encoding) |
| 346 | |
Georg Brandl | 66a796e | 2006-12-19 20:50:34 +0000 | [diff] [blame] | 347 | def test_intern(self): |
| 348 | self.assertRaises(TypeError, sys.intern) |
Guido van Rossum | af2362a | 2007-05-15 22:32:02 +0000 | [diff] [blame] | 349 | s = str8("never interned before") |
Georg Brandl | 66a796e | 2006-12-19 20:50:34 +0000 | [diff] [blame] | 350 | self.assert_(sys.intern(s) is s) |
| 351 | s2 = s.swapcase().swapcase() |
| 352 | self.assert_(sys.intern(s2) is s) |
| 353 | |
| 354 | # Subclasses of string can't be interned, because they |
| 355 | # provide too much opportunity for insane things to happen. |
| 356 | # We don't want them in the interned dict and if they aren't |
| 357 | # actually interned, we don't want to create the appearance |
| 358 | # that they are by allowing intern() to succeeed. |
| 359 | class S(str): |
| 360 | def __hash__(self): |
| 361 | return 123 |
| 362 | |
| 363 | self.assertRaises(TypeError, sys.intern, S("abc")) |
| 364 | |
| 365 | # It's still safe to pass these strings to routines that |
| 366 | # call intern internally, e.g. PyObject_SetAttr(). |
| 367 | s = S("abc") |
| 368 | setattr(s, s, s) |
| 369 | self.assertEqual(getattr(s, s), s) |
| 370 | |
| 371 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 372 | def test_main(): |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 373 | test.test_support.run_unittest(SysModuleTest) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 374 | |
| 375 | if __name__ == "__main__": |
| 376 | test_main() |