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