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 |
Antoine Pitrou | 9284053 | 2012-12-17 23:05:59 +0100 | [diff] [blame] | 10 | import sysconfig |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 11 | |
Georg Brandl | 0352655 | 2009-10-27 23:00:28 +0000 | [diff] [blame] | 12 | # count the number of test runs, used to create unique |
| 13 | # strings to intern in test_intern() |
| 14 | numruns = 0 |
| 15 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 16 | try: |
| 17 | import threading |
| 18 | except ImportError: |
| 19 | threading = None |
Georg Brandl | 0352655 | 2009-10-27 23:00:28 +0000 | [diff] [blame] | 20 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 21 | class SysModuleTest(unittest.TestCase): |
| 22 | |
Christian Heimes | ef181a7 | 2007-11-07 16:14:12 +0000 | [diff] [blame] | 23 | def setUp(self): |
| 24 | self.orig_stdout = sys.stdout |
| 25 | self.orig_stderr = sys.stderr |
| 26 | self.orig_displayhook = sys.displayhook |
| 27 | |
| 28 | def tearDown(self): |
| 29 | sys.stdout = self.orig_stdout |
| 30 | sys.stderr = self.orig_stderr |
| 31 | sys.displayhook = self.orig_displayhook |
Antoine Pitrou | 836f0e2 | 2009-10-27 19:25:57 +0000 | [diff] [blame] | 32 | test.support.reap_children() |
Christian Heimes | ef181a7 | 2007-11-07 16:14:12 +0000 | [diff] [blame] | 33 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 34 | def test_original_displayhook(self): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 35 | import builtins |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 36 | out = io.StringIO() |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 37 | sys.stdout = out |
| 38 | |
| 39 | dh = sys.__displayhook__ |
| 40 | |
| 41 | self.assertRaises(TypeError, dh) |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 42 | if hasattr(builtins, "_"): |
| 43 | del builtins._ |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 44 | |
| 45 | dh(None) |
| 46 | self.assertEqual(out.getvalue(), "") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 47 | self.assertTrue(not hasattr(builtins, "_")) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 48 | dh(42) |
| 49 | self.assertEqual(out.getvalue(), "42\n") |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 50 | self.assertEqual(builtins._, 42) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 51 | |
| 52 | del sys.stdout |
| 53 | self.assertRaises(RuntimeError, dh, 42) |
| 54 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 55 | def test_lost_displayhook(self): |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 56 | del sys.displayhook |
| 57 | code = compile("42", "<string>", "single") |
| 58 | self.assertRaises(RuntimeError, eval, code) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 59 | |
| 60 | def test_custom_displayhook(self): |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 61 | def baddisplayhook(obj): |
| 62 | raise ValueError |
| 63 | sys.displayhook = baddisplayhook |
| 64 | code = compile("42", "<string>", "single") |
| 65 | self.assertRaises(ValueError, eval, code) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 66 | |
| 67 | def test_original_excepthook(self): |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 68 | err = io.StringIO() |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 69 | sys.stderr = err |
| 70 | |
| 71 | eh = sys.__excepthook__ |
| 72 | |
| 73 | self.assertRaises(TypeError, eh) |
| 74 | try: |
| 75 | raise ValueError(42) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 76 | except ValueError as exc: |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 77 | eh(*sys.exc_info()) |
| 78 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 79 | self.assertTrue(err.getvalue().endswith("ValueError: 42\n")) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 80 | |
Benjamin Peterson | bc74e5b | 2008-08-23 20:19:17 +0000 | [diff] [blame] | 81 | def test_excepthook(self): |
| 82 | with test.support.captured_output("stderr") as stderr: |
| 83 | sys.excepthook(1, '1', 1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 84 | self.assertTrue("TypeError: print_exception(): Exception expected for " \ |
Benjamin Peterson | bc74e5b | 2008-08-23 20:19:17 +0000 | [diff] [blame] | 85 | "value, str found" in stderr.getvalue()) |
| 86 | |
Walter Dörwald | e7028ac | 2003-02-03 23:05:27 +0000 | [diff] [blame] | 87 | # FIXME: testing the code for a lost or replaced excepthook in |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 88 | # Python/pythonrun.c::PyErr_PrintEx() is tricky. |
| 89 | |
| 90 | def test_exit(self): |
Victor Stinner | e9fb319 | 2010-05-17 08:58:51 +0000 | [diff] [blame] | 91 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 92 | self.assertRaises(TypeError, sys.exit, 42, 42) |
| 93 | |
| 94 | # call without argument |
| 95 | try: |
| 96 | sys.exit(0) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 97 | except SystemExit as exc: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 98 | self.assertEqual(exc.code, 0) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 99 | except: |
| 100 | self.fail("wrong exception") |
| 101 | else: |
| 102 | self.fail("no exception") |
| 103 | |
| 104 | # call with tuple argument with one entry |
| 105 | # entry will be unpacked |
| 106 | try: |
| 107 | sys.exit(42) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 108 | except SystemExit as exc: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 109 | self.assertEqual(exc.code, 42) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 110 | except: |
| 111 | self.fail("wrong exception") |
| 112 | else: |
| 113 | self.fail("no exception") |
| 114 | |
| 115 | # call with integer argument |
| 116 | try: |
| 117 | sys.exit((42,)) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 118 | except SystemExit as exc: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 119 | self.assertEqual(exc.code, 42) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 120 | except: |
| 121 | self.fail("wrong exception") |
| 122 | else: |
| 123 | self.fail("no exception") |
| 124 | |
| 125 | # call with string argument |
| 126 | try: |
| 127 | sys.exit("exit") |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 128 | except SystemExit as exc: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 129 | self.assertEqual(exc.code, "exit") |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 130 | except: |
| 131 | self.fail("wrong exception") |
| 132 | else: |
| 133 | self.fail("no exception") |
| 134 | |
| 135 | # call with tuple argument with two entries |
| 136 | try: |
| 137 | sys.exit((17, 23)) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 138 | except SystemExit as exc: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 139 | self.assertEqual(exc.code, (17, 23)) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 140 | except: |
| 141 | self.fail("wrong exception") |
| 142 | else: |
| 143 | self.fail("no exception") |
| 144 | |
Michael W. Hudson | f058858 | 2005-02-15 15:26:11 +0000 | [diff] [blame] | 145 | # test that the exit machinery handles SystemExits properly |
Michael W. Hudson | f058858 | 2005-02-15 15:26:11 +0000 | [diff] [blame] | 146 | rc = subprocess.call([sys.executable, "-c", |
| 147 | "raise SystemExit(47)"]) |
| 148 | self.assertEqual(rc, 47) |
Tim Peters | f0db38d | 2005-02-15 21:50:12 +0000 | [diff] [blame] | 149 | |
Victor Stinner | 7126dbc | 2010-05-21 23:45:42 +0000 | [diff] [blame] | 150 | def check_exit_message(code, expected, env=None): |
Victor Stinner | e9fb319 | 2010-05-17 08:58:51 +0000 | [diff] [blame] | 151 | process = subprocess.Popen([sys.executable, "-c", code], |
Victor Stinner | 7126dbc | 2010-05-21 23:45:42 +0000 | [diff] [blame] | 152 | stderr=subprocess.PIPE, env=env) |
Victor Stinner | e9fb319 | 2010-05-17 08:58:51 +0000 | [diff] [blame] | 153 | stdout, stderr = process.communicate() |
| 154 | self.assertEqual(process.returncode, 1) |
Victor Stinner | 7cf7d72 | 2010-05-17 14:36:43 +0000 | [diff] [blame] | 155 | self.assertTrue(stderr.startswith(expected), |
Victor Stinner | c098385 | 2010-05-17 19:57:40 +0000 | [diff] [blame] | 156 | "%s doesn't start with %s" % (ascii(stderr), ascii(expected))) |
Victor Stinner | e9fb319 | 2010-05-17 08:58:51 +0000 | [diff] [blame] | 157 | |
| 158 | # test that stderr buffer if flushed before the exit message is written |
| 159 | # into stderr |
| 160 | check_exit_message( |
| 161 | r'import sys; sys.stderr.write("unflushed,"); sys.exit("message")', |
| 162 | b"unflushed,message") |
| 163 | |
Victor Stinner | 372ac5e | 2010-05-17 01:26:01 +0000 | [diff] [blame] | 164 | # test that the exit message is written with backslashreplace error |
| 165 | # handler to stderr |
Victor Stinner | e9fb319 | 2010-05-17 08:58:51 +0000 | [diff] [blame] | 166 | check_exit_message( |
| 167 | r'import sys; sys.exit("surrogates:\uDCFF")', |
| 168 | b"surrogates:\\udcff") |
Victor Stinner | 372ac5e | 2010-05-17 01:26:01 +0000 | [diff] [blame] | 169 | |
Victor Stinner | 7126dbc | 2010-05-21 23:45:42 +0000 | [diff] [blame] | 170 | # test that the unicode message is encoded to the stderr encoding |
| 171 | # instead of the default encoding (utf8) |
| 172 | env = os.environ.copy() |
| 173 | env['PYTHONIOENCODING'] = 'latin-1' |
| 174 | check_exit_message( |
| 175 | r'import sys; sys.exit("h\xe9")', |
| 176 | b"h\xe9", env=env) |
| 177 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 178 | def test_getdefaultencoding(self): |
Walter Dörwald | 4aeaa96 | 2007-05-22 16:13:46 +0000 | [diff] [blame] | 179 | self.assertRaises(TypeError, sys.getdefaultencoding, 42) |
| 180 | # 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] | 181 | self.assertIsInstance(sys.getdefaultencoding(), str) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 182 | |
Alexander Belopolsky | 07de165 | 2010-07-25 15:05:42 +0000 | [diff] [blame] | 183 | # testing sys.settrace() is done in test_sys_settrace.py |
| 184 | # testing sys.setprofile() is done in test_sys_setprofile.py |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 185 | |
| 186 | def test_setcheckinterval(self): |
Antoine Pitrou | c3b0757 | 2009-11-13 22:19:19 +0000 | [diff] [blame] | 187 | with warnings.catch_warnings(): |
| 188 | warnings.simplefilter("ignore") |
| 189 | self.assertRaises(TypeError, sys.setcheckinterval) |
| 190 | orig = sys.getcheckinterval() |
| 191 | for n in 0, 100, 120, orig: # orig last to restore starting state |
| 192 | sys.setcheckinterval(n) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 193 | self.assertEqual(sys.getcheckinterval(), n) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 194 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 195 | @unittest.skipUnless(threading, 'Threading required for this test.') |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 196 | def test_switchinterval(self): |
| 197 | self.assertRaises(TypeError, sys.setswitchinterval) |
| 198 | self.assertRaises(TypeError, sys.setswitchinterval, "a") |
| 199 | self.assertRaises(ValueError, sys.setswitchinterval, -1.0) |
| 200 | self.assertRaises(ValueError, sys.setswitchinterval, 0.0) |
| 201 | orig = sys.getswitchinterval() |
| 202 | # sanity check |
| 203 | self.assertTrue(orig < 0.5, orig) |
| 204 | try: |
| 205 | for n in 0.00001, 0.05, 3.0, orig: |
| 206 | sys.setswitchinterval(n) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 207 | self.assertAlmostEqual(sys.getswitchinterval(), n) |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 208 | finally: |
| 209 | sys.setswitchinterval(orig) |
| 210 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 211 | def test_recursionlimit(self): |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 212 | self.assertRaises(TypeError, sys.getrecursionlimit, 42) |
| 213 | oldlimit = sys.getrecursionlimit() |
| 214 | self.assertRaises(TypeError, sys.setrecursionlimit) |
| 215 | self.assertRaises(ValueError, sys.setrecursionlimit, -42) |
| 216 | sys.setrecursionlimit(10000) |
| 217 | self.assertEqual(sys.getrecursionlimit(), 10000) |
| 218 | sys.setrecursionlimit(oldlimit) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 219 | |
Brett Cannon | 5543e81 | 2011-01-25 18:26:35 +0000 | [diff] [blame] | 220 | @unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(), |
| 221 | 'fatal error if run with a trace function') |
Antoine Pitrou | 652e707 | 2009-03-13 19:25:20 +0000 | [diff] [blame] | 222 | def test_recursionlimit_recovery(self): |
| 223 | # NOTE: this test is slightly fragile in that it depends on the current |
| 224 | # recursion count when executing the test being low enough so as to |
| 225 | # trigger the recursion recovery detection in the _Py_MakeEndRecCheck |
| 226 | # macro (see ceval.h). |
| 227 | oldlimit = sys.getrecursionlimit() |
| 228 | def f(): |
| 229 | f() |
| 230 | try: |
| 231 | for i in (50, 1000): |
| 232 | # Issue #5392: stack overflow after hitting recursion limit twice |
| 233 | sys.setrecursionlimit(i) |
| 234 | self.assertRaises(RuntimeError, f) |
| 235 | self.assertRaises(RuntimeError, f) |
| 236 | finally: |
| 237 | sys.setrecursionlimit(oldlimit) |
| 238 | |
| 239 | def test_recursionlimit_fatalerror(self): |
| 240 | # A fatal error occurs if a second recursion limit is hit when recovering |
| 241 | # from a first one. |
| 242 | code = textwrap.dedent(""" |
| 243 | import sys |
| 244 | |
| 245 | def f(): |
| 246 | try: |
| 247 | f() |
| 248 | except RuntimeError: |
| 249 | f() |
| 250 | |
| 251 | sys.setrecursionlimit(%d) |
| 252 | f()""") |
Antoine Pitrou | 77e904e | 2013-10-08 23:04:32 +0200 | [diff] [blame] | 253 | with test.support.SuppressCrashReport(): |
Ezio Melotti | 958f7ae | 2013-03-20 18:14:48 +0200 | [diff] [blame] | 254 | for i in (50, 1000): |
| 255 | sub = subprocess.Popen([sys.executable, '-c', code % i], |
| 256 | stderr=subprocess.PIPE) |
| 257 | err = sub.communicate()[1] |
| 258 | self.assertTrue(sub.returncode, sub.returncode) |
| 259 | self.assertIn( |
| 260 | b"Fatal Python error: Cannot recover from stack overflow", |
| 261 | err) |
Antoine Pitrou | 652e707 | 2009-03-13 19:25:20 +0000 | [diff] [blame] | 262 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 263 | def test_getwindowsversion(self): |
Eric Smith | 05f33744 | 2010-01-27 01:25:02 +0000 | [diff] [blame] | 264 | # Raise SkipTest if sys doesn't have getwindowsversion attribute |
Eric Smith | d8aef76 | 2010-01-27 01:38:34 +0000 | [diff] [blame] | 265 | test.support.get_attribute(sys, "getwindowsversion") |
Eric Smith | 05f33744 | 2010-01-27 01:25:02 +0000 | [diff] [blame] | 266 | v = sys.getwindowsversion() |
Brian Curtin | 808e1ad | 2010-07-08 22:13:25 +0000 | [diff] [blame] | 267 | self.assertEqual(len(v), 5) |
Eric Smith | 05f33744 | 2010-01-27 01:25:02 +0000 | [diff] [blame] | 268 | self.assertIsInstance(v[0], int) |
| 269 | self.assertIsInstance(v[1], int) |
| 270 | self.assertIsInstance(v[2], int) |
| 271 | self.assertIsInstance(v[3], int) |
| 272 | self.assertIsInstance(v[4], str) |
| 273 | self.assertRaises(IndexError, operator.getitem, v, 5) |
| 274 | self.assertIsInstance(v.major, int) |
| 275 | self.assertIsInstance(v.minor, int) |
| 276 | self.assertIsInstance(v.build, int) |
| 277 | self.assertIsInstance(v.platform, int) |
| 278 | self.assertIsInstance(v.service_pack, str) |
Eric Smith | a1f20b9 | 2010-01-27 02:14:26 +0000 | [diff] [blame] | 279 | self.assertIsInstance(v.service_pack_minor, int) |
| 280 | self.assertIsInstance(v.service_pack_major, int) |
| 281 | self.assertIsInstance(v.suite_mask, int) |
| 282 | self.assertIsInstance(v.product_type, int) |
Eric Smith | 05f33744 | 2010-01-27 01:25:02 +0000 | [diff] [blame] | 283 | self.assertEqual(v[0], v.major) |
| 284 | self.assertEqual(v[1], v.minor) |
| 285 | self.assertEqual(v[2], v.build) |
| 286 | self.assertEqual(v[3], v.platform) |
| 287 | self.assertEqual(v[4], v.service_pack) |
Eric Smith | f7bb578 | 2010-01-27 00:44:57 +0000 | [diff] [blame] | 288 | |
Eric Smith | 05f33744 | 2010-01-27 01:25:02 +0000 | [diff] [blame] | 289 | # This is how platform.py calls it. Make sure tuple |
| 290 | # still has 5 elements |
| 291 | maj, min, buildno, plat, csd = sys.getwindowsversion() |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 292 | |
Barry Warsaw | e94a37f | 2008-10-17 01:29:56 +0000 | [diff] [blame] | 293 | def test_call_tracing(self): |
| 294 | self.assertRaises(TypeError, sys.call_tracing, type, 2) |
| 295 | |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame^] | 296 | @unittest.skipUnless(hasattr(sys, "setdlopenflags"), |
| 297 | 'test needs sys.setdlopenflags()') |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 298 | def test_dlopenflags(self): |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame^] | 299 | self.assertTrue(hasattr(sys, "getdlopenflags")) |
| 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) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 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", |
Christian Heimes | ad73a9c | 2013-08-10 16:36:18 +0200 | [diff] [blame] | 519 | "bytes_warning", "quiet", "hash_randomization", "isolated") |
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 | |
Serhiy Storchaka | bf28d2d | 2013-09-13 11:46:24 +0300 | [diff] [blame] | 548 | env["PYTHONIOENCODING"] = "ascii" |
| 549 | p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], |
| 550 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 551 | env=env) |
| 552 | out, err = p.communicate() |
| 553 | self.assertEqual(out, b'') |
| 554 | self.assertIn(b'UnicodeEncodeError:', err) |
| 555 | self.assertIn(rb"'\xa2'", err) |
| 556 | |
| 557 | env["PYTHONIOENCODING"] = "ascii:" |
| 558 | p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], |
| 559 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 560 | env=env) |
| 561 | out, err = p.communicate() |
| 562 | self.assertEqual(out, b'') |
| 563 | self.assertIn(b'UnicodeEncodeError:', err) |
| 564 | self.assertIn(rb"'\xa2'", err) |
| 565 | |
| 566 | env["PYTHONIOENCODING"] = ":surrogateescape" |
| 567 | p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xdcbd))'], |
| 568 | stdout=subprocess.PIPE, env=env) |
| 569 | out = p.communicate()[0].strip() |
| 570 | self.assertEqual(out, b'\xbd') |
| 571 | |
| 572 | @unittest.skipUnless(test.support.FS_NONASCII, |
| 573 | 'requires OS support of non-ASCII encodings') |
| 574 | def test_ioencoding_nonascii(self): |
| 575 | env = dict(os.environ) |
| 576 | |
| 577 | env["PYTHONIOENCODING"] = "" |
| 578 | p = subprocess.Popen([sys.executable, "-c", |
| 579 | 'print(%a)' % test.support.FS_NONASCII], |
| 580 | stdout=subprocess.PIPE, env=env) |
| 581 | out = p.communicate()[0].strip() |
| 582 | self.assertEqual(out, os.fsencode(test.support.FS_NONASCII)) |
| 583 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 584 | @unittest.skipIf(sys.base_prefix != sys.prefix, |
| 585 | 'Test is not venv-compatible') |
Victor Stinner | 171ba05 | 2010-03-12 14:20:59 +0000 | [diff] [blame] | 586 | def test_executable(self): |
Petri Lehtinen | 9713321 | 2012-02-02 20:59:48 +0200 | [diff] [blame] | 587 | # sys.executable should be absolute |
| 588 | self.assertEqual(os.path.abspath(sys.executable), sys.executable) |
| 589 | |
Victor Stinner | 171ba05 | 2010-03-12 14:20:59 +0000 | [diff] [blame] | 590 | # Issue #7774: Ensure that sys.executable is an empty string if argv[0] |
| 591 | # has been set to an non existent program name and Python is unable to |
| 592 | # retrieve the real program name |
Florent Xicluna | 02ea12b2 | 2010-07-28 16:39:41 +0000 | [diff] [blame] | 593 | |
Victor Stinner | 171ba05 | 2010-03-12 14:20:59 +0000 | [diff] [blame] | 594 | # For a normal installation, it should work without 'cwd' |
| 595 | # argument. For test runs in the build directory, see #7774. |
| 596 | python_dir = os.path.dirname(os.path.realpath(sys.executable)) |
| 597 | p = subprocess.Popen( |
| 598 | ["nonexistent", "-c", |
| 599 | 'import sys; print(sys.executable.encode("ascii", "backslashreplace"))'], |
| 600 | executable=sys.executable, stdout=subprocess.PIPE, cwd=python_dir) |
| 601 | stdout = p.communicate()[0] |
| 602 | executable = stdout.strip().decode("ASCII") |
| 603 | p.wait() |
| 604 | self.assertIn(executable, ["b''", repr(sys.executable.encode("ascii", "backslashreplace"))]) |
| 605 | |
Victor Stinner | 85ca2af | 2010-08-19 11:23:47 +0000 | [diff] [blame] | 606 | def check_fsencoding(self, fs_encoding, expected=None): |
| 607 | self.assertIsNotNone(fs_encoding) |
| 608 | codecs.lookup(fs_encoding) |
| 609 | if expected: |
| 610 | self.assertEqual(fs_encoding, expected) |
| 611 | |
Victor Stinner | c732e3b | 2010-08-18 22:44:15 +0000 | [diff] [blame] | 612 | def test_getfilesystemencoding(self): |
Victor Stinner | c732e3b | 2010-08-18 22:44:15 +0000 | [diff] [blame] | 613 | fs_encoding = sys.getfilesystemencoding() |
Victor Stinner | 85ca2af | 2010-08-19 11:23:47 +0000 | [diff] [blame] | 614 | if sys.platform == 'darwin': |
| 615 | expected = 'utf-8' |
| 616 | elif sys.platform == 'win32': |
| 617 | expected = 'mbcs' |
| 618 | else: |
| 619 | expected = None |
Victor Stinner | 9943524 | 2010-08-19 11:30:09 +0000 | [diff] [blame] | 620 | self.check_fsencoding(fs_encoding, expected) |
Victor Stinner | c732e3b | 2010-08-18 22:44:15 +0000 | [diff] [blame] | 621 | |
Barry Warsaw | 409da15 | 2012-06-03 16:18:47 -0400 | [diff] [blame] | 622 | def test_implementation(self): |
| 623 | # This test applies to all implementations equally. |
| 624 | |
Georg Brandl | e054f45 | 2012-09-29 09:44:17 +0200 | [diff] [blame] | 625 | levels = {'alpha': 0xA, 'beta': 0xB, 'candidate': 0xC, 'final': 0xF} |
Barry Warsaw | 409da15 | 2012-06-03 16:18:47 -0400 | [diff] [blame] | 626 | |
| 627 | self.assertTrue(hasattr(sys.implementation, 'name')) |
| 628 | self.assertTrue(hasattr(sys.implementation, 'version')) |
| 629 | self.assertTrue(hasattr(sys.implementation, 'hexversion')) |
| 630 | self.assertTrue(hasattr(sys.implementation, 'cache_tag')) |
| 631 | |
| 632 | version = sys.implementation.version |
| 633 | self.assertEqual(version[:2], (version.major, version.minor)) |
| 634 | |
| 635 | hexversion = (version.major << 24 | version.minor << 16 | |
| 636 | version.micro << 8 | levels[version.releaselevel] << 4 | |
| 637 | version.serial << 0) |
| 638 | self.assertEqual(sys.implementation.hexversion, hexversion) |
| 639 | |
Barry Warsaw | 028b37c | 2012-06-04 12:01:56 -0400 | [diff] [blame] | 640 | # PEP 421 requires that .name be lower case. |
Barry Warsaw | efce00c | 2012-06-04 12:02:42 -0400 | [diff] [blame] | 641 | self.assertEqual(sys.implementation.name, |
Barry Warsaw | 028b37c | 2012-06-04 12:01:56 -0400 | [diff] [blame] | 642 | sys.implementation.name.lower()) |
| 643 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 644 | def test_debugmallocstats(self): |
| 645 | # Test sys._debugmallocstats() |
| 646 | from test.script_helper import assert_python_ok |
| 647 | args = ['-c', 'import sys; sys._debugmallocstats()'] |
| 648 | ret, out, err = assert_python_ok(*args) |
| 649 | self.assertIn(b"free PyDictObjects", err) |
Martin v. Löwis | 0f59989 | 2008-06-02 11:13:03 +0000 | [diff] [blame] | 650 | |
Antoine Pitrou | f9d0b12 | 2012-12-09 14:28:26 +0100 | [diff] [blame] | 651 | @unittest.skipUnless(hasattr(sys, "getallocatedblocks"), |
| 652 | "sys.getallocatedblocks unavailable on this build") |
| 653 | def test_getallocatedblocks(self): |
| 654 | # Some sanity checks |
Antoine Pitrou | 9284053 | 2012-12-17 23:05:59 +0100 | [diff] [blame] | 655 | with_pymalloc = sysconfig.get_config_var('WITH_PYMALLOC') |
Antoine Pitrou | f9d0b12 | 2012-12-09 14:28:26 +0100 | [diff] [blame] | 656 | a = sys.getallocatedblocks() |
| 657 | self.assertIs(type(a), int) |
Antoine Pitrou | 9284053 | 2012-12-17 23:05:59 +0100 | [diff] [blame] | 658 | if with_pymalloc: |
| 659 | self.assertGreater(a, 0) |
| 660 | else: |
Antoine Pitrou | f61dc4c | 2012-12-18 19:50:58 +0100 | [diff] [blame] | 661 | # When WITH_PYMALLOC isn't available, we don't know anything |
| 662 | # about the underlying implementation: the function might |
| 663 | # return 0 or something greater. |
| 664 | self.assertGreaterEqual(a, 0) |
Antoine Pitrou | f9d0b12 | 2012-12-09 14:28:26 +0100 | [diff] [blame] | 665 | try: |
| 666 | # While we could imagine a Python session where the number of |
| 667 | # multiple buffer objects would exceed the sharing of references, |
| 668 | # it is unlikely to happen in a normal test run. |
| 669 | self.assertLess(a, sys.gettotalrefcount()) |
| 670 | except AttributeError: |
| 671 | # gettotalrefcount() not available |
| 672 | pass |
| 673 | gc.collect() |
| 674 | b = sys.getallocatedblocks() |
| 675 | self.assertLessEqual(b, a) |
| 676 | gc.collect() |
| 677 | c = sys.getallocatedblocks() |
| 678 | self.assertIn(c, range(b - 50, b + 50)) |
| 679 | |
| 680 | |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 681 | class SizeofTest(unittest.TestCase): |
| 682 | |
| 683 | def setUp(self): |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 684 | self.P = struct.calcsize('P') |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 685 | self.longdigit = sys.int_info.sizeof_digit |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 686 | import _testcapi |
| 687 | self.gc_headsize = _testcapi.SIZEOF_PYGC_HEAD |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 688 | self.file = open(test.support.TESTFN, 'wb') |
| 689 | |
| 690 | def tearDown(self): |
| 691 | self.file.close() |
| 692 | test.support.unlink(test.support.TESTFN) |
| 693 | |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 694 | check_sizeof = test.support.check_sizeof |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 695 | |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 696 | def test_gc_head_size(self): |
| 697 | # 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] | 698 | vsize = test.support.calcvobjsize |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 699 | gc_header_size = self.gc_headsize |
| 700 | # bool objects are not gc tracked |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 701 | self.assertEqual(sys.getsizeof(True), vsize('') + self.longdigit) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 702 | # but lists are |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 703 | self.assertEqual(sys.getsizeof([]), vsize('Pn') + gc_header_size) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 704 | |
| 705 | def test_default(self): |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 706 | size = test.support.calcvobjsize |
| 707 | self.assertEqual(sys.getsizeof(True), size('') + self.longdigit) |
| 708 | self.assertEqual(sys.getsizeof(True, -1), size('') + self.longdigit) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 709 | |
| 710 | def test_objecttypes(self): |
| 711 | # check all types defined in Objects/ |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 712 | size = test.support.calcobjsize |
| 713 | vsize = test.support.calcvobjsize |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 714 | check = self.check_sizeof |
| 715 | # bool |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 716 | check(True, vsize('') + self.longdigit) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 717 | # buffer |
| 718 | # XXX |
| 719 | # builtin_function_or_method |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 720 | check(len, size('3P')) # XXX check layout |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 721 | # bytearray |
| 722 | samples = [b'', b'u'*100000] |
| 723 | for sample in samples: |
| 724 | x = bytearray(sample) |
Antoine Pitrou | 5df8a8a | 2013-10-05 21:12:18 +0200 | [diff] [blame] | 725 | check(x, vsize('n2Pi') + x.__alloc__()) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 726 | # bytearray_iterator |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 727 | check(iter(bytearray()), size('nP')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 728 | # cell |
| 729 | def get_cell(): |
| 730 | x = 42 |
| 731 | def inner(): |
| 732 | return x |
| 733 | return inner |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 734 | check(get_cell().__closure__[0], size('P')) |
Robert Schuppenies | 4efb518 | 2008-06-06 11:49:51 +0000 | [diff] [blame] | 735 | # code |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 736 | check(get_cell().__code__, size('5i9Pi3P')) |
| 737 | check(get_cell.__code__, size('5i9Pi3P')) |
Martin v. Löwis | 3bbd2fa | 2012-07-26 22:23:23 +0200 | [diff] [blame] | 738 | def get_cell2(x): |
| 739 | def inner(): |
| 740 | return x |
| 741 | return inner |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 742 | check(get_cell2.__code__, size('5i9Pi3P') + 1) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 743 | # complex |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 744 | check(complex(0,1), size('2d')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 745 | # method_descriptor (descriptor object) |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 746 | check(str.lower, size('3PP')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 747 | # classmethod_descriptor (descriptor object) |
| 748 | # XXX |
| 749 | # member_descriptor (descriptor object) |
| 750 | import datetime |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 751 | check(datetime.timedelta.days, size('3PP')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 752 | # getset_descriptor (descriptor object) |
| 753 | import collections |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 754 | check(collections.defaultdict.default_factory, size('3PP')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 755 | # wrapper_descriptor (descriptor object) |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 756 | check(int.__add__, size('3P2P')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 757 | # method-wrapper (descriptor object) |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 758 | check({}.__iter__, size('2P')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 759 | # dict |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 760 | check({}, size('n2P' + '2nPn' + 8*'n2P')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 761 | 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] | 762 | check(longdict, size('n2P' + '2nPn') + 16*struct.calcsize('n2P')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 763 | # dictionary-keyiterator |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 764 | check({}.keys(), size('P')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 765 | # dictionary-valueiterator |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 766 | check({}.values(), size('P')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 767 | # dictionary-itemiterator |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 768 | check({}.items(), size('P')) |
| 769 | # dictionary iterator |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 770 | check(iter({}), size('P2nPn')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 771 | # dictproxy |
| 772 | class C(object): pass |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 773 | check(C.__dict__, size('P')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 774 | # BaseException |
Antoine Pitrou | 80bc00f | 2013-05-25 23:47:29 +0200 | [diff] [blame] | 775 | check(BaseException(), size('5Pb')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 776 | # UnicodeEncodeError |
Antoine Pitrou | 80bc00f | 2013-05-25 23:47:29 +0200 | [diff] [blame] | 777 | check(UnicodeEncodeError("", "", 0, 0, ""), size('5Pb 2P2nP')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 778 | # UnicodeDecodeError |
Antoine Pitrou | 80bc00f | 2013-05-25 23:47:29 +0200 | [diff] [blame] | 779 | check(UnicodeDecodeError("", b"", 0, 0, ""), size('5Pb 2P2nP')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 780 | # UnicodeTranslateError |
Antoine Pitrou | 80bc00f | 2013-05-25 23:47:29 +0200 | [diff] [blame] | 781 | check(UnicodeTranslateError("", 0, 1, ""), size('5Pb 2P2nP')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 782 | # ellipses |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 783 | check(Ellipsis, size('')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 784 | # EncodingMap |
| 785 | import codecs, encodings.iso8859_3 |
| 786 | x = codecs.charmap_build(encodings.iso8859_3.decoding_table) |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 787 | check(x, size('32B2iB')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 788 | # enumerate |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 789 | check(enumerate([]), size('n3P')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 790 | # reverse |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 791 | check(reversed(''), size('nP')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 792 | # float |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 793 | check(float(0), size('d')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 794 | # sys.floatinfo |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 795 | check(sys.float_info, vsize('') + self.P * len(sys.float_info)) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 796 | # frame |
| 797 | import inspect |
| 798 | CO_MAXBLOCKS = 20 |
| 799 | x = inspect.currentframe() |
| 800 | ncells = len(x.f_code.co_cellvars) |
| 801 | nfrees = len(x.f_code.co_freevars) |
| 802 | extras = x.f_code.co_stacksize + x.f_code.co_nlocals +\ |
| 803 | ncells + nfrees - 1 |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 804 | check(x, vsize('13P3ic' + CO_MAXBLOCKS*'3i' + 'P' + extras*'P')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 805 | # function |
| 806 | def func(): pass |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 807 | check(func, size('12P')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 808 | class c(): |
| 809 | @staticmethod |
| 810 | def foo(): |
| 811 | pass |
| 812 | @classmethod |
| 813 | def bar(cls): |
| 814 | pass |
| 815 | # staticmethod |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 816 | check(foo, size('PP')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 817 | # classmethod |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 818 | check(bar, size('PP')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 819 | # generator |
| 820 | def get_gen(): yield 1 |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 821 | check(get_gen(), size('Pb2P')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 822 | # iterator |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 823 | check(iter('abc'), size('lP')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 824 | # callable-iterator |
| 825 | import re |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 826 | check(re.finditer('',''), size('2P')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 827 | # list |
| 828 | samples = [[], [1,2,3], ['1', '2', '3']] |
| 829 | for sample in samples: |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 830 | check(sample, vsize('Pn') + len(sample)*self.P) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 831 | # sortwrapper (list) |
| 832 | # XXX |
| 833 | # cmpwrapper (list) |
| 834 | # XXX |
| 835 | # listiterator (list) |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 836 | check(iter([]), size('lP')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 837 | # listreverseiterator (list) |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 838 | check(reversed([]), size('nP')) |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 839 | # int |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 840 | check(0, vsize('')) |
| 841 | check(1, vsize('') + self.longdigit) |
| 842 | check(-1, vsize('') + self.longdigit) |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 843 | PyLong_BASE = 2**sys.int_info.bits_per_digit |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 844 | check(int(PyLong_BASE), vsize('') + 2*self.longdigit) |
| 845 | check(int(PyLong_BASE**2-1), vsize('') + 2*self.longdigit) |
| 846 | check(int(PyLong_BASE**2), vsize('') + 3*self.longdigit) |
Stefan Krah | 9a2d99e | 2012-02-25 12:24:21 +0100 | [diff] [blame] | 847 | # memoryview |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 848 | check(memoryview(b''), size('Pnin 2P2n2i5P 3cPn')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 849 | # module |
Antoine Pitrou | dcedaf6 | 2013-07-31 23:14:08 +0200 | [diff] [blame] | 850 | check(unittest, size('PnPPP')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 851 | # None |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 852 | check(None, size('')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 853 | # NotImplementedType |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 854 | check(NotImplemented, size('')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 855 | # object |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 856 | check(object(), size('')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 857 | # property (descriptor object) |
| 858 | class C(object): |
| 859 | def getx(self): return self.__x |
| 860 | def setx(self, value): self.__x = value |
| 861 | def delx(self): del self.__x |
| 862 | x = property(getx, setx, delx, "") |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 863 | check(x, size('4Pi')) |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 864 | # PyCapsule |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 865 | # XXX |
| 866 | # rangeiterator |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 867 | check(iter(range(1)), size('4l')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 868 | # reverse |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 869 | check(reversed(''), size('nP')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 870 | # range |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 871 | check(range(1), size('4P')) |
| 872 | check(range(66000), size('4P')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 873 | # set |
| 874 | # frozenset |
| 875 | PySet_MINSIZE = 8 |
| 876 | samples = [[], range(10), range(50)] |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 877 | s = size('3n2P' + PySet_MINSIZE*'nP' + 'nP') |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 878 | for sample in samples: |
| 879 | minused = len(sample) |
| 880 | if minused == 0: tmp = 1 |
| 881 | # the computation of minused is actually a bit more complicated |
| 882 | # but this suffices for the sizeof test |
| 883 | minused = minused*2 |
| 884 | newsize = PySet_MINSIZE |
| 885 | while newsize <= minused: |
| 886 | newsize = newsize << 1 |
| 887 | if newsize <= 8: |
| 888 | check(set(sample), s) |
| 889 | check(frozenset(sample), s) |
| 890 | else: |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 891 | check(set(sample), s + newsize*struct.calcsize('nP')) |
| 892 | check(frozenset(sample), s + newsize*struct.calcsize('nP')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 893 | # setiterator |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 894 | check(iter(set()), size('P3n')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 895 | # slice |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 896 | check(slice(0), size('3P')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 897 | # super |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 898 | check(super(int), size('3P')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 899 | # tuple |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 900 | check((), vsize('')) |
| 901 | check((1,2,3), vsize('') + 3*self.P) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 902 | # type |
Martin v. Loewis | 4f2f3b6 | 2012-04-24 19:13:57 +0200 | [diff] [blame] | 903 | # static type: PyTypeObject |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 904 | s = vsize('P2n15Pl4Pn9Pn11PIP') |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 905 | check(int, s) |
Martin v. Loewis | 4f2f3b6 | 2012-04-24 19:13:57 +0200 | [diff] [blame] | 906 | # (PyTypeObject + PyNumberMethods + PyMappingMethods + |
| 907 | # PySequenceMethods + PyBufferProcs + 4P) |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 908 | s = vsize('P2n15Pl4Pn9Pn11PIP') + struct.calcsize('34P 3P 10P 2P 4P') |
Martin v. Loewis | 4f2f3b6 | 2012-04-24 19:13:57 +0200 | [diff] [blame] | 909 | # Separate block for PyDictKeysObject with 4 entries |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 910 | s += struct.calcsize("2nPn") + 4*struct.calcsize("n2P") |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 911 | # class |
| 912 | class newstyleclass(object): pass |
| 913 | check(newstyleclass, s) |
Martin v. Loewis | 4f2f3b6 | 2012-04-24 19:13:57 +0200 | [diff] [blame] | 914 | # dict with shared keys |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 915 | check(newstyleclass().__dict__, size('n2P' + '2nPn')) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 916 | # unicode |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 917 | # each tuple contains a string and its expected character size |
| 918 | # don't put any static strings here, as they may contain |
| 919 | # wchar_t or UTF-8 representations |
| 920 | samples = ['1'*100, '\xff'*50, |
| 921 | '\u0100'*40, '\uffff'*100, |
| 922 | '\U00010000'*30, '\U0010ffff'*100] |
Antoine Pitrou | 80bc00f | 2013-05-25 23:47:29 +0200 | [diff] [blame] | 923 | asciifields = "nnbP" |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 924 | compactfields = asciifields + "nPn" |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 925 | unicodefields = compactfields + "P" |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 926 | for s in samples: |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 927 | maxchar = ord(max(s)) |
| 928 | if maxchar < 128: |
| 929 | L = size(asciifields) + len(s) + 1 |
| 930 | elif maxchar < 256: |
| 931 | L = size(compactfields) + len(s) + 1 |
| 932 | elif maxchar < 65536: |
| 933 | L = size(compactfields) + 2*(len(s) + 1) |
| 934 | else: |
| 935 | L = size(compactfields) + 4*(len(s) + 1) |
| 936 | check(s, L) |
| 937 | # verify that the UTF-8 size is accounted for |
| 938 | s = chr(0x4000) # 4 bytes canonical representation |
| 939 | check(s, size(compactfields) + 4) |
Martin v. Löwis | f02aa65 | 2011-09-28 09:22:13 +0200 | [diff] [blame] | 940 | # compile() will trigger the generation of the UTF-8 |
| 941 | # representation as a side effect |
| 942 | compile(s, "<stdin>", "eval") |
| 943 | check(s, size(compactfields) + 4 + 4) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 944 | # TODO: add check that forces the presence of wchar_t representation |
| 945 | # TODO: add check that forces layout of unicodefields |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 946 | # weakref |
| 947 | import weakref |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 948 | check(weakref.ref(int), size('2Pn2P')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 949 | # weakproxy |
| 950 | # XXX |
| 951 | # weakcallableproxy |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 952 | check(weakref.proxy(int), size('2Pn2P')) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 953 | |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 954 | def test_pythontypes(self): |
| 955 | # check all types defined in Python/ |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 956 | size = test.support.calcobjsize |
| 957 | vsize = test.support.calcvobjsize |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 958 | check = self.check_sizeof |
| 959 | # _ast.AST |
| 960 | import _ast |
Martin v. Löwis | 2b16844 | 2012-07-29 16:38:45 +0200 | [diff] [blame] | 961 | check(_ast.AST(), size('P')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 962 | try: |
| 963 | raise TypeError |
| 964 | except TypeError: |
| 965 | tb = sys.exc_info()[2] |
| 966 | # traceback |
Benjamin Peterson | b29614e | 2012-10-09 11:16:03 -0400 | [diff] [blame] | 967 | if tb is not None: |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 968 | check(tb, size('2P2i')) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 969 | # symtable entry |
| 970 | # XXX |
| 971 | # sys.flags |
Martin v. Löwis | 33f7997 | 2012-07-29 16:33:05 +0200 | [diff] [blame] | 972 | check(sys.flags, vsize('') + self.P * len(sys.flags)) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 973 | |
| 974 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 975 | def test_main(): |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 976 | test.support.run_unittest(SysModuleTest, SizeofTest) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 977 | |
| 978 | if __name__ == "__main__": |
| 979 | test_main() |