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