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