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 |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 4 | |
| 5 | class SysModuleTest(unittest.TestCase): |
| 6 | |
Christian Heimes | ef181a7 | 2007-11-07 16:14:12 +0000 | [diff] [blame] | 7 | def setUp(self): |
| 8 | self.orig_stdout = sys.stdout |
| 9 | self.orig_stderr = sys.stderr |
| 10 | self.orig_displayhook = sys.displayhook |
| 11 | |
| 12 | def tearDown(self): |
| 13 | sys.stdout = self.orig_stdout |
| 14 | sys.stderr = self.orig_stderr |
| 15 | sys.displayhook = self.orig_displayhook |
| 16 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 17 | def test_original_displayhook(self): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 18 | import builtins |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 19 | out = io.StringIO() |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 20 | sys.stdout = out |
| 21 | |
| 22 | dh = sys.__displayhook__ |
| 23 | |
| 24 | self.assertRaises(TypeError, dh) |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 25 | if hasattr(builtins, "_"): |
| 26 | del builtins._ |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 27 | |
| 28 | dh(None) |
| 29 | self.assertEqual(out.getvalue(), "") |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 30 | self.assert_(not hasattr(builtins, "_")) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 31 | dh(42) |
| 32 | self.assertEqual(out.getvalue(), "42\n") |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 33 | self.assertEqual(builtins._, 42) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 34 | |
| 35 | del sys.stdout |
| 36 | self.assertRaises(RuntimeError, dh, 42) |
| 37 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 38 | def test_lost_displayhook(self): |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 39 | del sys.displayhook |
| 40 | code = compile("42", "<string>", "single") |
| 41 | self.assertRaises(RuntimeError, eval, code) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 42 | |
| 43 | def test_custom_displayhook(self): |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 44 | def baddisplayhook(obj): |
| 45 | raise ValueError |
| 46 | sys.displayhook = baddisplayhook |
| 47 | code = compile("42", "<string>", "single") |
| 48 | self.assertRaises(ValueError, eval, code) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 49 | |
| 50 | def test_original_excepthook(self): |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 51 | err = io.StringIO() |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 52 | sys.stderr = err |
| 53 | |
| 54 | eh = sys.__excepthook__ |
| 55 | |
| 56 | self.assertRaises(TypeError, eh) |
| 57 | try: |
| 58 | raise ValueError(42) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 59 | except ValueError as exc: |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 60 | eh(*sys.exc_info()) |
| 61 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 62 | self.assert_(err.getvalue().endswith("ValueError: 42\n")) |
| 63 | |
Walter Dörwald | e7028ac | 2003-02-03 23:05:27 +0000 | [diff] [blame] | 64 | # FIXME: testing the code for a lost or replaced excepthook in |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 65 | # Python/pythonrun.c::PyErr_PrintEx() is tricky. |
| 66 | |
| 67 | def test_exit(self): |
| 68 | self.assertRaises(TypeError, sys.exit, 42, 42) |
| 69 | |
| 70 | # call without argument |
| 71 | try: |
| 72 | sys.exit(0) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 73 | except SystemExit as exc: |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 74 | self.assertEquals(exc.code, 0) |
| 75 | except: |
| 76 | self.fail("wrong exception") |
| 77 | else: |
| 78 | self.fail("no exception") |
| 79 | |
| 80 | # call with tuple argument with one entry |
| 81 | # entry will be unpacked |
| 82 | try: |
| 83 | sys.exit(42) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 84 | except SystemExit as exc: |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 85 | self.assertEquals(exc.code, 42) |
| 86 | except: |
| 87 | self.fail("wrong exception") |
| 88 | else: |
| 89 | self.fail("no exception") |
| 90 | |
| 91 | # call with integer argument |
| 92 | try: |
| 93 | sys.exit((42,)) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 94 | except SystemExit as exc: |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 95 | self.assertEquals(exc.code, 42) |
| 96 | except: |
| 97 | self.fail("wrong exception") |
| 98 | else: |
| 99 | self.fail("no exception") |
| 100 | |
| 101 | # call with string argument |
| 102 | try: |
| 103 | sys.exit("exit") |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 104 | except SystemExit as exc: |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 105 | self.assertEquals(exc.code, "exit") |
| 106 | except: |
| 107 | self.fail("wrong exception") |
| 108 | else: |
| 109 | self.fail("no exception") |
| 110 | |
| 111 | # call with tuple argument with two entries |
| 112 | try: |
| 113 | sys.exit((17, 23)) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 114 | except SystemExit as exc: |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 115 | self.assertEquals(exc.code, (17, 23)) |
| 116 | except: |
| 117 | self.fail("wrong exception") |
| 118 | else: |
| 119 | self.fail("no exception") |
| 120 | |
Michael W. Hudson | f058858 | 2005-02-15 15:26:11 +0000 | [diff] [blame] | 121 | # test that the exit machinery handles SystemExits properly |
| 122 | import subprocess |
Michael W. Hudson | f058858 | 2005-02-15 15:26:11 +0000 | [diff] [blame] | 123 | rc = subprocess.call([sys.executable, "-c", |
| 124 | "raise SystemExit(47)"]) |
| 125 | self.assertEqual(rc, 47) |
Tim Peters | f0db38d | 2005-02-15 21:50:12 +0000 | [diff] [blame] | 126 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 127 | def test_getdefaultencoding(self): |
Walter Dörwald | 4aeaa96 | 2007-05-22 16:13:46 +0000 | [diff] [blame] | 128 | self.assertRaises(TypeError, sys.getdefaultencoding, 42) |
| 129 | # can't check more than the type, as the user might have changed it |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 130 | self.assert_(isinstance(sys.getdefaultencoding(), str)) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 131 | |
| 132 | # testing sys.settrace() is done in test_trace.py |
| 133 | # testing sys.setprofile() is done in test_profile.py |
| 134 | |
| 135 | def test_setcheckinterval(self): |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 136 | self.assertRaises(TypeError, sys.setcheckinterval) |
Tim Peters | e5e065b | 2003-07-06 18:36:54 +0000 | [diff] [blame] | 137 | orig = sys.getcheckinterval() |
| 138 | for n in 0, 100, 120, orig: # orig last to restore starting state |
| 139 | sys.setcheckinterval(n) |
| 140 | self.assertEquals(sys.getcheckinterval(), n) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 141 | |
| 142 | def test_recursionlimit(self): |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 143 | self.assertRaises(TypeError, sys.getrecursionlimit, 42) |
| 144 | oldlimit = sys.getrecursionlimit() |
| 145 | self.assertRaises(TypeError, sys.setrecursionlimit) |
| 146 | self.assertRaises(ValueError, sys.setrecursionlimit, -42) |
| 147 | sys.setrecursionlimit(10000) |
| 148 | self.assertEqual(sys.getrecursionlimit(), 10000) |
| 149 | sys.setrecursionlimit(oldlimit) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 150 | |
| 151 | def test_getwindowsversion(self): |
| 152 | if hasattr(sys, "getwindowsversion"): |
| 153 | v = sys.getwindowsversion() |
| 154 | self.assert_(isinstance(v, tuple)) |
| 155 | self.assertEqual(len(v), 5) |
| 156 | self.assert_(isinstance(v[0], int)) |
| 157 | self.assert_(isinstance(v[1], int)) |
| 158 | self.assert_(isinstance(v[2], int)) |
| 159 | self.assert_(isinstance(v[3], int)) |
| 160 | self.assert_(isinstance(v[4], str)) |
| 161 | |
| 162 | def test_dlopenflags(self): |
| 163 | if hasattr(sys, "setdlopenflags"): |
| 164 | self.assert_(hasattr(sys, "getdlopenflags")) |
| 165 | self.assertRaises(TypeError, sys.getdlopenflags, 42) |
| 166 | oldflags = sys.getdlopenflags() |
| 167 | self.assertRaises(TypeError, sys.setdlopenflags) |
| 168 | sys.setdlopenflags(oldflags+1) |
| 169 | self.assertEqual(sys.getdlopenflags(), oldflags+1) |
| 170 | sys.setdlopenflags(oldflags) |
| 171 | |
| 172 | def test_refcount(self): |
| 173 | self.assertRaises(TypeError, sys.getrefcount) |
| 174 | c = sys.getrefcount(None) |
| 175 | n = None |
| 176 | self.assertEqual(sys.getrefcount(None), c+1) |
| 177 | del n |
| 178 | self.assertEqual(sys.getrefcount(None), c) |
| 179 | if hasattr(sys, "gettotalrefcount"): |
| 180 | self.assert_(isinstance(sys.gettotalrefcount(), int)) |
| 181 | |
| 182 | def test_getframe(self): |
| 183 | self.assertRaises(TypeError, sys._getframe, 42, 42) |
Neal Norwitz | eb2a5ef | 2003-02-18 15:22:10 +0000 | [diff] [blame] | 184 | self.assertRaises(ValueError, sys._getframe, 2000000000) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 185 | self.assert_( |
Christian Heimes | 4a22b5d | 2007-11-25 09:39:14 +0000 | [diff] [blame] | 186 | SysModuleTest.test_getframe.__code__ \ |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 187 | is sys._getframe().f_code |
| 188 | ) |
| 189 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 190 | # sys._current_frames() is a CPython-only gimmick. |
| 191 | def test_current_frames(self): |
| 192 | have_threads = True |
| 193 | try: |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 194 | import _thread |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 195 | except ImportError: |
| 196 | have_threads = False |
| 197 | |
| 198 | if have_threads: |
| 199 | self.current_frames_with_threads() |
| 200 | else: |
| 201 | self.current_frames_without_threads() |
| 202 | |
| 203 | # Test sys._current_frames() in a WITH_THREADS build. |
| 204 | def current_frames_with_threads(self): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 205 | import threading, _thread |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 206 | import traceback |
| 207 | |
| 208 | # Spawn a thread that blocks at a known place. Then the main |
| 209 | # thread does sys._current_frames(), and verifies that the frames |
| 210 | # returned make sense. |
| 211 | entered_g = threading.Event() |
| 212 | leave_g = threading.Event() |
| 213 | thread_info = [] # the thread's id |
| 214 | |
| 215 | def f123(): |
| 216 | g456() |
| 217 | |
| 218 | def g456(): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 219 | thread_info.append(_thread.get_ident()) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 220 | entered_g.set() |
| 221 | leave_g.wait() |
| 222 | |
| 223 | t = threading.Thread(target=f123) |
| 224 | t.start() |
| 225 | entered_g.wait() |
| 226 | |
| 227 | # At this point, t has finished its entered_g.set(), although it's |
| 228 | # impossible to guess whether it's still on that line or has moved on |
| 229 | # to its leave_g.wait(). |
| 230 | self.assertEqual(len(thread_info), 1) |
| 231 | thread_id = thread_info[0] |
| 232 | |
| 233 | d = sys._current_frames() |
| 234 | |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 235 | main_id = _thread.get_ident() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 236 | self.assert_(main_id in d) |
| 237 | self.assert_(thread_id in d) |
| 238 | |
| 239 | # Verify that the captured main-thread frame is _this_ frame. |
| 240 | frame = d.pop(main_id) |
| 241 | self.assert_(frame is sys._getframe()) |
| 242 | |
| 243 | # Verify that the captured thread frame is blocked in g456, called |
| 244 | # from f123. This is a litte tricky, since various bits of |
| 245 | # threading.py are also in the thread's call stack. |
| 246 | frame = d.pop(thread_id) |
| 247 | stack = traceback.extract_stack(frame) |
| 248 | for i, (filename, lineno, funcname, sourceline) in enumerate(stack): |
| 249 | if funcname == "f123": |
| 250 | break |
| 251 | else: |
| 252 | self.fail("didn't find f123() on thread's call stack") |
| 253 | |
| 254 | self.assertEqual(sourceline, "g456()") |
| 255 | |
| 256 | # And the next record must be for g456(). |
| 257 | filename, lineno, funcname, sourceline = stack[i+1] |
| 258 | self.assertEqual(funcname, "g456") |
| 259 | self.assert_(sourceline in ["leave_g.wait()", "entered_g.set()"]) |
| 260 | |
| 261 | # Reap the spawned thread. |
| 262 | leave_g.set() |
| 263 | t.join() |
| 264 | |
| 265 | # Test sys._current_frames() when thread support doesn't exist. |
| 266 | def current_frames_without_threads(self): |
| 267 | # Not much happens here: there is only one thread, with artificial |
| 268 | # "thread id" 0. |
| 269 | d = sys._current_frames() |
| 270 | self.assertEqual(len(d), 1) |
| 271 | self.assert_(0 in d) |
| 272 | self.assert_(d[0] is sys._getframe()) |
| 273 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 274 | def test_attributes(self): |
| 275 | self.assert_(isinstance(sys.api_version, int)) |
| 276 | self.assert_(isinstance(sys.argv, list)) |
| 277 | self.assert_(sys.byteorder in ("little", "big")) |
| 278 | self.assert_(isinstance(sys.builtin_module_names, tuple)) |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 279 | self.assert_(isinstance(sys.copyright, str)) |
| 280 | self.assert_(isinstance(sys.exec_prefix, str)) |
| 281 | self.assert_(isinstance(sys.executable, str)) |
Christian Heimes | 9385266 | 2007-12-01 12:22:32 +0000 | [diff] [blame] | 282 | self.assertEqual(len(sys.float_info), 11) |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 283 | self.assertEqual(sys.float_info.radix, 2) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 284 | self.assert_(isinstance(sys.hexversion, int)) |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 285 | self.assert_(isinstance(sys.maxsize, int)) |
Walter Dörwald | 4aeaa96 | 2007-05-22 16:13:46 +0000 | [diff] [blame] | 286 | self.assert_(isinstance(sys.maxunicode, int)) |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 287 | self.assert_(isinstance(sys.platform, str)) |
| 288 | self.assert_(isinstance(sys.prefix, str)) |
| 289 | self.assert_(isinstance(sys.version, str)) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 290 | vi = sys.version_info |
| 291 | self.assert_(isinstance(vi, tuple)) |
| 292 | self.assertEqual(len(vi), 5) |
| 293 | self.assert_(isinstance(vi[0], int)) |
| 294 | self.assert_(isinstance(vi[1], int)) |
| 295 | self.assert_(isinstance(vi[2], int)) |
| 296 | self.assert_(vi[3] in ("alpha", "beta", "candidate", "final")) |
| 297 | self.assert_(isinstance(vi[4], int)) |
| 298 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 299 | def test_43581(self): |
| 300 | # Can't use sys.stdout, as this is a cStringIO object when |
| 301 | # the test runs under regrtest. |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 302 | self.assertEqual(sys.__stdout__.encoding, sys.__stderr__.encoding) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 303 | |
Georg Brandl | 66a796e | 2006-12-19 20:50:34 +0000 | [diff] [blame] | 304 | def test_intern(self): |
| 305 | self.assertRaises(TypeError, sys.intern) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 306 | s = "never interned before" |
Georg Brandl | 66a796e | 2006-12-19 20:50:34 +0000 | [diff] [blame] | 307 | self.assert_(sys.intern(s) is s) |
| 308 | s2 = s.swapcase().swapcase() |
| 309 | self.assert_(sys.intern(s2) is s) |
| 310 | |
| 311 | # Subclasses of string can't be interned, because they |
| 312 | # provide too much opportunity for insane things to happen. |
| 313 | # We don't want them in the interned dict and if they aren't |
| 314 | # actually interned, we don't want to create the appearance |
| 315 | # that they are by allowing intern() to succeeed. |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 316 | class S(str): |
Georg Brandl | 66a796e | 2006-12-19 20:50:34 +0000 | [diff] [blame] | 317 | def __hash__(self): |
| 318 | return 123 |
| 319 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 320 | self.assertRaises(TypeError, sys.intern, S("abc")) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 321 | |
Georg Brandl | 66a796e | 2006-12-19 20:50:34 +0000 | [diff] [blame] | 322 | |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 323 | def test_sys_flags(self): |
| 324 | self.failUnless(sys.flags) |
| 325 | attrs = ("debug", "division_warning", |
| 326 | "inspect", "interactive", "optimize", "dont_write_bytecode", |
Georg Brandl | 01a7d82 | 2008-06-04 14:03:46 +0000 | [diff] [blame] | 327 | "no_site", "ignore_environment", "verbose", |
Benjamin Peterson | 699adb9 | 2008-05-08 22:27:58 +0000 | [diff] [blame] | 328 | "bytes_warning") |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 329 | for attr in attrs: |
| 330 | self.assert_(hasattr(sys.flags, attr), attr) |
| 331 | self.assertEqual(type(getattr(sys.flags, attr)), int, attr) |
| 332 | self.assert_(repr(sys.flags)) |
| 333 | |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 334 | def test_clear_type_cache(self): |
| 335 | sys._clear_type_cache() |
| 336 | |
| 337 | def test_compact_freelists(self): |
| 338 | sys._compact_freelists() |
| 339 | r = sys._compact_freelists() |
Christian Heimes | 3ecfea71 | 2008-02-09 20:51:34 +0000 | [diff] [blame] | 340 | ## freed blocks shouldn't change |
| 341 | #self.assertEqual(r[0][2], 0) |
| 342 | ## fill freelists |
| 343 | #ints = list(range(10000)) |
| 344 | #floats = [float(i) for i in ints] |
| 345 | #del ints |
| 346 | #del floats |
| 347 | ## should free more than 100 blocks |
| 348 | #r = sys._compact_freelists() |
| 349 | #self.assert_(r[0][1] > 100, r[0][1]) |
| 350 | #self.assert_(r[0][2] > 100, r[0][2]) |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 351 | |
Martin v. Löwis | 0f59989 | 2008-06-02 11:13:03 +0000 | [diff] [blame] | 352 | def test_ioencoding(self): |
| 353 | import subprocess,os |
| 354 | env = dict(os.environ) |
| 355 | |
| 356 | # Test character: cent sign, encoded as 0x4A (ASCII J) in CP424, |
| 357 | # not representable in ASCII. |
| 358 | |
| 359 | env["PYTHONIOENCODING"] = "cp424" |
| 360 | p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], |
| 361 | stdout = subprocess.PIPE, env=env) |
| 362 | out = p.stdout.read() |
| 363 | self.assertEqual(out, "\xa2\n".encode("cp424")) |
| 364 | |
| 365 | env["PYTHONIOENCODING"] = "ascii:replace" |
| 366 | p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], |
| 367 | stdout = subprocess.PIPE, env=env) |
| 368 | out = p.stdout.read().strip() |
| 369 | self.assertEqual(out, b'?') |
| 370 | |
| 371 | |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame^] | 372 | class SizeofTest(unittest.TestCase): |
| 373 | |
| 374 | def setUp(self): |
| 375 | import struct |
| 376 | self.i = len(struct.pack('i', 0)) |
| 377 | self.l = len(struct.pack('l', 0)) |
| 378 | self.p = len(struct.pack('P', 0)) |
| 379 | self.headersize = self.l + self.p |
| 380 | if hasattr(sys, "gettotalrefcount"): |
| 381 | self.headersize += 2 * self.p |
| 382 | self.file = open(test.support.TESTFN, 'wb') |
| 383 | |
| 384 | def tearDown(self): |
| 385 | self.file.close() |
| 386 | test.support.unlink(test.support.TESTFN) |
| 387 | |
| 388 | def check_sizeof(self, o, size): |
| 389 | result = sys.getsizeof(o) |
| 390 | msg = 'wrong size for %s: got %d, expected %d' \ |
| 391 | % (type(o), result, size) |
| 392 | self.assertEqual(result, size, msg) |
| 393 | |
| 394 | def align(self, value): |
| 395 | mod = value % self.p |
| 396 | if mod != 0: |
| 397 | return value - mod + self.p |
| 398 | else: |
| 399 | return value |
| 400 | |
| 401 | def test_align(self): |
| 402 | self.assertEqual(self.align(0) % self.p, 0) |
| 403 | self.assertEqual(self.align(1) % self.p, 0) |
| 404 | self.assertEqual(self.align(3) % self.p, 0) |
| 405 | self.assertEqual(self.align(4) % self.p, 0) |
| 406 | self.assertEqual(self.align(7) % self.p, 0) |
| 407 | self.assertEqual(self.align(8) % self.p, 0) |
| 408 | self.assertEqual(self.align(9) % self.p, 0) |
| 409 | |
| 410 | def test_standardtypes(self): |
| 411 | i = self.i |
| 412 | l = self.l |
| 413 | p = self.p |
| 414 | h = self.headersize |
| 415 | # bool |
| 416 | self.check_sizeof(True, h + 2*l) |
| 417 | # bytearray |
| 418 | self.check_sizeof(bytes(), h + self.align(i) + l + p) |
| 419 | # cell |
| 420 | def get_cell(): |
| 421 | x = 42 |
| 422 | def inner(): |
| 423 | return x |
| 424 | return inner |
| 425 | self.check_sizeof(get_cell().__closure__[0], h + p) |
| 426 | # code XXX wrong size |
| 427 | # self.check_sizeof(get_cell().__code__, h + self.align(4*i) + 8*p +\ |
| 428 | # self.align(i) + 2*p) |
| 429 | # complex |
| 430 | self.check_sizeof(complex(0,1), h + 2*8) |
| 431 | # enumerate |
| 432 | self.check_sizeof(enumerate([]), h + l + 3*p) |
| 433 | # reverse |
| 434 | self.check_sizeof(reversed(''), h + l + p ) |
| 435 | # file XXX wrong size |
| 436 | #self.check_sizeof(self.file, h + 4*p + self.align(2*i) + 4*p +\ |
| 437 | # self.align(3*i) + 3*p + self.align(i)) |
| 438 | # float |
| 439 | self.check_sizeof(float(0), h + 8) |
| 440 | # function |
| 441 | def func(): pass |
| 442 | self.check_sizeof(func, h + 11 * p) |
| 443 | class c(): |
| 444 | @staticmethod |
| 445 | def foo(): |
| 446 | pass |
| 447 | @classmethod |
| 448 | def bar(cls): |
| 449 | pass |
| 450 | # staticmethod |
| 451 | self.check_sizeof(foo, h + l) |
| 452 | # classmethod |
| 453 | self.check_sizeof(bar, h + l) |
| 454 | # generator |
| 455 | def get_gen(): yield 1 |
| 456 | self.check_sizeof(get_gen(), h + p + self.align(i) + 2*p) |
| 457 | # builtin_function_or_method |
| 458 | self.check_sizeof(abs, h + 3*p) |
| 459 | # module |
| 460 | self.check_sizeof(unittest, h + p) |
| 461 | # range |
| 462 | self.check_sizeof(range(1), h + 3*p) |
| 463 | # slice |
| 464 | self.check_sizeof(slice(0), h + 3*p) |
| 465 | |
| 466 | h += l |
| 467 | # new-style class |
| 468 | class class_newstyle(object): |
| 469 | def method(): |
| 470 | pass |
| 471 | # type (PyTypeObject + PyNumberMethods + PyMappingMethods + |
| 472 | # PySequenceMethods + PyBufferProcs) |
| 473 | # XXX wrong size |
| 474 | # len_typeobject = p + 2*l + 15*p + l + 4*p + l + 9*p + l + 11*p |
| 475 | # self.check_sizeof(class_newstyle, |
| 476 | # h + len_typeobject + 42*p + 10*p + 3*p + 6*p) |
| 477 | |
| 478 | |
| 479 | def test_specialtypes(self): |
| 480 | i = self.i |
| 481 | l = self.l |
| 482 | p = self.p |
| 483 | h = self.headersize |
| 484 | # dict |
| 485 | self.check_sizeof({}, h + 3*l + 3*p + 8*(l + 2*p)) |
| 486 | longdict = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8} |
| 487 | self.check_sizeof(longdict, h + 3*l + 3*p + 8*(l + 2*p) + 16*(l + 2*p)) |
| 488 | # list |
| 489 | self.check_sizeof([], h + l + p + l) |
| 490 | self.check_sizeof([1, 2, 3], h + l + p + l + 3*l) |
| 491 | |
| 492 | h += l |
| 493 | # long |
| 494 | self.check_sizeof(0, h + self.align(2)) |
| 495 | self.check_sizeof(1, h + self.align(2)) |
| 496 | self.check_sizeof(-1, h + self.align(2)) |
| 497 | self.check_sizeof(32768, h + self.align(2) + 2) |
| 498 | self.check_sizeof(32768*32768-1, h + self.align(2) + 2) |
| 499 | self.check_sizeof(32768*32768, h + self.align(2) + 4) |
| 500 | # XXX add Unicode support |
| 501 | # self.check_sizeof('', h + l + self.align(i + 1)) |
| 502 | # self.check_sizeof('abc', h + l + self.align(i + 1) + 3) |
| 503 | |
| 504 | |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 505 | def test_main(): |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame^] | 506 | test.support.run_unittest(SysModuleTest, SizeofTest) |
Walter Dörwald | c350246 | 2003-02-03 23:03:49 +0000 | [diff] [blame] | 507 | |
| 508 | if __name__ == "__main__": |
| 509 | test_main() |