Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 1 | # -*- coding: iso-8859-1 -*- |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame^] | 2 | import unittest, 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 | |
Christian Heimes | ef181a7 | 2007-11-07 16:14:12 +0000 | [diff] [blame] | 7 | def setUp(self): |
| 8 | self.orig_stdout = sys.stdout |
| 9 | self.orig_stderr = sys.stderr |
| 10 | self.orig_displayhook = sys.displayhook |
| 11 | |
| 12 | def tearDown(self): |
| 13 | sys.stdout = self.orig_stdout |
| 14 | sys.stderr = self.orig_stderr |
| 15 | sys.displayhook = self.orig_displayhook |
| 16 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 17 | def test_original_displayhook(self): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 18 | import builtins |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 19 | out = io.StringIO() |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 20 | sys.stdout = out |
| 21 | |
| 22 | dh = sys.__displayhook__ |
| 23 | |
| 24 | self.assertRaises(TypeError, dh) |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 25 | if hasattr(builtins, "_"): |
| 26 | del builtins._ |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 27 | |
| 28 | dh(None) |
| 29 | self.assertEqual(out.getvalue(), "") |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 30 | self.assert_(not hasattr(builtins, "_")) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 31 | dh(42) |
| 32 | self.assertEqual(out.getvalue(), "42\n") |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 33 | self.assertEqual(builtins._, 42) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 34 | |
| 35 | del sys.stdout |
| 36 | self.assertRaises(RuntimeError, dh, 42) |
| 37 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 38 | def test_lost_displayhook(self): |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 39 | del sys.displayhook |
| 40 | code = compile("42", "<string>", "single") |
| 41 | self.assertRaises(RuntimeError, eval, code) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 42 | |
| 43 | def test_custom_displayhook(self): |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 44 | def baddisplayhook(obj): |
| 45 | raise ValueError |
| 46 | sys.displayhook = baddisplayhook |
| 47 | code = compile("42", "<string>", "single") |
| 48 | self.assertRaises(ValueError, eval, code) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 49 | |
| 50 | def test_original_excepthook(self): |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 51 | err = io.StringIO() |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 52 | sys.stderr = err |
| 53 | |
| 54 | eh = sys.__excepthook__ |
| 55 | |
| 56 | self.assertRaises(TypeError, eh) |
| 57 | try: |
| 58 | raise ValueError(42) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 59 | except ValueError as exc: |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 60 | eh(*sys.exc_info()) |
| 61 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 62 | self.assert_(err.getvalue().endswith("ValueError: 42\n")) |
| 63 | |
Walter Dörwald | e7028ac | 2003-02-03 23:05:27 +0000 | [diff] [blame] | 64 | # FIXME: testing the code for a lost or replaced excepthook in |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 65 | # Python/pythonrun.c::PyErr_PrintEx() is tricky. |
| 66 | |
| 67 | def test_exit(self): |
| 68 | self.assertRaises(TypeError, sys.exit, 42, 42) |
| 69 | |
| 70 | # call without argument |
| 71 | try: |
| 72 | sys.exit(0) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 73 | except SystemExit as exc: |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 74 | self.assertEquals(exc.code, 0) |
| 75 | except: |
| 76 | self.fail("wrong exception") |
| 77 | else: |
| 78 | self.fail("no exception") |
| 79 | |
| 80 | # call with tuple argument with one entry |
| 81 | # entry will be unpacked |
| 82 | try: |
| 83 | sys.exit(42) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 84 | except SystemExit as exc: |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 85 | self.assertEquals(exc.code, 42) |
| 86 | except: |
| 87 | self.fail("wrong exception") |
| 88 | else: |
| 89 | self.fail("no exception") |
| 90 | |
| 91 | # call with integer argument |
| 92 | try: |
| 93 | sys.exit((42,)) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 94 | except SystemExit as exc: |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 95 | self.assertEquals(exc.code, 42) |
| 96 | except: |
| 97 | self.fail("wrong exception") |
| 98 | else: |
| 99 | self.fail("no exception") |
| 100 | |
| 101 | # call with string argument |
| 102 | try: |
| 103 | sys.exit("exit") |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 104 | except SystemExit as exc: |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 105 | self.assertEquals(exc.code, "exit") |
| 106 | except: |
| 107 | self.fail("wrong exception") |
| 108 | else: |
| 109 | self.fail("no exception") |
| 110 | |
| 111 | # call with tuple argument with two entries |
| 112 | try: |
| 113 | sys.exit((17, 23)) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 114 | except SystemExit as exc: |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 115 | self.assertEquals(exc.code, (17, 23)) |
| 116 | except: |
| 117 | self.fail("wrong exception") |
| 118 | else: |
| 119 | self.fail("no exception") |
| 120 | |
Michael W. Hudson | f058858 | 2005-02-15 15:26:11 +0000 | [diff] [blame] | 121 | # test that the exit machinery handles SystemExits properly |
| 122 | import subprocess |
Michael W. Hudson | f058858 | 2005-02-15 15:26:11 +0000 | [diff] [blame] | 123 | rc = subprocess.call([sys.executable, "-c", |
| 124 | "raise SystemExit(47)"]) |
| 125 | self.assertEqual(rc, 47) |
Tim Peters | f0db38d | 2005-02-15 21:50:12 +0000 | [diff] [blame] | 126 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 127 | def test_getdefaultencoding(self): |
Walter Dörwald | 4aeaa96 | 2007-05-22 16:13:46 +0000 | [diff] [blame] | 128 | self.assertRaises(TypeError, sys.getdefaultencoding, 42) |
| 129 | # 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] | 130 | self.assert_(isinstance(sys.getdefaultencoding(), str)) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 131 | |
| 132 | # testing sys.settrace() is done in test_trace.py |
| 133 | # testing sys.setprofile() is done in test_profile.py |
| 134 | |
| 135 | def test_setcheckinterval(self): |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 136 | self.assertRaises(TypeError, sys.setcheckinterval) |
Tim Peters | e5e065b | 2003-07-06 18:36:54 +0000 | [diff] [blame] | 137 | orig = sys.getcheckinterval() |
| 138 | for n in 0, 100, 120, orig: # orig last to restore starting state |
| 139 | sys.setcheckinterval(n) |
| 140 | self.assertEquals(sys.getcheckinterval(), n) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 141 | |
| 142 | def test_recursionlimit(self): |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 143 | self.assertRaises(TypeError, sys.getrecursionlimit, 42) |
| 144 | oldlimit = sys.getrecursionlimit() |
| 145 | self.assertRaises(TypeError, sys.setrecursionlimit) |
| 146 | self.assertRaises(ValueError, sys.setrecursionlimit, -42) |
| 147 | sys.setrecursionlimit(10000) |
| 148 | self.assertEqual(sys.getrecursionlimit(), 10000) |
| 149 | sys.setrecursionlimit(oldlimit) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 150 | |
| 151 | def test_getwindowsversion(self): |
| 152 | if hasattr(sys, "getwindowsversion"): |
| 153 | v = sys.getwindowsversion() |
| 154 | self.assert_(isinstance(v, tuple)) |
| 155 | self.assertEqual(len(v), 5) |
| 156 | self.assert_(isinstance(v[0], int)) |
| 157 | self.assert_(isinstance(v[1], int)) |
| 158 | self.assert_(isinstance(v[2], int)) |
| 159 | self.assert_(isinstance(v[3], int)) |
| 160 | self.assert_(isinstance(v[4], str)) |
| 161 | |
| 162 | def test_dlopenflags(self): |
| 163 | if hasattr(sys, "setdlopenflags"): |
| 164 | self.assert_(hasattr(sys, "getdlopenflags")) |
| 165 | self.assertRaises(TypeError, sys.getdlopenflags, 42) |
| 166 | oldflags = sys.getdlopenflags() |
| 167 | self.assertRaises(TypeError, sys.setdlopenflags) |
| 168 | sys.setdlopenflags(oldflags+1) |
| 169 | self.assertEqual(sys.getdlopenflags(), oldflags+1) |
| 170 | sys.setdlopenflags(oldflags) |
| 171 | |
| 172 | def test_refcount(self): |
| 173 | self.assertRaises(TypeError, sys.getrefcount) |
| 174 | c = sys.getrefcount(None) |
| 175 | n = None |
| 176 | self.assertEqual(sys.getrefcount(None), c+1) |
| 177 | del n |
| 178 | self.assertEqual(sys.getrefcount(None), c) |
| 179 | if hasattr(sys, "gettotalrefcount"): |
| 180 | self.assert_(isinstance(sys.gettotalrefcount(), int)) |
| 181 | |
| 182 | def test_getframe(self): |
| 183 | self.assertRaises(TypeError, sys._getframe, 42, 42) |
Neal Norwitz | eb2a5ef | 2003-02-18 15:22:10 +0000 | [diff] [blame] | 184 | self.assertRaises(ValueError, sys._getframe, 2000000000) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 185 | self.assert_( |
Christian Heimes | 4a22b5d | 2007-11-25 09:39:14 +0000 | [diff] [blame] | 186 | SysModuleTest.test_getframe.__code__ \ |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 187 | is sys._getframe().f_code |
| 188 | ) |
| 189 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 190 | # sys._current_frames() is a CPython-only gimmick. |
| 191 | def test_current_frames(self): |
| 192 | have_threads = True |
| 193 | try: |
| 194 | import thread |
| 195 | except ImportError: |
| 196 | have_threads = False |
| 197 | |
| 198 | if have_threads: |
| 199 | self.current_frames_with_threads() |
| 200 | else: |
| 201 | self.current_frames_without_threads() |
| 202 | |
| 203 | # Test sys._current_frames() in a WITH_THREADS build. |
| 204 | def current_frames_with_threads(self): |
| 205 | import threading, thread |
| 206 | import traceback |
| 207 | |
| 208 | # Spawn a thread that blocks at a known place. Then the main |
| 209 | # thread does sys._current_frames(), and verifies that the frames |
| 210 | # returned make sense. |
| 211 | entered_g = threading.Event() |
| 212 | leave_g = threading.Event() |
| 213 | thread_info = [] # the thread's id |
| 214 | |
| 215 | def f123(): |
| 216 | g456() |
| 217 | |
| 218 | def g456(): |
| 219 | thread_info.append(thread.get_ident()) |
| 220 | entered_g.set() |
| 221 | leave_g.wait() |
| 222 | |
| 223 | t = threading.Thread(target=f123) |
| 224 | t.start() |
| 225 | entered_g.wait() |
| 226 | |
| 227 | # At this point, t has finished its entered_g.set(), although it's |
| 228 | # impossible to guess whether it's still on that line or has moved on |
| 229 | # to its leave_g.wait(). |
| 230 | self.assertEqual(len(thread_info), 1) |
| 231 | thread_id = thread_info[0] |
| 232 | |
| 233 | d = sys._current_frames() |
| 234 | |
| 235 | main_id = thread.get_ident() |
| 236 | self.assert_(main_id in d) |
| 237 | self.assert_(thread_id in d) |
| 238 | |
| 239 | # Verify that the captured main-thread frame is _this_ frame. |
| 240 | frame = d.pop(main_id) |
| 241 | self.assert_(frame is sys._getframe()) |
| 242 | |
| 243 | # Verify that the captured thread frame is blocked in g456, called |
| 244 | # from f123. This is a litte tricky, since various bits of |
| 245 | # threading.py are also in the thread's call stack. |
| 246 | frame = d.pop(thread_id) |
| 247 | stack = traceback.extract_stack(frame) |
| 248 | for i, (filename, lineno, funcname, sourceline) in enumerate(stack): |
| 249 | if funcname == "f123": |
| 250 | break |
| 251 | else: |
| 252 | self.fail("didn't find f123() on thread's call stack") |
| 253 | |
| 254 | self.assertEqual(sourceline, "g456()") |
| 255 | |
| 256 | # And the next record must be for g456(). |
| 257 | filename, lineno, funcname, sourceline = stack[i+1] |
| 258 | self.assertEqual(funcname, "g456") |
| 259 | self.assert_(sourceline in ["leave_g.wait()", "entered_g.set()"]) |
| 260 | |
| 261 | # Reap the spawned thread. |
| 262 | leave_g.set() |
| 263 | t.join() |
| 264 | |
| 265 | # Test sys._current_frames() when thread support doesn't exist. |
| 266 | def current_frames_without_threads(self): |
| 267 | # Not much happens here: there is only one thread, with artificial |
| 268 | # "thread id" 0. |
| 269 | d = sys._current_frames() |
| 270 | self.assertEqual(len(d), 1) |
| 271 | self.assert_(0 in d) |
| 272 | self.assert_(d[0] is sys._getframe()) |
| 273 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 274 | def test_attributes(self): |
| 275 | self.assert_(isinstance(sys.api_version, int)) |
| 276 | self.assert_(isinstance(sys.argv, list)) |
| 277 | self.assert_(sys.byteorder in ("little", "big")) |
| 278 | self.assert_(isinstance(sys.builtin_module_names, tuple)) |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 279 | self.assert_(isinstance(sys.copyright, str)) |
| 280 | self.assert_(isinstance(sys.exec_prefix, str)) |
| 281 | self.assert_(isinstance(sys.executable, str)) |
Christian Heimes | 9385266 | 2007-12-01 12:22:32 +0000 | [diff] [blame] | 282 | self.assertEqual(len(sys.float_info), 11) |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 283 | self.assertEqual(sys.float_info.radix, 2) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 284 | self.assert_(isinstance(sys.hexversion, int)) |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 285 | self.assert_(isinstance(sys.maxsize, int)) |
Walter Dörwald | 4aeaa96 | 2007-05-22 16:13:46 +0000 | [diff] [blame] | 286 | self.assert_(isinstance(sys.maxunicode, int)) |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 287 | self.assert_(isinstance(sys.platform, str)) |
| 288 | self.assert_(isinstance(sys.prefix, str)) |
| 289 | self.assert_(isinstance(sys.version, str)) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 290 | vi = sys.version_info |
| 291 | self.assert_(isinstance(vi, tuple)) |
| 292 | self.assertEqual(len(vi), 5) |
| 293 | self.assert_(isinstance(vi[0], int)) |
| 294 | self.assert_(isinstance(vi[1], int)) |
| 295 | self.assert_(isinstance(vi[2], int)) |
| 296 | self.assert_(vi[3] in ("alpha", "beta", "candidate", "final")) |
| 297 | self.assert_(isinstance(vi[4], int)) |
| 298 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 299 | def test_43581(self): |
| 300 | # Can't use sys.stdout, as this is a cStringIO object when |
| 301 | # the test runs under regrtest. |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 302 | self.assertEqual(sys.__stdout__.encoding, sys.__stderr__.encoding) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 303 | |
Georg Brandl | 66a796e | 2006-12-19 20:50:34 +0000 | [diff] [blame] | 304 | def test_intern(self): |
| 305 | self.assertRaises(TypeError, sys.intern) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 306 | s = "never interned before" |
Georg Brandl | 66a796e | 2006-12-19 20:50:34 +0000 | [diff] [blame] | 307 | self.assert_(sys.intern(s) is s) |
| 308 | s2 = s.swapcase().swapcase() |
| 309 | self.assert_(sys.intern(s2) is s) |
| 310 | |
| 311 | # Subclasses of string can't be interned, because they |
| 312 | # provide too much opportunity for insane things to happen. |
| 313 | # We don't want them in the interned dict and if they aren't |
| 314 | # actually interned, we don't want to create the appearance |
| 315 | # that they are by allowing intern() to succeeed. |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 316 | class S(str): |
Georg Brandl | 66a796e | 2006-12-19 20:50:34 +0000 | [diff] [blame] | 317 | def __hash__(self): |
| 318 | return 123 |
| 319 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 320 | self.assertRaises(TypeError, sys.intern, S("abc")) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 321 | |
Georg Brandl | 66a796e | 2006-12-19 20:50:34 +0000 | [diff] [blame] | 322 | |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 323 | def test_sys_flags(self): |
| 324 | self.failUnless(sys.flags) |
| 325 | attrs = ("debug", "division_warning", |
| 326 | "inspect", "interactive", "optimize", "dont_write_bytecode", |
Benjamin Peterson | 699adb9 | 2008-05-08 22:27:58 +0000 | [diff] [blame] | 327 | "no_site", "ignore_environment", "tabcheck", "verbose", |
| 328 | "bytes_warning") |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 329 | for attr in attrs: |
| 330 | self.assert_(hasattr(sys.flags, attr), attr) |
| 331 | self.assertEqual(type(getattr(sys.flags, attr)), int, attr) |
| 332 | self.assert_(repr(sys.flags)) |
| 333 | |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 334 | def test_clear_type_cache(self): |
| 335 | sys._clear_type_cache() |
| 336 | |
| 337 | def test_compact_freelists(self): |
| 338 | sys._compact_freelists() |
| 339 | r = sys._compact_freelists() |
Christian Heimes | 3ecfea71 | 2008-02-09 20:51:34 +0000 | [diff] [blame] | 340 | ## freed blocks shouldn't change |
| 341 | #self.assertEqual(r[0][2], 0) |
| 342 | ## fill freelists |
| 343 | #ints = list(range(10000)) |
| 344 | #floats = [float(i) for i in ints] |
| 345 | #del ints |
| 346 | #del floats |
| 347 | ## should free more than 100 blocks |
| 348 | #r = sys._compact_freelists() |
| 349 | #self.assert_(r[0][1] > 100, r[0][1]) |
| 350 | #self.assert_(r[0][2] > 100, r[0][2]) |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 351 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 352 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame^] | 353 | test.support.run_unittest(SysModuleTest) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 354 | |
| 355 | if __name__ == "__main__": |
| 356 | test_main() |