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