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