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 |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3 | import sys, io, os |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 4 | import struct |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 5 | |
| 6 | class SysModuleTest(unittest.TestCase): |
| 7 | |
Christian Heimes | ef181a7 | 2007-11-07 16:14:12 +0000 | [diff] [blame] | 8 | def setUp(self): |
| 9 | self.orig_stdout = sys.stdout |
| 10 | self.orig_stderr = sys.stderr |
| 11 | self.orig_displayhook = sys.displayhook |
| 12 | |
| 13 | def tearDown(self): |
| 14 | sys.stdout = self.orig_stdout |
| 15 | sys.stderr = self.orig_stderr |
| 16 | sys.displayhook = self.orig_displayhook |
| 17 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 18 | def test_original_displayhook(self): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 19 | import builtins |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 20 | out = io.StringIO() |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 21 | sys.stdout = out |
| 22 | |
| 23 | dh = sys.__displayhook__ |
| 24 | |
| 25 | self.assertRaises(TypeError, dh) |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 26 | if hasattr(builtins, "_"): |
| 27 | del builtins._ |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 28 | |
| 29 | dh(None) |
| 30 | self.assertEqual(out.getvalue(), "") |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 31 | self.assert_(not hasattr(builtins, "_")) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 32 | dh(42) |
| 33 | self.assertEqual(out.getvalue(), "42\n") |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 34 | self.assertEqual(builtins._, 42) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 35 | |
| 36 | del sys.stdout |
| 37 | self.assertRaises(RuntimeError, dh, 42) |
| 38 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 39 | def test_lost_displayhook(self): |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 40 | del sys.displayhook |
| 41 | code = compile("42", "<string>", "single") |
| 42 | self.assertRaises(RuntimeError, eval, code) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 43 | |
| 44 | def test_custom_displayhook(self): |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 45 | def baddisplayhook(obj): |
| 46 | raise ValueError |
| 47 | sys.displayhook = baddisplayhook |
| 48 | code = compile("42", "<string>", "single") |
| 49 | self.assertRaises(ValueError, eval, code) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 50 | |
| 51 | def test_original_excepthook(self): |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 52 | err = io.StringIO() |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 53 | sys.stderr = err |
| 54 | |
| 55 | eh = sys.__excepthook__ |
| 56 | |
| 57 | self.assertRaises(TypeError, eh) |
| 58 | try: |
| 59 | raise ValueError(42) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 60 | except ValueError as exc: |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 61 | eh(*sys.exc_info()) |
| 62 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 63 | self.assert_(err.getvalue().endswith("ValueError: 42\n")) |
| 64 | |
Walter Dörwald | e7028ac | 2003-02-03 23:05:27 +0000 | [diff] [blame] | 65 | # FIXME: testing the code for a lost or replaced excepthook in |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 66 | # Python/pythonrun.c::PyErr_PrintEx() is tricky. |
| 67 | |
| 68 | def test_exit(self): |
| 69 | self.assertRaises(TypeError, sys.exit, 42, 42) |
| 70 | |
| 71 | # call without argument |
| 72 | try: |
| 73 | sys.exit(0) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 74 | except SystemExit as exc: |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 75 | self.assertEquals(exc.code, 0) |
| 76 | except: |
| 77 | self.fail("wrong exception") |
| 78 | else: |
| 79 | self.fail("no exception") |
| 80 | |
| 81 | # call with tuple argument with one entry |
| 82 | # entry will be unpacked |
| 83 | try: |
| 84 | sys.exit(42) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 85 | except SystemExit as exc: |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 86 | self.assertEquals(exc.code, 42) |
| 87 | except: |
| 88 | self.fail("wrong exception") |
| 89 | else: |
| 90 | self.fail("no exception") |
| 91 | |
| 92 | # call with integer argument |
| 93 | try: |
| 94 | sys.exit((42,)) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 95 | except SystemExit as exc: |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 96 | self.assertEquals(exc.code, 42) |
| 97 | except: |
| 98 | self.fail("wrong exception") |
| 99 | else: |
| 100 | self.fail("no exception") |
| 101 | |
| 102 | # call with string argument |
| 103 | try: |
| 104 | sys.exit("exit") |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 105 | except SystemExit as exc: |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 106 | self.assertEquals(exc.code, "exit") |
| 107 | except: |
| 108 | self.fail("wrong exception") |
| 109 | else: |
| 110 | self.fail("no exception") |
| 111 | |
| 112 | # call with tuple argument with two entries |
| 113 | try: |
| 114 | sys.exit((17, 23)) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 115 | except SystemExit as exc: |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 116 | self.assertEquals(exc.code, (17, 23)) |
| 117 | except: |
| 118 | self.fail("wrong exception") |
| 119 | else: |
| 120 | self.fail("no exception") |
| 121 | |
Michael W. Hudson | f058858 | 2005-02-15 15:26:11 +0000 | [diff] [blame] | 122 | # test that the exit machinery handles SystemExits properly |
| 123 | import subprocess |
Michael W. Hudson | f058858 | 2005-02-15 15:26:11 +0000 | [diff] [blame] | 124 | rc = subprocess.call([sys.executable, "-c", |
| 125 | "raise SystemExit(47)"]) |
| 126 | self.assertEqual(rc, 47) |
Tim Peters | f0db38d | 2005-02-15 21:50:12 +0000 | [diff] [blame] | 127 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 128 | def test_getdefaultencoding(self): |
Walter Dörwald | 4aeaa96 | 2007-05-22 16:13:46 +0000 | [diff] [blame] | 129 | self.assertRaises(TypeError, sys.getdefaultencoding, 42) |
| 130 | # 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] | 131 | self.assert_(isinstance(sys.getdefaultencoding(), str)) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 132 | |
| 133 | # testing sys.settrace() is done in test_trace.py |
| 134 | # testing sys.setprofile() is done in test_profile.py |
| 135 | |
| 136 | def test_setcheckinterval(self): |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 137 | self.assertRaises(TypeError, sys.setcheckinterval) |
Tim Peters | e5e065b | 2003-07-06 18:36:54 +0000 | [diff] [blame] | 138 | orig = sys.getcheckinterval() |
| 139 | for n in 0, 100, 120, orig: # orig last to restore starting state |
| 140 | sys.setcheckinterval(n) |
| 141 | self.assertEquals(sys.getcheckinterval(), n) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 142 | |
| 143 | def test_recursionlimit(self): |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 144 | self.assertRaises(TypeError, sys.getrecursionlimit, 42) |
| 145 | oldlimit = sys.getrecursionlimit() |
| 146 | self.assertRaises(TypeError, sys.setrecursionlimit) |
| 147 | self.assertRaises(ValueError, sys.setrecursionlimit, -42) |
| 148 | sys.setrecursionlimit(10000) |
| 149 | self.assertEqual(sys.getrecursionlimit(), 10000) |
| 150 | sys.setrecursionlimit(oldlimit) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 151 | |
| 152 | def test_getwindowsversion(self): |
| 153 | if hasattr(sys, "getwindowsversion"): |
| 154 | v = sys.getwindowsversion() |
| 155 | self.assert_(isinstance(v, tuple)) |
| 156 | self.assertEqual(len(v), 5) |
| 157 | self.assert_(isinstance(v[0], int)) |
| 158 | self.assert_(isinstance(v[1], int)) |
| 159 | self.assert_(isinstance(v[2], int)) |
| 160 | self.assert_(isinstance(v[3], int)) |
| 161 | self.assert_(isinstance(v[4], str)) |
| 162 | |
| 163 | def test_dlopenflags(self): |
| 164 | if hasattr(sys, "setdlopenflags"): |
| 165 | self.assert_(hasattr(sys, "getdlopenflags")) |
| 166 | self.assertRaises(TypeError, sys.getdlopenflags, 42) |
| 167 | oldflags = sys.getdlopenflags() |
| 168 | self.assertRaises(TypeError, sys.setdlopenflags) |
| 169 | sys.setdlopenflags(oldflags+1) |
| 170 | self.assertEqual(sys.getdlopenflags(), oldflags+1) |
| 171 | sys.setdlopenflags(oldflags) |
| 172 | |
| 173 | def test_refcount(self): |
| 174 | self.assertRaises(TypeError, sys.getrefcount) |
| 175 | c = sys.getrefcount(None) |
| 176 | n = None |
| 177 | self.assertEqual(sys.getrefcount(None), c+1) |
| 178 | del n |
| 179 | self.assertEqual(sys.getrefcount(None), c) |
| 180 | if hasattr(sys, "gettotalrefcount"): |
| 181 | self.assert_(isinstance(sys.gettotalrefcount(), int)) |
| 182 | |
| 183 | def test_getframe(self): |
| 184 | self.assertRaises(TypeError, sys._getframe, 42, 42) |
Neal Norwitz | eb2a5ef | 2003-02-18 15:22:10 +0000 | [diff] [blame] | 185 | self.assertRaises(ValueError, sys._getframe, 2000000000) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 186 | self.assert_( |
Christian Heimes | 4a22b5d | 2007-11-25 09:39:14 +0000 | [diff] [blame] | 187 | SysModuleTest.test_getframe.__code__ \ |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 188 | is sys._getframe().f_code |
| 189 | ) |
| 190 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 191 | # sys._current_frames() is a CPython-only gimmick. |
| 192 | def test_current_frames(self): |
| 193 | have_threads = True |
| 194 | try: |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 195 | import _thread |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 196 | except ImportError: |
| 197 | have_threads = False |
| 198 | |
| 199 | if have_threads: |
| 200 | self.current_frames_with_threads() |
| 201 | else: |
| 202 | self.current_frames_without_threads() |
| 203 | |
| 204 | # Test sys._current_frames() in a WITH_THREADS build. |
| 205 | def current_frames_with_threads(self): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 206 | import threading, _thread |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 207 | import traceback |
| 208 | |
| 209 | # Spawn a thread that blocks at a known place. Then the main |
| 210 | # thread does sys._current_frames(), and verifies that the frames |
| 211 | # returned make sense. |
| 212 | entered_g = threading.Event() |
| 213 | leave_g = threading.Event() |
| 214 | thread_info = [] # the thread's id |
| 215 | |
| 216 | def f123(): |
| 217 | g456() |
| 218 | |
| 219 | def g456(): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 220 | thread_info.append(_thread.get_ident()) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 221 | entered_g.set() |
| 222 | leave_g.wait() |
| 223 | |
| 224 | t = threading.Thread(target=f123) |
| 225 | t.start() |
| 226 | entered_g.wait() |
| 227 | |
| 228 | # At this point, t has finished its entered_g.set(), although it's |
| 229 | # impossible to guess whether it's still on that line or has moved on |
| 230 | # to its leave_g.wait(). |
| 231 | self.assertEqual(len(thread_info), 1) |
| 232 | thread_id = thread_info[0] |
| 233 | |
| 234 | d = sys._current_frames() |
| 235 | |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 236 | main_id = _thread.get_ident() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 237 | self.assert_(main_id in d) |
| 238 | self.assert_(thread_id in d) |
| 239 | |
| 240 | # Verify that the captured main-thread frame is _this_ frame. |
| 241 | frame = d.pop(main_id) |
| 242 | self.assert_(frame is sys._getframe()) |
| 243 | |
| 244 | # Verify that the captured thread frame is blocked in g456, called |
| 245 | # from f123. This is a litte tricky, since various bits of |
| 246 | # threading.py are also in the thread's call stack. |
| 247 | frame = d.pop(thread_id) |
| 248 | stack = traceback.extract_stack(frame) |
| 249 | for i, (filename, lineno, funcname, sourceline) in enumerate(stack): |
| 250 | if funcname == "f123": |
| 251 | break |
| 252 | else: |
| 253 | self.fail("didn't find f123() on thread's call stack") |
| 254 | |
| 255 | self.assertEqual(sourceline, "g456()") |
| 256 | |
| 257 | # And the next record must be for g456(). |
| 258 | filename, lineno, funcname, sourceline = stack[i+1] |
| 259 | self.assertEqual(funcname, "g456") |
| 260 | self.assert_(sourceline in ["leave_g.wait()", "entered_g.set()"]) |
| 261 | |
| 262 | # Reap the spawned thread. |
| 263 | leave_g.set() |
| 264 | t.join() |
| 265 | |
| 266 | # Test sys._current_frames() when thread support doesn't exist. |
| 267 | def current_frames_without_threads(self): |
| 268 | # Not much happens here: there is only one thread, with artificial |
| 269 | # "thread id" 0. |
| 270 | d = sys._current_frames() |
| 271 | self.assertEqual(len(d), 1) |
| 272 | self.assert_(0 in d) |
| 273 | self.assert_(d[0] is sys._getframe()) |
| 274 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 275 | def test_attributes(self): |
| 276 | self.assert_(isinstance(sys.api_version, int)) |
| 277 | self.assert_(isinstance(sys.argv, list)) |
| 278 | self.assert_(sys.byteorder in ("little", "big")) |
| 279 | self.assert_(isinstance(sys.builtin_module_names, tuple)) |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 280 | self.assert_(isinstance(sys.copyright, str)) |
| 281 | self.assert_(isinstance(sys.exec_prefix, str)) |
| 282 | self.assert_(isinstance(sys.executable, str)) |
Christian Heimes | 9385266 | 2007-12-01 12:22:32 +0000 | [diff] [blame] | 283 | self.assertEqual(len(sys.float_info), 11) |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 284 | self.assertEqual(sys.float_info.radix, 2) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 285 | self.assert_(isinstance(sys.hexversion, int)) |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 286 | self.assert_(isinstance(sys.maxsize, int)) |
Walter Dörwald | 4aeaa96 | 2007-05-22 16:13:46 +0000 | [diff] [blame] | 287 | self.assert_(isinstance(sys.maxunicode, int)) |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 288 | self.assert_(isinstance(sys.platform, str)) |
| 289 | self.assert_(isinstance(sys.prefix, str)) |
| 290 | self.assert_(isinstance(sys.version, str)) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 291 | vi = sys.version_info |
| 292 | self.assert_(isinstance(vi, tuple)) |
| 293 | self.assertEqual(len(vi), 5) |
| 294 | self.assert_(isinstance(vi[0], int)) |
| 295 | self.assert_(isinstance(vi[1], int)) |
| 296 | self.assert_(isinstance(vi[2], int)) |
| 297 | self.assert_(vi[3] in ("alpha", "beta", "candidate", "final")) |
| 298 | self.assert_(isinstance(vi[4], int)) |
| 299 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 300 | def test_43581(self): |
Georg Brandl | 0312494 | 2008-06-10 15:50:56 +0000 | [diff] [blame] | 301 | # Can't use sys.stdout, as this is a StringIO object when |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 302 | # the test runs under regrtest. |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 303 | self.assertEqual(sys.__stdout__.encoding, sys.__stderr__.encoding) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 304 | |
Georg Brandl | 66a796e | 2006-12-19 20:50:34 +0000 | [diff] [blame] | 305 | def test_intern(self): |
| 306 | self.assertRaises(TypeError, sys.intern) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 307 | s = "never interned before" |
Georg Brandl | 66a796e | 2006-12-19 20:50:34 +0000 | [diff] [blame] | 308 | self.assert_(sys.intern(s) is s) |
| 309 | s2 = s.swapcase().swapcase() |
| 310 | self.assert_(sys.intern(s2) is s) |
| 311 | |
| 312 | # Subclasses of string can't be interned, because they |
| 313 | # provide too much opportunity for insane things to happen. |
| 314 | # We don't want them in the interned dict and if they aren't |
| 315 | # actually interned, we don't want to create the appearance |
| 316 | # that they are by allowing intern() to succeeed. |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 317 | class S(str): |
Georg Brandl | 66a796e | 2006-12-19 20:50:34 +0000 | [diff] [blame] | 318 | def __hash__(self): |
| 319 | return 123 |
| 320 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 321 | self.assertRaises(TypeError, sys.intern, S("abc")) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 322 | |
Georg Brandl | 66a796e | 2006-12-19 20:50:34 +0000 | [diff] [blame] | 323 | |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 324 | def test_sys_flags(self): |
| 325 | self.failUnless(sys.flags) |
| 326 | attrs = ("debug", "division_warning", |
| 327 | "inspect", "interactive", "optimize", "dont_write_bytecode", |
Georg Brandl | c5e88d5 | 2008-06-06 09:02:07 +0000 | [diff] [blame] | 328 | "no_user_site", "no_site", "ignore_environment", "verbose", |
Benjamin Peterson | 699adb9 | 2008-05-08 22:27:58 +0000 | [diff] [blame] | 329 | "bytes_warning") |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 330 | for attr in attrs: |
| 331 | self.assert_(hasattr(sys.flags, attr), attr) |
| 332 | self.assertEqual(type(getattr(sys.flags, attr)), int, attr) |
| 333 | self.assert_(repr(sys.flags)) |
Georg Brandl | c5e88d5 | 2008-06-06 09:02:07 +0000 | [diff] [blame] | 334 | self.assertEqual(len(sys.flags), len(attrs)) |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 335 | |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 336 | def test_clear_type_cache(self): |
| 337 | sys._clear_type_cache() |
| 338 | |
| 339 | def test_compact_freelists(self): |
| 340 | sys._compact_freelists() |
| 341 | r = sys._compact_freelists() |
Christian Heimes | 3ecfea71 | 2008-02-09 20:51:34 +0000 | [diff] [blame] | 342 | ## freed blocks shouldn't change |
| 343 | #self.assertEqual(r[0][2], 0) |
| 344 | ## fill freelists |
| 345 | #ints = list(range(10000)) |
| 346 | #floats = [float(i) for i in ints] |
| 347 | #del ints |
| 348 | #del floats |
| 349 | ## should free more than 100 blocks |
| 350 | #r = sys._compact_freelists() |
| 351 | #self.assert_(r[0][1] > 100, r[0][1]) |
| 352 | #self.assert_(r[0][2] > 100, r[0][2]) |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 353 | |
Martin v. Löwis | 0f59989 | 2008-06-02 11:13:03 +0000 | [diff] [blame] | 354 | def test_ioencoding(self): |
| 355 | import subprocess,os |
| 356 | env = dict(os.environ) |
| 357 | |
| 358 | # Test character: cent sign, encoded as 0x4A (ASCII J) in CP424, |
| 359 | # not representable in ASCII. |
| 360 | |
| 361 | env["PYTHONIOENCODING"] = "cp424" |
| 362 | p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], |
| 363 | stdout = subprocess.PIPE, env=env) |
| 364 | out = p.stdout.read() |
| 365 | self.assertEqual(out, "\xa2\n".encode("cp424")) |
| 366 | |
| 367 | env["PYTHONIOENCODING"] = "ascii:replace" |
| 368 | p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], |
| 369 | stdout = subprocess.PIPE, env=env) |
| 370 | out = p.stdout.read().strip() |
| 371 | self.assertEqual(out, b'?') |
| 372 | |
| 373 | |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 374 | class SizeofTest(unittest.TestCase): |
| 375 | |
| 376 | def setUp(self): |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 377 | self.c = len(struct.pack('c', ' ')) |
| 378 | self.H = len(struct.pack('H', 0)) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 379 | self.i = len(struct.pack('i', 0)) |
| 380 | self.l = len(struct.pack('l', 0)) |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 381 | self.P = len(struct.pack('P', 0)) |
| 382 | # due to missing size_t information from struct, it is assumed that |
| 383 | # sizeof(Py_ssize_t) = sizeof(void*) |
| 384 | self.header = 'PP' |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 385 | if hasattr(sys, "gettotalrefcount"): |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 386 | self.header += '2P' |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 387 | self.file = open(test.support.TESTFN, 'wb') |
| 388 | |
| 389 | def tearDown(self): |
| 390 | self.file.close() |
| 391 | test.support.unlink(test.support.TESTFN) |
| 392 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 393 | def check_sizeof(self, o, size, size2=None): |
| 394 | """Check size of o. Possible are size and optionally size2).""" |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 395 | result = sys.getsizeof(o) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 396 | msg = 'wrong size for %s: got %d, expected ' % (type(o), result) |
| 397 | if (size2 != None) and (result != size): |
| 398 | self.assertEqual(result, size2, msg + str(size2)) |
| 399 | else: |
| 400 | self.assertEqual(result, size, msg + str(size)) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 401 | |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 402 | def calcsize(self, fmt): |
| 403 | """Wrapper around struct.calcsize which enforces the alignment of the |
| 404 | end of a structure to the alignment requirement of pointer. |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 405 | |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 406 | Note: This wrapper should only be used if a pointer member is included |
| 407 | and no member with a size larger than a pointer exists. |
| 408 | """ |
| 409 | return struct.calcsize(fmt + '0P') |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 410 | |
| 411 | def test_standardtypes(self): |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 412 | h = self.header |
| 413 | size = self.calcsize |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 414 | # cell |
| 415 | def get_cell(): |
| 416 | x = 42 |
| 417 | def inner(): |
| 418 | return x |
| 419 | return inner |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 420 | self.check_sizeof(get_cell().__closure__[0], size(h + 'P')) |
Robert Schuppenies | 4efb518 | 2008-06-06 11:49:51 +0000 | [diff] [blame] | 421 | # code |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 422 | self.check_sizeof(get_cell().__code__, size(h + '5i8Pi2P')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 423 | # complex |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 424 | self.check_sizeof(complex(0,1), size(h + '2d')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 425 | # enumerate |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 426 | self.check_sizeof(enumerate([]), size(h + 'l3P')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 427 | # reverse |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 428 | self.check_sizeof(reversed(''), size(h + 'PP')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 429 | # float |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 430 | self.check_sizeof(float(0), size(h + 'd')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 431 | # function |
| 432 | def func(): pass |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 433 | self.check_sizeof(func, size(h + '11P')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 434 | class c(): |
| 435 | @staticmethod |
| 436 | def foo(): |
| 437 | pass |
| 438 | @classmethod |
| 439 | def bar(cls): |
| 440 | pass |
| 441 | # staticmethod |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 442 | self.check_sizeof(foo, size(h + 'P')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 443 | # classmethod |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 444 | self.check_sizeof(bar, size(h + 'P')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 445 | # generator |
| 446 | def get_gen(): yield 1 |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 447 | self.check_sizeof(get_gen(), size(h + 'Pi2P')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 448 | # builtin_function_or_method |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 449 | self.check_sizeof(abs, size(h + '3P')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 450 | # module |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 451 | self.check_sizeof(unittest, size(h + '3P')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 452 | # range |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 453 | self.check_sizeof(range(1), size(h + '3P')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 454 | # slice |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 455 | self.check_sizeof(slice(0), size(h + '3P')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 456 | |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 457 | h += 'P' |
| 458 | # bool |
| 459 | self.check_sizeof(True, size(h + 'H')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 460 | # new-style class |
| 461 | class class_newstyle(object): |
| 462 | def method(): |
| 463 | pass |
Robert Schuppenies | 4efb518 | 2008-06-06 11:49:51 +0000 | [diff] [blame] | 464 | # type (PyTypeObject + PyNumberMethods + PyMappingMethods + |
| 465 | # PySequenceMethods + PyBufferProcs) |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 466 | self.check_sizeof(class_newstyle, size(h + 'P2P15Pl4PP9PP11PI') +\ |
| 467 | size('16Pi17P 3P 10P 2P 2P')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 468 | |
| 469 | def test_specialtypes(self): |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 470 | h = self.header |
| 471 | size = self.calcsize |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 472 | # dict |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 473 | self.check_sizeof({}, size(h + '3P2P') + 8*size('P2P')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 474 | longdict = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8} |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 475 | self.check_sizeof(longdict, size(h + '3P2P') + (8+16)*size('P2P')) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 476 | # unicode |
Benjamin Peterson | b49d994 | 2008-06-17 12:44:04 +0000 | [diff] [blame] | 477 | usize = len('\0'.encode('unicode-internal')) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 478 | samples = ['', '1'*100] |
| 479 | # we need to test for both sizes, because we don't know if the string |
| 480 | # has been cached |
| 481 | for s in samples: |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 482 | basicsize = size(h + 'PPliP') + usize * (len(s) + 1) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 483 | defenc = bytes(s, 'ascii') |
| 484 | self.check_sizeof(s, basicsize, |
| 485 | size2=basicsize + sys.getsizeof(defenc)) |
| 486 | # trigger caching encoded version as bytes object |
| 487 | try: |
| 488 | getattr(sys, s) |
| 489 | except AttributeError: |
| 490 | pass |
| 491 | finally: |
| 492 | self.check_sizeof(s, basicsize + sys.getsizeof(defenc)) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 493 | |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 494 | h += 'P' |
| 495 | # list |
| 496 | self.check_sizeof([], size(h + 'PP')) |
| 497 | self.check_sizeof([1, 2, 3], size(h + 'PP') + 3*self.P) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 498 | # long |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 499 | self.check_sizeof(0, size(h + 'H')) |
| 500 | self.check_sizeof(1, size(h + 'H')) |
| 501 | self.check_sizeof(-1, size(h + 'H')) |
| 502 | self.check_sizeof(32768, size(h + 'H') + self.H) |
| 503 | self.check_sizeof(32768*32768-1, size(h + 'H') + self.H) |
| 504 | self.check_sizeof(32768*32768, size(h + 'H') + 2*self.H) |
Amaury Forgeot d'Arc | 35c8658 | 2008-06-17 21:11:29 +0000 | [diff] [blame] | 505 | # tuple |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 506 | self.check_sizeof((), size(h)) |
| 507 | self.check_sizeof((1,2,3), size(h) + 3*self.P) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 508 | |
| 509 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 510 | def test_main(): |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 511 | test.support.run_unittest(SysModuleTest, SizeofTest) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 512 | |
| 513 | if __name__ == "__main__": |
| 514 | test_main() |