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