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 |
Antoine Pitrou | 652e707 | 2009-03-13 19:25:20 +0000 | [diff] [blame] | 5 | import subprocess |
| 6 | import textwrap |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 7 | |
| 8 | class SysModuleTest(unittest.TestCase): |
| 9 | |
Christian Heimes | ef181a7 | 2007-11-07 16:14:12 +0000 | [diff] [blame] | 10 | def setUp(self): |
| 11 | self.orig_stdout = sys.stdout |
| 12 | self.orig_stderr = sys.stderr |
| 13 | self.orig_displayhook = sys.displayhook |
| 14 | |
| 15 | def tearDown(self): |
| 16 | sys.stdout = self.orig_stdout |
| 17 | sys.stderr = self.orig_stderr |
| 18 | sys.displayhook = self.orig_displayhook |
| 19 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 20 | def test_original_displayhook(self): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 21 | import builtins |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 22 | out = io.StringIO() |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 23 | sys.stdout = out |
| 24 | |
| 25 | dh = sys.__displayhook__ |
| 26 | |
| 27 | self.assertRaises(TypeError, dh) |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 28 | if hasattr(builtins, "_"): |
| 29 | del builtins._ |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 30 | |
| 31 | dh(None) |
| 32 | self.assertEqual(out.getvalue(), "") |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 33 | self.assert_(not hasattr(builtins, "_")) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 34 | dh(42) |
| 35 | self.assertEqual(out.getvalue(), "42\n") |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 36 | self.assertEqual(builtins._, 42) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 37 | |
| 38 | del sys.stdout |
| 39 | self.assertRaises(RuntimeError, dh, 42) |
| 40 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 41 | def test_lost_displayhook(self): |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 42 | del sys.displayhook |
| 43 | code = compile("42", "<string>", "single") |
| 44 | self.assertRaises(RuntimeError, eval, code) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 45 | |
| 46 | def test_custom_displayhook(self): |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 47 | def baddisplayhook(obj): |
| 48 | raise ValueError |
| 49 | sys.displayhook = baddisplayhook |
| 50 | code = compile("42", "<string>", "single") |
| 51 | self.assertRaises(ValueError, eval, code) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 52 | |
| 53 | def test_original_excepthook(self): |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 54 | err = io.StringIO() |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 55 | sys.stderr = err |
| 56 | |
| 57 | eh = sys.__excepthook__ |
| 58 | |
| 59 | self.assertRaises(TypeError, eh) |
| 60 | try: |
| 61 | raise ValueError(42) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 62 | except ValueError as exc: |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 63 | eh(*sys.exc_info()) |
| 64 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 65 | self.assert_(err.getvalue().endswith("ValueError: 42\n")) |
| 66 | |
Benjamin Peterson | bc74e5b | 2008-08-23 20:19:17 +0000 | [diff] [blame] | 67 | def test_excepthook(self): |
| 68 | with test.support.captured_output("stderr") as stderr: |
| 69 | sys.excepthook(1, '1', 1) |
| 70 | self.assert_("TypeError: print_exception(): Exception expected for " \ |
| 71 | "value, str found" in stderr.getvalue()) |
| 72 | |
Walter Dörwald | e7028ac | 2003-02-03 23:05:27 +0000 | [diff] [blame] | 73 | # FIXME: testing the code for a lost or replaced excepthook in |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 74 | # Python/pythonrun.c::PyErr_PrintEx() is tricky. |
| 75 | |
| 76 | def test_exit(self): |
| 77 | self.assertRaises(TypeError, sys.exit, 42, 42) |
| 78 | |
| 79 | # call without argument |
| 80 | try: |
| 81 | sys.exit(0) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 82 | except SystemExit as exc: |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 83 | self.assertEquals(exc.code, 0) |
| 84 | except: |
| 85 | self.fail("wrong exception") |
| 86 | else: |
| 87 | self.fail("no exception") |
| 88 | |
| 89 | # call with tuple argument with one entry |
| 90 | # entry will be unpacked |
| 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 integer argument |
| 101 | try: |
| 102 | sys.exit((42,)) |
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, 42) |
| 105 | except: |
| 106 | self.fail("wrong exception") |
| 107 | else: |
| 108 | self.fail("no exception") |
| 109 | |
| 110 | # call with string argument |
| 111 | try: |
| 112 | sys.exit("exit") |
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, "exit") |
| 115 | except: |
| 116 | self.fail("wrong exception") |
| 117 | else: |
| 118 | self.fail("no exception") |
| 119 | |
| 120 | # call with tuple argument with two entries |
| 121 | try: |
| 122 | sys.exit((17, 23)) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 123 | except SystemExit as exc: |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 124 | self.assertEquals(exc.code, (17, 23)) |
| 125 | except: |
| 126 | self.fail("wrong exception") |
| 127 | else: |
| 128 | self.fail("no exception") |
| 129 | |
Michael W. Hudson | f058858 | 2005-02-15 15:26:11 +0000 | [diff] [blame] | 130 | # test that the exit machinery handles SystemExits properly |
| 131 | import subprocess |
Michael W. Hudson | f058858 | 2005-02-15 15:26:11 +0000 | [diff] [blame] | 132 | rc = subprocess.call([sys.executable, "-c", |
| 133 | "raise SystemExit(47)"]) |
| 134 | self.assertEqual(rc, 47) |
Tim Peters | f0db38d | 2005-02-15 21:50:12 +0000 | [diff] [blame] | 135 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 136 | def test_getdefaultencoding(self): |
Walter Dörwald | 4aeaa96 | 2007-05-22 16:13:46 +0000 | [diff] [blame] | 137 | self.assertRaises(TypeError, sys.getdefaultencoding, 42) |
| 138 | # 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] | 139 | self.assert_(isinstance(sys.getdefaultencoding(), str)) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 140 | |
| 141 | # testing sys.settrace() is done in test_trace.py |
| 142 | # testing sys.setprofile() is done in test_profile.py |
| 143 | |
| 144 | def test_setcheckinterval(self): |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 145 | self.assertRaises(TypeError, sys.setcheckinterval) |
Tim Peters | e5e065b | 2003-07-06 18:36:54 +0000 | [diff] [blame] | 146 | orig = sys.getcheckinterval() |
| 147 | for n in 0, 100, 120, orig: # orig last to restore starting state |
| 148 | sys.setcheckinterval(n) |
| 149 | self.assertEquals(sys.getcheckinterval(), n) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 150 | |
| 151 | def test_recursionlimit(self): |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 152 | self.assertRaises(TypeError, sys.getrecursionlimit, 42) |
| 153 | oldlimit = sys.getrecursionlimit() |
| 154 | self.assertRaises(TypeError, sys.setrecursionlimit) |
| 155 | self.assertRaises(ValueError, sys.setrecursionlimit, -42) |
| 156 | sys.setrecursionlimit(10000) |
| 157 | self.assertEqual(sys.getrecursionlimit(), 10000) |
| 158 | sys.setrecursionlimit(oldlimit) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 159 | |
Antoine Pitrou | 652e707 | 2009-03-13 19:25:20 +0000 | [diff] [blame] | 160 | def test_recursionlimit_recovery(self): |
| 161 | # NOTE: this test is slightly fragile in that it depends on the current |
| 162 | # recursion count when executing the test being low enough so as to |
| 163 | # trigger the recursion recovery detection in the _Py_MakeEndRecCheck |
| 164 | # macro (see ceval.h). |
| 165 | oldlimit = sys.getrecursionlimit() |
| 166 | def f(): |
| 167 | f() |
| 168 | try: |
| 169 | for i in (50, 1000): |
| 170 | # Issue #5392: stack overflow after hitting recursion limit twice |
| 171 | sys.setrecursionlimit(i) |
| 172 | self.assertRaises(RuntimeError, f) |
| 173 | self.assertRaises(RuntimeError, f) |
| 174 | finally: |
| 175 | sys.setrecursionlimit(oldlimit) |
| 176 | |
| 177 | def test_recursionlimit_fatalerror(self): |
| 178 | # A fatal error occurs if a second recursion limit is hit when recovering |
| 179 | # from a first one. |
Antoine Pitrou | 17dc446 | 2009-04-04 17:36:05 +0000 | [diff] [blame] | 180 | if os.name == "nt": |
| 181 | raise unittest.SkipTest( |
| 182 | "under Windows, test would generate a spurious crash dialog") |
Antoine Pitrou | 652e707 | 2009-03-13 19:25:20 +0000 | [diff] [blame] | 183 | code = textwrap.dedent(""" |
| 184 | import sys |
| 185 | |
| 186 | def f(): |
| 187 | try: |
| 188 | f() |
| 189 | except RuntimeError: |
| 190 | f() |
| 191 | |
| 192 | sys.setrecursionlimit(%d) |
| 193 | f()""") |
| 194 | for i in (50, 1000): |
| 195 | sub = subprocess.Popen([sys.executable, '-c', code % i], |
| 196 | stderr=subprocess.PIPE) |
| 197 | err = sub.communicate()[1] |
| 198 | self.assertTrue(sub.returncode, sub.returncode) |
| 199 | self.assertTrue( |
| 200 | b"Fatal Python error: Cannot recover from stack overflow" in err, |
| 201 | err) |
| 202 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 203 | def test_getwindowsversion(self): |
| 204 | if hasattr(sys, "getwindowsversion"): |
| 205 | v = sys.getwindowsversion() |
| 206 | self.assert_(isinstance(v, tuple)) |
| 207 | self.assertEqual(len(v), 5) |
| 208 | self.assert_(isinstance(v[0], int)) |
| 209 | self.assert_(isinstance(v[1], int)) |
| 210 | self.assert_(isinstance(v[2], int)) |
| 211 | self.assert_(isinstance(v[3], int)) |
| 212 | self.assert_(isinstance(v[4], str)) |
| 213 | |
Barry Warsaw | e94a37f | 2008-10-17 01:29:56 +0000 | [diff] [blame] | 214 | def test_call_tracing(self): |
| 215 | self.assertRaises(TypeError, sys.call_tracing, type, 2) |
| 216 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 217 | def test_dlopenflags(self): |
| 218 | if hasattr(sys, "setdlopenflags"): |
| 219 | self.assert_(hasattr(sys, "getdlopenflags")) |
| 220 | self.assertRaises(TypeError, sys.getdlopenflags, 42) |
| 221 | oldflags = sys.getdlopenflags() |
| 222 | self.assertRaises(TypeError, sys.setdlopenflags) |
| 223 | sys.setdlopenflags(oldflags+1) |
| 224 | self.assertEqual(sys.getdlopenflags(), oldflags+1) |
| 225 | sys.setdlopenflags(oldflags) |
| 226 | |
| 227 | def test_refcount(self): |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 228 | # n here must be a global in order for this test to pass while |
| 229 | # tracing with a python function. Tracing calls PyFrame_FastToLocals |
| 230 | # which will add a copy of any locals to the frame object, causing |
| 231 | # the reference count to increase by 2 instead of 1. |
| 232 | global n |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 233 | self.assertRaises(TypeError, sys.getrefcount) |
| 234 | c = sys.getrefcount(None) |
| 235 | n = None |
| 236 | self.assertEqual(sys.getrefcount(None), c+1) |
| 237 | del n |
| 238 | self.assertEqual(sys.getrefcount(None), c) |
| 239 | if hasattr(sys, "gettotalrefcount"): |
| 240 | self.assert_(isinstance(sys.gettotalrefcount(), int)) |
| 241 | |
| 242 | def test_getframe(self): |
| 243 | self.assertRaises(TypeError, sys._getframe, 42, 42) |
Neal Norwitz | eb2a5ef | 2003-02-18 15:22:10 +0000 | [diff] [blame] | 244 | self.assertRaises(ValueError, sys._getframe, 2000000000) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 245 | self.assert_( |
Christian Heimes | 4a22b5d | 2007-11-25 09:39:14 +0000 | [diff] [blame] | 246 | SysModuleTest.test_getframe.__code__ \ |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 247 | is sys._getframe().f_code |
| 248 | ) |
| 249 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 250 | # sys._current_frames() is a CPython-only gimmick. |
| 251 | def test_current_frames(self): |
| 252 | have_threads = True |
| 253 | try: |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 254 | import _thread |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 255 | except ImportError: |
| 256 | have_threads = False |
| 257 | |
| 258 | if have_threads: |
| 259 | self.current_frames_with_threads() |
| 260 | else: |
| 261 | self.current_frames_without_threads() |
| 262 | |
| 263 | # Test sys._current_frames() in a WITH_THREADS build. |
| 264 | def current_frames_with_threads(self): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 265 | import threading, _thread |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 266 | import traceback |
| 267 | |
| 268 | # Spawn a thread that blocks at a known place. Then the main |
| 269 | # thread does sys._current_frames(), and verifies that the frames |
| 270 | # returned make sense. |
| 271 | entered_g = threading.Event() |
| 272 | leave_g = threading.Event() |
| 273 | thread_info = [] # the thread's id |
| 274 | |
| 275 | def f123(): |
| 276 | g456() |
| 277 | |
| 278 | def g456(): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 279 | thread_info.append(_thread.get_ident()) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 280 | entered_g.set() |
| 281 | leave_g.wait() |
| 282 | |
| 283 | t = threading.Thread(target=f123) |
| 284 | t.start() |
| 285 | entered_g.wait() |
| 286 | |
| 287 | # At this point, t has finished its entered_g.set(), although it's |
| 288 | # impossible to guess whether it's still on that line or has moved on |
| 289 | # to its leave_g.wait(). |
| 290 | self.assertEqual(len(thread_info), 1) |
| 291 | thread_id = thread_info[0] |
| 292 | |
| 293 | d = sys._current_frames() |
| 294 | |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 295 | main_id = _thread.get_ident() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 296 | self.assert_(main_id in d) |
| 297 | self.assert_(thread_id in d) |
| 298 | |
| 299 | # Verify that the captured main-thread frame is _this_ frame. |
| 300 | frame = d.pop(main_id) |
| 301 | self.assert_(frame is sys._getframe()) |
| 302 | |
| 303 | # Verify that the captured thread frame is blocked in g456, called |
| 304 | # from f123. This is a litte tricky, since various bits of |
| 305 | # threading.py are also in the thread's call stack. |
| 306 | frame = d.pop(thread_id) |
| 307 | stack = traceback.extract_stack(frame) |
| 308 | for i, (filename, lineno, funcname, sourceline) in enumerate(stack): |
| 309 | if funcname == "f123": |
| 310 | break |
| 311 | else: |
| 312 | self.fail("didn't find f123() on thread's call stack") |
| 313 | |
| 314 | self.assertEqual(sourceline, "g456()") |
| 315 | |
| 316 | # And the next record must be for g456(). |
| 317 | filename, lineno, funcname, sourceline = stack[i+1] |
| 318 | self.assertEqual(funcname, "g456") |
| 319 | self.assert_(sourceline in ["leave_g.wait()", "entered_g.set()"]) |
| 320 | |
| 321 | # Reap the spawned thread. |
| 322 | leave_g.set() |
| 323 | t.join() |
| 324 | |
| 325 | # Test sys._current_frames() when thread support doesn't exist. |
| 326 | def current_frames_without_threads(self): |
| 327 | # Not much happens here: there is only one thread, with artificial |
| 328 | # "thread id" 0. |
| 329 | d = sys._current_frames() |
| 330 | self.assertEqual(len(d), 1) |
| 331 | self.assert_(0 in d) |
| 332 | self.assert_(d[0] is sys._getframe()) |
| 333 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 334 | def test_attributes(self): |
| 335 | self.assert_(isinstance(sys.api_version, int)) |
| 336 | self.assert_(isinstance(sys.argv, list)) |
| 337 | self.assert_(sys.byteorder in ("little", "big")) |
| 338 | self.assert_(isinstance(sys.builtin_module_names, tuple)) |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 339 | self.assert_(isinstance(sys.copyright, str)) |
| 340 | self.assert_(isinstance(sys.exec_prefix, str)) |
| 341 | self.assert_(isinstance(sys.executable, str)) |
Christian Heimes | 9385266 | 2007-12-01 12:22:32 +0000 | [diff] [blame] | 342 | self.assertEqual(len(sys.float_info), 11) |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 343 | self.assertEqual(sys.float_info.radix, 2) |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 344 | self.assertEqual(len(sys.int_info), 2) |
| 345 | self.assert_(sys.int_info.bits_per_digit % 5 == 0) |
| 346 | self.assert_(sys.int_info.sizeof_digit >= 1) |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame^] | 347 | self.assertEqual(type(sys.int_info.bits_per_digit), int) |
| 348 | self.assertEqual(type(sys.int_info.sizeof_digit), int) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 349 | self.assert_(isinstance(sys.hexversion, int)) |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 350 | self.assert_(isinstance(sys.maxsize, int)) |
Walter Dörwald | 4aeaa96 | 2007-05-22 16:13:46 +0000 | [diff] [blame] | 351 | self.assert_(isinstance(sys.maxunicode, int)) |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 352 | self.assert_(isinstance(sys.platform, str)) |
| 353 | self.assert_(isinstance(sys.prefix, str)) |
| 354 | self.assert_(isinstance(sys.version, str)) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 355 | vi = sys.version_info |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 356 | self.assert_(isinstance(vi[:], tuple)) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 357 | self.assertEqual(len(vi), 5) |
| 358 | self.assert_(isinstance(vi[0], int)) |
| 359 | self.assert_(isinstance(vi[1], int)) |
| 360 | self.assert_(isinstance(vi[2], int)) |
| 361 | self.assert_(vi[3] in ("alpha", "beta", "candidate", "final")) |
| 362 | self.assert_(isinstance(vi[4], int)) |
Eric Smith | 0e5b562 | 2009-02-06 01:32:42 +0000 | [diff] [blame] | 363 | self.assert_(isinstance(vi.major, int)) |
| 364 | self.assert_(isinstance(vi.minor, int)) |
| 365 | self.assert_(isinstance(vi.micro, int)) |
| 366 | self.assert_(vi.releaselevel in |
| 367 | ("alpha", "beta", "candidate", "final")) |
| 368 | self.assert_(isinstance(vi.serial, int)) |
| 369 | self.assertEqual(vi[0], vi.major) |
| 370 | self.assertEqual(vi[1], vi.minor) |
| 371 | self.assertEqual(vi[2], vi.micro) |
| 372 | self.assertEqual(vi[3], vi.releaselevel) |
| 373 | self.assertEqual(vi[4], vi.serial) |
| 374 | self.assert_(vi > (1,0,0)) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 375 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 376 | def test_43581(self): |
Georg Brandl | 0312494 | 2008-06-10 15:50:56 +0000 | [diff] [blame] | 377 | # Can't use sys.stdout, as this is a StringIO object when |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 378 | # the test runs under regrtest. |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 379 | self.assertEqual(sys.__stdout__.encoding, sys.__stderr__.encoding) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 380 | |
Georg Brandl | 66a796e | 2006-12-19 20:50:34 +0000 | [diff] [blame] | 381 | def test_intern(self): |
| 382 | self.assertRaises(TypeError, sys.intern) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 383 | s = "never interned before" |
Georg Brandl | 66a796e | 2006-12-19 20:50:34 +0000 | [diff] [blame] | 384 | self.assert_(sys.intern(s) is s) |
| 385 | s2 = s.swapcase().swapcase() |
| 386 | self.assert_(sys.intern(s2) is s) |
| 387 | |
| 388 | # Subclasses of string can't be interned, because they |
| 389 | # provide too much opportunity for insane things to happen. |
| 390 | # We don't want them in the interned dict and if they aren't |
| 391 | # actually interned, we don't want to create the appearance |
| 392 | # that they are by allowing intern() to succeeed. |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 393 | class S(str): |
Georg Brandl | 66a796e | 2006-12-19 20:50:34 +0000 | [diff] [blame] | 394 | def __hash__(self): |
| 395 | return 123 |
| 396 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 397 | self.assertRaises(TypeError, sys.intern, S("abc")) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 398 | |
Georg Brandl | 66a796e | 2006-12-19 20:50:34 +0000 | [diff] [blame] | 399 | |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 400 | def test_sys_flags(self): |
| 401 | self.failUnless(sys.flags) |
| 402 | attrs = ("debug", "division_warning", |
| 403 | "inspect", "interactive", "optimize", "dont_write_bytecode", |
Georg Brandl | c5e88d5 | 2008-06-06 09:02:07 +0000 | [diff] [blame] | 404 | "no_user_site", "no_site", "ignore_environment", "verbose", |
Benjamin Peterson | 699adb9 | 2008-05-08 22:27:58 +0000 | [diff] [blame] | 405 | "bytes_warning") |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 406 | for attr in attrs: |
| 407 | self.assert_(hasattr(sys.flags, attr), attr) |
| 408 | self.assertEqual(type(getattr(sys.flags, attr)), int, attr) |
| 409 | self.assert_(repr(sys.flags)) |
Georg Brandl | c5e88d5 | 2008-06-06 09:02:07 +0000 | [diff] [blame] | 410 | self.assertEqual(len(sys.flags), len(attrs)) |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 411 | |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 412 | def test_clear_type_cache(self): |
| 413 | sys._clear_type_cache() |
| 414 | |
Martin v. Löwis | 0f59989 | 2008-06-02 11:13:03 +0000 | [diff] [blame] | 415 | def test_ioencoding(self): |
| 416 | import subprocess,os |
| 417 | env = dict(os.environ) |
| 418 | |
| 419 | # Test character: cent sign, encoded as 0x4A (ASCII J) in CP424, |
| 420 | # not representable in ASCII. |
| 421 | |
| 422 | env["PYTHONIOENCODING"] = "cp424" |
| 423 | p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], |
| 424 | stdout = subprocess.PIPE, env=env) |
| 425 | out = p.stdout.read() |
| 426 | self.assertEqual(out, "\xa2\n".encode("cp424")) |
| 427 | |
| 428 | env["PYTHONIOENCODING"] = "ascii:replace" |
| 429 | p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], |
| 430 | stdout = subprocess.PIPE, env=env) |
| 431 | out = p.stdout.read().strip() |
| 432 | self.assertEqual(out, b'?') |
| 433 | |
| 434 | |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 435 | class SizeofTest(unittest.TestCase): |
| 436 | |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 437 | TPFLAGS_HAVE_GC = 1<<14 |
| 438 | TPFLAGS_HEAPTYPE = 1<<9 |
| 439 | |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 440 | def setUp(self): |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 441 | self.c = len(struct.pack('c', ' ')) |
| 442 | self.H = len(struct.pack('H', 0)) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 443 | self.i = len(struct.pack('i', 0)) |
| 444 | self.l = len(struct.pack('l', 0)) |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 445 | self.P = len(struct.pack('P', 0)) |
| 446 | # due to missing size_t information from struct, it is assumed that |
| 447 | # sizeof(Py_ssize_t) = sizeof(void*) |
| 448 | self.header = 'PP' |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 449 | self.vheader = self.header + 'P' |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 450 | if hasattr(sys, "gettotalrefcount"): |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 451 | self.header += '2P' |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 452 | self.vheader += '2P' |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 453 | self.longdigit = sys.int_info.sizeof_digit |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 454 | import _testcapi |
| 455 | self.gc_headsize = _testcapi.SIZEOF_PYGC_HEAD |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 456 | self.file = open(test.support.TESTFN, 'wb') |
| 457 | |
| 458 | def tearDown(self): |
| 459 | self.file.close() |
| 460 | test.support.unlink(test.support.TESTFN) |
| 461 | |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 462 | def check_sizeof(self, o, size): |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 463 | result = sys.getsizeof(o) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 464 | # add GC header size |
| 465 | if ((type(o) == type) and (o.__flags__ & self.TPFLAGS_HEAPTYPE) or\ |
| 466 | ((type(o) != type) and (type(o).__flags__ & self.TPFLAGS_HAVE_GC))): |
| 467 | size += self.gc_headsize |
| 468 | msg = 'wrong size for %s: got %d, expected %d' \ |
| 469 | % (type(o), result, size) |
| 470 | self.assertEqual(result, size, msg) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 471 | |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 472 | def calcsize(self, fmt): |
| 473 | """Wrapper around struct.calcsize which enforces the alignment of the |
| 474 | end of a structure to the alignment requirement of pointer. |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 475 | |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 476 | Note: This wrapper should only be used if a pointer member is included |
| 477 | and no member with a size larger than a pointer exists. |
| 478 | """ |
| 479 | return struct.calcsize(fmt + '0P') |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 480 | |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 481 | def test_gc_head_size(self): |
| 482 | # Check that the gc header size is added to objects tracked by the gc. |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 483 | h = self.header |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 484 | vh = self.vheader |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 485 | size = self.calcsize |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 486 | gc_header_size = self.gc_headsize |
| 487 | # bool objects are not gc tracked |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 488 | self.assertEqual(sys.getsizeof(True), size(vh) + self.longdigit) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 489 | # but lists are |
| 490 | self.assertEqual(sys.getsizeof([]), size(vh + 'PP') + gc_header_size) |
| 491 | |
| 492 | def test_default(self): |
| 493 | h = self.header |
| 494 | vh = self.vheader |
| 495 | size = self.calcsize |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 496 | self.assertEqual(sys.getsizeof(True), size(vh) + self.longdigit) |
| 497 | self.assertEqual(sys.getsizeof(True, -1), size(vh) + self.longdigit) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 498 | |
| 499 | def test_objecttypes(self): |
| 500 | # check all types defined in Objects/ |
| 501 | h = self.header |
| 502 | vh = self.vheader |
| 503 | size = self.calcsize |
| 504 | check = self.check_sizeof |
| 505 | # bool |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 506 | check(True, size(vh) + self.longdigit) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 507 | # buffer |
| 508 | # XXX |
| 509 | # builtin_function_or_method |
| 510 | check(len, size(h + '3P')) |
| 511 | # bytearray |
| 512 | samples = [b'', b'u'*100000] |
| 513 | for sample in samples: |
| 514 | x = bytearray(sample) |
| 515 | check(x, size(vh + 'iPP') + x.__alloc__() * self.c) |
| 516 | # bytearray_iterator |
| 517 | check(iter(bytearray()), size(h + 'PP')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 518 | # cell |
| 519 | def get_cell(): |
| 520 | x = 42 |
| 521 | def inner(): |
| 522 | return x |
| 523 | return inner |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 524 | check(get_cell().__closure__[0], size(h + 'P')) |
Robert Schuppenies | 4efb518 | 2008-06-06 11:49:51 +0000 | [diff] [blame] | 525 | # code |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 526 | check(get_cell().__code__, size(h + '5i8Pi2P')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 527 | # complex |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 528 | check(complex(0,1), size(h + '2d')) |
| 529 | # method_descriptor (descriptor object) |
| 530 | check(str.lower, size(h + '2PP')) |
| 531 | # classmethod_descriptor (descriptor object) |
| 532 | # XXX |
| 533 | # member_descriptor (descriptor object) |
| 534 | import datetime |
| 535 | check(datetime.timedelta.days, size(h + '2PP')) |
| 536 | # getset_descriptor (descriptor object) |
| 537 | import collections |
| 538 | check(collections.defaultdict.default_factory, size(h + '2PP')) |
| 539 | # wrapper_descriptor (descriptor object) |
| 540 | check(int.__add__, size(h + '2P2P')) |
| 541 | # method-wrapper (descriptor object) |
| 542 | check({}.__iter__, size(h + '2P')) |
| 543 | # dict |
| 544 | check({}, size(h + '3P2P' + 8*'P2P')) |
| 545 | longdict = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8} |
| 546 | check(longdict, size(h + '3P2P' + 8*'P2P') + 16*size('P2P')) |
| 547 | # dictionary-keyiterator |
| 548 | check({}.keys(), size(h + 'P')) |
| 549 | # dictionary-valueiterator |
| 550 | check({}.values(), size(h + 'P')) |
| 551 | # dictionary-itemiterator |
| 552 | check({}.items(), size(h + 'P')) |
| 553 | # dictproxy |
| 554 | class C(object): pass |
| 555 | check(C.__dict__, size(h + 'P')) |
| 556 | # BaseException |
| 557 | check(BaseException(), size(h + '5P')) |
| 558 | # UnicodeEncodeError |
| 559 | check(UnicodeEncodeError("", "", 0, 0, ""), size(h + '5P 2P2PP')) |
| 560 | # UnicodeDecodeError |
| 561 | # XXX |
| 562 | # check(UnicodeDecodeError("", "", 0, 0, ""), size(h + '5P2PP')) |
| 563 | # UnicodeTranslateError |
| 564 | check(UnicodeTranslateError("", 0, 1, ""), size(h + '5P 2P2PP')) |
| 565 | # ellipses |
| 566 | check(Ellipsis, size(h + '')) |
| 567 | # EncodingMap |
| 568 | import codecs, encodings.iso8859_3 |
| 569 | x = codecs.charmap_build(encodings.iso8859_3.decoding_table) |
| 570 | check(x, size(h + '32B2iB')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 571 | # enumerate |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 572 | check(enumerate([]), size(h + 'l3P')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 573 | # reverse |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 574 | check(reversed(''), size(h + 'PP')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 575 | # float |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 576 | check(float(0), size(h + 'd')) |
| 577 | # sys.floatinfo |
| 578 | check(sys.float_info, size(vh) + self.P * len(sys.float_info)) |
| 579 | # frame |
| 580 | import inspect |
| 581 | CO_MAXBLOCKS = 20 |
| 582 | x = inspect.currentframe() |
| 583 | ncells = len(x.f_code.co_cellvars) |
| 584 | nfrees = len(x.f_code.co_freevars) |
| 585 | extras = x.f_code.co_stacksize + x.f_code.co_nlocals +\ |
| 586 | ncells + nfrees - 1 |
| 587 | check(x, size(vh + '12P3i' + CO_MAXBLOCKS*'3i' + 'P' + extras*'P')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 588 | # function |
| 589 | def func(): pass |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 590 | check(func, size(h + '11P')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 591 | class c(): |
| 592 | @staticmethod |
| 593 | def foo(): |
| 594 | pass |
| 595 | @classmethod |
| 596 | def bar(cls): |
| 597 | pass |
| 598 | # staticmethod |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 599 | check(foo, size(h + 'P')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 600 | # classmethod |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 601 | check(bar, size(h + 'P')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 602 | # generator |
| 603 | def get_gen(): yield 1 |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 604 | check(get_gen(), size(h + 'Pi2P')) |
| 605 | # iterator |
| 606 | check(iter('abc'), size(h + 'lP')) |
| 607 | # callable-iterator |
| 608 | import re |
| 609 | check(re.finditer('',''), size(h + '2P')) |
| 610 | # list |
| 611 | samples = [[], [1,2,3], ['1', '2', '3']] |
| 612 | for sample in samples: |
| 613 | check(sample, size(vh + 'PP') + len(sample)*self.P) |
| 614 | # sortwrapper (list) |
| 615 | # XXX |
| 616 | # cmpwrapper (list) |
| 617 | # XXX |
| 618 | # listiterator (list) |
| 619 | check(iter([]), size(h + 'lP')) |
| 620 | # listreverseiterator (list) |
| 621 | check(reversed([]), size(h + 'lP')) |
| 622 | # long |
| 623 | check(0, size(vh)) |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 624 | check(1, size(vh) + self.longdigit) |
| 625 | check(-1, size(vh) + self.longdigit) |
| 626 | PyLong_BASE = 2**sys.int_info.bits_per_digit |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame^] | 627 | check(int(PyLong_BASE), size(vh) + 2*self.longdigit) |
| 628 | check(int(PyLong_BASE**2-1), size(vh) + 2*self.longdigit) |
| 629 | check(int(PyLong_BASE**2), size(vh) + 3*self.longdigit) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 630 | # memory |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 631 | check(memoryview(b''), size(h + 'P PP2P2i7P')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 632 | # module |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 633 | check(unittest, size(h + '3P')) |
| 634 | # None |
| 635 | check(None, size(h + '')) |
| 636 | # NotImplementedType |
| 637 | check(NotImplemented, size(h)) |
| 638 | # object |
| 639 | check(object(), size(h + '')) |
| 640 | # property (descriptor object) |
| 641 | class C(object): |
| 642 | def getx(self): return self.__x |
| 643 | def setx(self, value): self.__x = value |
| 644 | def delx(self): del self.__x |
| 645 | x = property(getx, setx, delx, "") |
| 646 | check(x, size(h + '4Pi')) |
| 647 | # PyCObject |
| 648 | # XXX |
| 649 | # rangeiterator |
| 650 | check(iter(range(1)), size(h + '4l')) |
| 651 | # reverse |
| 652 | check(reversed(''), size(h + 'PP')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 653 | # range |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 654 | check(range(1), size(h + '3P')) |
Robert Schuppenies | 357877c | 2008-09-16 07:21:01 +0000 | [diff] [blame] | 655 | check(range(66000), size(h + '3P')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 656 | # set |
| 657 | # frozenset |
| 658 | PySet_MINSIZE = 8 |
| 659 | samples = [[], range(10), range(50)] |
| 660 | s = size(h + '3P2P' + PySet_MINSIZE*'lP' + 'lP') |
| 661 | for sample in samples: |
| 662 | minused = len(sample) |
| 663 | if minused == 0: tmp = 1 |
| 664 | # the computation of minused is actually a bit more complicated |
| 665 | # but this suffices for the sizeof test |
| 666 | minused = minused*2 |
| 667 | newsize = PySet_MINSIZE |
| 668 | while newsize <= minused: |
| 669 | newsize = newsize << 1 |
| 670 | if newsize <= 8: |
| 671 | check(set(sample), s) |
| 672 | check(frozenset(sample), s) |
| 673 | else: |
| 674 | check(set(sample), s + newsize*struct.calcsize('lP')) |
| 675 | check(frozenset(sample), s + newsize*struct.calcsize('lP')) |
| 676 | # setiterator |
| 677 | check(iter(set()), size(h + 'P3P')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 678 | # slice |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 679 | check(slice(0), size(h + '3P')) |
| 680 | # super |
| 681 | check(super(int), size(h + '3P')) |
| 682 | # tuple |
| 683 | check((), size(vh)) |
| 684 | check((1,2,3), size(vh) + 3*self.P) |
| 685 | # type |
| 686 | # (PyTypeObject + PyNumberMethods + PyMappingMethods + |
| 687 | # PySequenceMethods + PyBufferProcs) |
| 688 | s = size(vh + 'P2P15Pl4PP9PP11PI') + size('16Pi17P 3P 10P 2P 2P') |
| 689 | check(int, s) |
| 690 | # class |
| 691 | class newstyleclass(object): pass |
| 692 | check(newstyleclass, s) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 693 | # unicode |
Benjamin Peterson | b49d994 | 2008-06-17 12:44:04 +0000 | [diff] [blame] | 694 | usize = len('\0'.encode('unicode-internal')) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 695 | samples = ['', '1'*100] |
| 696 | # we need to test for both sizes, because we don't know if the string |
| 697 | # has been cached |
| 698 | for s in samples: |
Robert Schuppenies | 4d45bfe | 2008-06-28 23:58:47 +0000 | [diff] [blame] | 699 | basicsize = size(h + 'PPliP') + usize * (len(s) + 1) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 700 | check(s, basicsize) |
| 701 | # weakref |
| 702 | import weakref |
| 703 | check(weakref.ref(int), size(h + '2Pl2P')) |
| 704 | # weakproxy |
| 705 | # XXX |
| 706 | # weakcallableproxy |
| 707 | check(weakref.proxy(int), size(h + '2Pl2P')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 708 | |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 709 | def test_pythontypes(self): |
| 710 | # check all types defined in Python/ |
| 711 | h = self.header |
| 712 | vh = self.vheader |
| 713 | size = self.calcsize |
| 714 | check = self.check_sizeof |
| 715 | # _ast.AST |
| 716 | import _ast |
| 717 | check(_ast.AST(), size(h + '')) |
| 718 | # imp.NullImporter |
| 719 | import imp |
| 720 | check(imp.NullImporter(self.file.name), size(h + '')) |
| 721 | try: |
| 722 | raise TypeError |
| 723 | except TypeError: |
| 724 | tb = sys.exc_info()[2] |
| 725 | # traceback |
| 726 | if tb != None: |
| 727 | check(tb, size(h + '2P2i')) |
| 728 | # symtable entry |
| 729 | # XXX |
| 730 | # sys.flags |
| 731 | check(sys.flags, size(vh) + self.P * len(sys.flags)) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 732 | |
Martin v. Löwis | 04dc25c | 2008-10-03 16:09:28 +0000 | [diff] [blame] | 733 | def test_setfilesystemencoding(self): |
| 734 | old = sys.getfilesystemencoding() |
| 735 | sys.setfilesystemencoding("iso-8859-1") |
| 736 | self.assertEqual(sys.getfilesystemencoding(), "iso-8859-1") |
| 737 | sys.setfilesystemencoding(old) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 738 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 739 | def test_main(): |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 740 | test.support.run_unittest(SysModuleTest, SizeofTest) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 741 | |
| 742 | if __name__ == "__main__": |
| 743 | test_main() |