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