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