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