Changeset 391 for python/trunk/Lib/test/test_sys.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/test/test_sys.py
r2 r391 1 1 # -*- coding: iso-8859-1 -*- 2 2 import unittest, test.test_support 3 import sys, cStringIO, os3 import sys, os, cStringIO 4 4 import struct 5 import operator 5 6 6 7 class SysModuleTest(unittest.TestCase): 8 9 def tearDown(self): 10 test.test_support.reap_children() 7 11 8 12 def test_original_displayhook(self): … … 20 24 dh(None) 21 25 self.assertEqual(out.getvalue(), "") 22 self.assert _(not hasattr(__builtin__, "_"))26 self.assertTrue(not hasattr(__builtin__, "_")) 23 27 dh(42) 24 28 self.assertEqual(out.getvalue(), "42\n") … … 60 64 61 65 sys.stderr = savestderr 62 self.assert _(err.getvalue().endswith("ValueError: 42\n"))66 self.assertTrue(err.getvalue().endswith("ValueError: 42\n")) 63 67 64 68 # FIXME: testing the code for a lost or replaced excepthook in … … 72 76 def clear_check(exc): 73 77 typ, value, traceback = sys.exc_info() 74 self.assert_(typ is not None) 75 self.assert_(value is exc) 76 self.assert_(traceback is not None) 77 78 sys.exc_clear() 78 self.assertTrue(typ is not None) 79 self.assertTrue(value is exc) 80 self.assertTrue(traceback is not None) 81 82 with test.test_support.check_py3k_warnings(): 83 sys.exc_clear() 79 84 80 85 typ, value, traceback = sys.exc_info() 81 self.assert _(typ is None)82 self.assert _(value is None)83 self.assert _(traceback is None)86 self.assertTrue(typ is None) 87 self.assertTrue(value is None) 88 self.assertTrue(traceback is None) 84 89 85 90 def clear(): … … 101 106 typ2, value2, traceback2 = sys.exc_info() 102 107 103 self.assert _(typ1 is typ2)104 self.assert _(value1 is exc)105 self.assert _(value1 is value2)106 self.assert _(traceback1 is traceback2)108 self.assertTrue(typ1 is typ2) 109 self.assertTrue(value1 is exc) 110 self.assertTrue(value1 is value2) 111 self.assertTrue(traceback1 is traceback2) 107 112 108 113 # Check that an exception can be cleared outside of an except block … … 116 121 sys.exit(0) 117 122 except SystemExit, exc: 118 self.assertEqual s(exc.code, 0)123 self.assertEqual(exc.code, 0) 119 124 except: 120 125 self.fail("wrong exception") … … 127 132 sys.exit(42) 128 133 except SystemExit, exc: 129 self.assertEqual s(exc.code, 42)134 self.assertEqual(exc.code, 42) 130 135 except: 131 136 self.fail("wrong exception") … … 137 142 sys.exit((42,)) 138 143 except SystemExit, exc: 139 self.assertEqual s(exc.code, 42)144 self.assertEqual(exc.code, 42) 140 145 except: 141 146 self.fail("wrong exception") … … 147 152 sys.exit("exit") 148 153 except SystemExit, exc: 149 self.assertEqual s(exc.code, "exit")154 self.assertEqual(exc.code, "exit") 150 155 except: 151 156 self.fail("wrong exception") … … 157 162 sys.exit((17, 23)) 158 163 except SystemExit, exc: 159 self.assertEqual s(exc.code, (17, 23))164 self.assertEqual(exc.code, (17, 23)) 160 165 except: 161 166 self.fail("wrong exception") … … 174 179 self.assertEqual(rc, 47) 175 180 181 def check_exit_message(code, expected, env=None): 182 process = subprocess.Popen([sys.executable, "-c", code], 183 stderr=subprocess.PIPE, env=env) 184 stdout, stderr = process.communicate() 185 self.assertEqual(process.returncode, 1) 186 self.assertTrue(stderr.startswith(expected), 187 "%s doesn't start with %s" % (repr(stderr), repr(expected))) 188 189 # test that stderr buffer if flushed before the exit message is written 190 # into stderr 191 check_exit_message( 192 r'import sys; sys.stderr.write("unflushed,"); sys.exit("message")', 193 b"unflushed,message") 194 195 # test that the unicode message is encoded to the stderr encoding 196 env = os.environ.copy() 197 env['PYTHONIOENCODING'] = 'latin-1' 198 check_exit_message( 199 r'import sys; sys.exit(u"h\xe9")', 200 b"h\xe9", env=env) 176 201 177 202 def test_getdefaultencoding(self): … … 179 204 self.assertRaises(TypeError, sys.getdefaultencoding, 42) 180 205 # can't check more than the type, as the user might have changed it 181 self.assert _(isinstance(sys.getdefaultencoding(), str))182 183 # testing sys.settrace() is done in test_ trace.py184 # testing sys.setprofile() is done in test_ profile.py206 self.assertIsInstance(sys.getdefaultencoding(), str) 207 208 # testing sys.settrace() is done in test_sys_settrace.py 209 # testing sys.setprofile() is done in test_sys_setprofile.py 185 210 186 211 def test_setcheckinterval(self): … … 189 214 for n in 0, 100, 120, orig: # orig last to restore starting state 190 215 sys.setcheckinterval(n) 191 self.assertEqual s(sys.getcheckinterval(), n)216 self.assertEqual(sys.getcheckinterval(), n) 192 217 193 218 def test_recursionlimit(self): … … 200 225 sys.setrecursionlimit(oldlimit) 201 226 227 self.assertRaises(OverflowError, sys.setrecursionlimit, 1 << 31) 228 try: 229 sys.setrecursionlimit((1 << 31) - 5) 230 try: 231 # issue13546: isinstance(e, ValueError) used to fail 232 # when the recursion limit is close to 1<<31 233 raise ValueError() 234 except ValueError, e: 235 pass 236 finally: 237 sys.setrecursionlimit(oldlimit) 238 202 239 def test_getwindowsversion(self): 203 if hasattr(sys, "getwindowsversion"): 204 v = sys.getwindowsversion() 205 self.assert_(isinstance(v, tuple)) 206 self.assertEqual(len(v), 5) 207 self.assert_(isinstance(v[0], int)) 208 self.assert_(isinstance(v[1], int)) 209 self.assert_(isinstance(v[2], int)) 210 self.assert_(isinstance(v[3], int)) 211 self.assert_(isinstance(v[4], str)) 240 # Raise SkipTest if sys doesn't have getwindowsversion attribute 241 test.test_support.get_attribute(sys, "getwindowsversion") 242 v = sys.getwindowsversion() 243 self.assertEqual(len(v), 5) 244 self.assertIsInstance(v[0], int) 245 self.assertIsInstance(v[1], int) 246 self.assertIsInstance(v[2], int) 247 self.assertIsInstance(v[3], int) 248 self.assertIsInstance(v[4], str) 249 self.assertRaises(IndexError, operator.getitem, v, 5) 250 self.assertIsInstance(v.major, int) 251 self.assertIsInstance(v.minor, int) 252 self.assertIsInstance(v.build, int) 253 self.assertIsInstance(v.platform, int) 254 self.assertIsInstance(v.service_pack, str) 255 self.assertIsInstance(v.service_pack_minor, int) 256 self.assertIsInstance(v.service_pack_major, int) 257 self.assertIsInstance(v.suite_mask, int) 258 self.assertIsInstance(v.product_type, int) 259 self.assertEqual(v[0], v.major) 260 self.assertEqual(v[1], v.minor) 261 self.assertEqual(v[2], v.build) 262 self.assertEqual(v[3], v.platform) 263 self.assertEqual(v[4], v.service_pack) 264 265 # This is how platform.py calls it. Make sure tuple 266 # still has 5 elements 267 maj, min, buildno, plat, csd = sys.getwindowsversion() 212 268 213 269 def test_dlopenflags(self): 214 270 if hasattr(sys, "setdlopenflags"): 215 self.assert _(hasattr(sys, "getdlopenflags"))271 self.assertTrue(hasattr(sys, "getdlopenflags")) 216 272 self.assertRaises(TypeError, sys.getdlopenflags, 42) 217 273 oldflags = sys.getdlopenflags() … … 234 290 self.assertEqual(sys.getrefcount(None), c) 235 291 if hasattr(sys, "gettotalrefcount"): 236 self.assert _(isinstance(sys.gettotalrefcount(), int))292 self.assertIsInstance(sys.gettotalrefcount(), int) 237 293 238 294 def test_getframe(self): 239 295 self.assertRaises(TypeError, sys._getframe, 42, 42) 240 296 self.assertRaises(ValueError, sys._getframe, 2000000000) 241 self.assert _(297 self.assertTrue( 242 298 SysModuleTest.test_getframe.im_func.func_code \ 243 299 is sys._getframe().f_code … … 258 314 259 315 # Test sys._current_frames() in a WITH_THREADS build. 316 @test.test_support.reap_threads 260 317 def current_frames_with_threads(self): 261 318 import threading, thread … … 290 347 291 348 main_id = thread.get_ident() 292 self.assert _(main_id ind)293 self.assert _(thread_id ind)349 self.assertIn(main_id, d) 350 self.assertIn(thread_id, d) 294 351 295 352 # Verify that the captured main-thread frame is _this_ frame. 296 353 frame = d.pop(main_id) 297 self.assert _(frame is sys._getframe())354 self.assertTrue(frame is sys._getframe()) 298 355 299 356 # Verify that the captured thread frame is blocked in g456, called … … 313 370 filename, lineno, funcname, sourceline = stack[i+1] 314 371 self.assertEqual(funcname, "g456") 315 self.assert _(sourceline in["leave_g.wait()", "entered_g.set()"])372 self.assertIn(sourceline, ["leave_g.wait()", "entered_g.set()"]) 316 373 317 374 # Reap the spawned thread. … … 325 382 d = sys._current_frames() 326 383 self.assertEqual(len(d), 1) 327 self.assert _(0 ind)328 self.assert _(d[0] is sys._getframe())384 self.assertIn(0, d) 385 self.assertTrue(d[0] is sys._getframe()) 329 386 330 387 def test_attributes(self): 331 self.assert _(isinstance(sys.api_version, int))332 self.assert _(isinstance(sys.argv, list))333 self.assert _(sys.byteorder in("little", "big"))334 self.assert _(isinstance(sys.builtin_module_names, tuple))335 self.assert _(isinstance(sys.copyright, basestring))336 self.assert _(isinstance(sys.exec_prefix, basestring))337 self.assert _(isinstance(sys.executable, basestring))388 self.assertIsInstance(sys.api_version, int) 389 self.assertIsInstance(sys.argv, list) 390 self.assertIn(sys.byteorder, ("little", "big")) 391 self.assertIsInstance(sys.builtin_module_names, tuple) 392 self.assertIsInstance(sys.copyright, basestring) 393 self.assertIsInstance(sys.exec_prefix, basestring) 394 self.assertIsInstance(sys.executable, basestring) 338 395 self.assertEqual(len(sys.float_info), 11) 339 396 self.assertEqual(sys.float_info.radix, 2) 340 self.assert_(isinstance(sys.hexversion, int)) 341 self.assert_(isinstance(sys.maxint, int)) 397 self.assertEqual(len(sys.long_info), 2) 398 self.assertTrue(sys.long_info.bits_per_digit % 5 == 0) 399 self.assertTrue(sys.long_info.sizeof_digit >= 1) 400 self.assertEqual(type(sys.long_info.bits_per_digit), int) 401 self.assertEqual(type(sys.long_info.sizeof_digit), int) 402 self.assertIsInstance(sys.hexversion, int) 403 self.assertIsInstance(sys.maxint, int) 342 404 if test.test_support.have_unicode: 343 self.assert _(isinstance(sys.maxunicode, int))344 self.assert _(isinstance(sys.platform, basestring))345 self.assert _(isinstance(sys.prefix, basestring))346 self.assert _(isinstance(sys.version, basestring))405 self.assertIsInstance(sys.maxunicode, int) 406 self.assertIsInstance(sys.platform, basestring) 407 self.assertIsInstance(sys.prefix, basestring) 408 self.assertIsInstance(sys.version, basestring) 347 409 vi = sys.version_info 348 self.assert _(isinstance(vi, tuple))410 self.assertIsInstance(vi[:], tuple) 349 411 self.assertEqual(len(vi), 5) 350 self.assert_(isinstance(vi[0], int)) 351 self.assert_(isinstance(vi[1], int)) 352 self.assert_(isinstance(vi[2], int)) 353 self.assert_(vi[3] in ("alpha", "beta", "candidate", "final")) 354 self.assert_(isinstance(vi[4], int)) 412 self.assertIsInstance(vi[0], int) 413 self.assertIsInstance(vi[1], int) 414 self.assertIsInstance(vi[2], int) 415 self.assertIn(vi[3], ("alpha", "beta", "candidate", "final")) 416 self.assertIsInstance(vi[4], int) 417 self.assertIsInstance(vi.major, int) 418 self.assertIsInstance(vi.minor, int) 419 self.assertIsInstance(vi.micro, int) 420 self.assertIn(vi.releaselevel, ("alpha", "beta", "candidate", "final")) 421 self.assertIsInstance(vi.serial, int) 422 self.assertEqual(vi[0], vi.major) 423 self.assertEqual(vi[1], vi.minor) 424 self.assertEqual(vi[2], vi.micro) 425 self.assertEqual(vi[3], vi.releaselevel) 426 self.assertEqual(vi[4], vi.serial) 427 self.assertTrue(vi > (1,0,0)) 428 self.assertIsInstance(sys.float_repr_style, str) 429 self.assertIn(sys.float_repr_style, ('short', 'legacy')) 355 430 356 431 def test_43581(self): 357 432 # Can't use sys.stdout, as this is a cStringIO object when 358 433 # the test runs under regrtest. 359 self.assert _(sys.__stdout__.encoding == sys.__stderr__.encoding)434 self.assertTrue(sys.__stdout__.encoding == sys.__stderr__.encoding) 360 435 361 436 def test_sys_flags(self): 362 self. failUnless(sys.flags)437 self.assertTrue(sys.flags) 363 438 attrs = ("debug", "py3k_warning", "division_warning", "division_new", 364 439 "inspect", "interactive", "optimize", "dont_write_bytecode", 365 440 "no_site", "ignore_environment", "tabcheck", "verbose", 366 "unicode", "bytes_warning" )441 "unicode", "bytes_warning", "hash_randomization") 367 442 for attr in attrs: 368 self.assert _(hasattr(sys.flags, attr), attr)443 self.assertTrue(hasattr(sys.flags, attr), attr) 369 444 self.assertEqual(type(getattr(sys.flags, attr)), int, attr) 370 self.assert _(repr(sys.flags))445 self.assertTrue(repr(sys.flags)) 371 446 372 447 def test_clear_type_cache(self): … … 374 449 375 450 def test_ioencoding(self): 376 import subprocess ,os451 import subprocess 377 452 env = dict(os.environ) 378 453 … … 383 458 p = subprocess.Popen([sys.executable, "-c", 'print unichr(0xa2)'], 384 459 stdout = subprocess.PIPE, env=env) 385 out = p. stdout.read().strip()460 out = p.communicate()[0].strip() 386 461 self.assertEqual(out, unichr(0xa2).encode("cp424")) 387 462 … … 389 464 p = subprocess.Popen([sys.executable, "-c", 'print unichr(0xa2)'], 390 465 stdout = subprocess.PIPE, env=env) 391 out = p. stdout.read().strip()466 out = p.communicate()[0].strip() 392 467 self.assertEqual(out, '?') 393 468 … … 396 471 self.assertRaises(TypeError, sys.call_tracing, str, 2) 397 472 473 def test_executable(self): 474 # sys.executable should be absolute 475 self.assertEqual(os.path.abspath(sys.executable), sys.executable) 476 477 # Issue #7774: Ensure that sys.executable is an empty string if argv[0] 478 # has been set to an non existent program name and Python is unable to 479 # retrieve the real program name 480 import subprocess 481 # For a normal installation, it should work without 'cwd' 482 # argument. For test runs in the build directory, see #7774. 483 python_dir = os.path.dirname(os.path.realpath(sys.executable)) 484 p = subprocess.Popen( 485 ["nonexistent", "-c", 'import sys; print repr(sys.executable)'], 486 executable=sys.executable, stdout=subprocess.PIPE, cwd=python_dir) 487 executable = p.communicate()[0].strip() 488 p.wait() 489 self.assertIn(executable, ["''", repr(sys.executable)]) 398 490 399 491 class SizeofTest(unittest.TestCase): 400 492 401 TPFLAGS_HAVE_GC = 1<<14402 TPFLAGS_HEAPTYPE = 1L<<9403 404 493 def setUp(self): 405 self.c = len(struct.pack('c', ' ')) 406 self.H = len(struct.pack('H', 0)) 407 self.i = len(struct.pack('i', 0)) 408 self.l = len(struct.pack('l', 0)) 409 self.P = len(struct.pack('P', 0)) 410 # due to missing size_t information from struct, it is assumed that 411 # sizeof(Py_ssize_t) = sizeof(void*) 412 self.header = 'PP' 413 self.vheader = self.header + 'P' 414 if hasattr(sys, "gettotalrefcount"): 415 self.header += '2P' 416 self.vheader += '2P' 494 self.P = struct.calcsize('P') 495 self.longdigit = sys.long_info.sizeof_digit 417 496 import _testcapi 418 497 self.gc_headsize = _testcapi.SIZEOF_PYGC_HEAD … … 423 502 test.test_support.unlink(test.test_support.TESTFN) 424 503 425 def check_sizeof(self, o, size): 426 result = sys.getsizeof(o) 427 if ((type(o) == type) and (o.__flags__ & self.TPFLAGS_HEAPTYPE) or\ 428 ((type(o) != type) and (type(o).__flags__ & self.TPFLAGS_HAVE_GC))): 429 size += self.gc_headsize 430 msg = 'wrong size for %s: got %d, expected %d' \ 431 % (type(o), result, size) 432 self.assertEqual(result, size, msg) 433 434 def calcsize(self, fmt): 435 """Wrapper around struct.calcsize which enforces the alignment of the 436 end of a structure to the alignment requirement of pointer. 437 438 Note: This wrapper should only be used if a pointer member is included 439 and no member with a size larger than a pointer exists. 440 """ 441 return struct.calcsize(fmt + '0P') 504 check_sizeof = test.test_support.check_sizeof 442 505 443 506 def test_gc_head_size(self): 444 507 # Check that the gc header size is added to objects tracked by the gc. 445 h = self.header 446 size = self.calcsize 508 size = test.test_support.calcobjsize 447 509 gc_header_size = self.gc_headsize 448 510 # bool objects are not gc tracked 449 self.assertEqual(sys.getsizeof(True), size( h +'l'))511 self.assertEqual(sys.getsizeof(True), size('l')) 450 512 # but lists are 451 self.assertEqual(sys.getsizeof([]), size( h +'P PP') + gc_header_size)513 self.assertEqual(sys.getsizeof([]), size('P PP') + gc_header_size) 452 514 453 515 def test_default(self): 454 h = self.header 455 size = self.calcsize 456 self.assertEqual(sys.getsizeof(True, -1), size(h + 'l')) 516 size = test.test_support.calcobjsize 517 self.assertEqual(sys.getsizeof(True, -1), size('l')) 457 518 458 519 def test_objecttypes(self): 459 520 # check all types defined in Objects/ 460 h = self.header 461 vh = self.vheader 462 size = self.calcsize 521 size = test.test_support.calcobjsize 522 vsize = test.test_support.calcvobjsize 463 523 check = self.check_sizeof 464 524 # bool 465 check(True, size( h +'l'))525 check(True, size('l')) 466 526 # buffer 467 check(buffer(''), size(h + '2P2Pil')) 527 with test.test_support.check_py3k_warnings(): 528 check(buffer(''), size('2P2Pil')) 468 529 # builtin_function_or_method 469 check(len, size( h +'3P'))530 check(len, size('3P')) 470 531 # bytearray 471 532 samples = ['', 'u'*100000] 472 533 for sample in samples: 473 534 x = bytearray(sample) 474 check(x, size(vh + 'iPP') + x.__alloc__() * self.c)535 check(x, vsize('iPP') + x.__alloc__()) 475 536 # bytearray_iterator 476 check(iter(bytearray()), size( h +'PP'))537 check(iter(bytearray()), size('PP')) 477 538 # cell 478 539 def get_cell(): … … 481 542 return x 482 543 return inner 483 check(get_cell().func_closure[0], size( h +'P'))544 check(get_cell().func_closure[0], size('P')) 484 545 # classobj (old-style class) 485 546 class class_oldstyle(): 486 547 def method(): 487 548 pass 488 check(class_oldstyle, size( h + '6P'))549 check(class_oldstyle, size('7P')) 489 550 # instance (old-style class) 490 check(class_oldstyle(), size( h +'3P'))551 check(class_oldstyle(), size('3P')) 491 552 # instancemethod (old-style class) 492 check(class_oldstyle().method, size( h +'4P'))553 check(class_oldstyle().method, size('4P')) 493 554 # complex 494 check(complex(0,1), size( h +'2d'))555 check(complex(0,1), size('2d')) 495 556 # code 496 check(get_cell().func_code, size( h + '4i8Pi2P'))557 check(get_cell().func_code, size('4i8Pi3P')) 497 558 # BaseException 498 check(BaseException(), size( h +'3P'))559 check(BaseException(), size('3P')) 499 560 # UnicodeEncodeError 500 check(UnicodeEncodeError("", u"", 0, 0, ""), size( h +'5P2PP'))561 check(UnicodeEncodeError("", u"", 0, 0, ""), size('5P2PP')) 501 562 # UnicodeDecodeError 502 check(UnicodeDecodeError("", "", 0, 0, ""), size( h +'5P2PP'))563 check(UnicodeDecodeError("", "", 0, 0, ""), size('5P2PP')) 503 564 # UnicodeTranslateError 504 check(UnicodeTranslateError(u"", 0, 1, ""), size( h +'5P2PP'))565 check(UnicodeTranslateError(u"", 0, 1, ""), size('5P2PP')) 505 566 # method_descriptor (descriptor object) 506 check(str.lower, size( h +'2PP'))567 check(str.lower, size('2PP')) 507 568 # classmethod_descriptor (descriptor object) 508 569 # XXX 509 570 # member_descriptor (descriptor object) 510 571 import datetime 511 check(datetime.timedelta.days, size( h +'2PP'))572 check(datetime.timedelta.days, size('2PP')) 512 573 # getset_descriptor (descriptor object) 513 574 import __builtin__ 514 check(__builtin__.file.closed, size( h +'2PP'))575 check(__builtin__.file.closed, size('2PP')) 515 576 # wrapper_descriptor (descriptor object) 516 check(int.__add__, size( h +'2P2P'))577 check(int.__add__, size('2P2P')) 517 578 # dictproxy 518 579 class C(object): pass 519 check(C.__dict__, size( h +'P'))580 check(C.__dict__, size('P')) 520 581 # method-wrapper (descriptor object) 521 check({}.__iter__, size( h +'2P'))582 check({}.__iter__, size('2P')) 522 583 # dict 523 check({}, size( h +'3P2P' + 8*'P2P'))584 check({}, size('3P2P' + 8*'P2P')) 524 585 x = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8} 525 check(x, size( h + '3P2P' + 8*'P2P') + 16*size('P2P'))586 check(x, size('3P2P' + 8*'P2P') + 16*struct.calcsize('P2P')) 526 587 # dictionary-keyiterator 527 check({}.iterkeys(), size( h +'P2PPP'))588 check({}.iterkeys(), size('P2PPP')) 528 589 # dictionary-valueiterator 529 check({}.itervalues(), size( h +'P2PPP'))590 check({}.itervalues(), size('P2PPP')) 530 591 # dictionary-itemiterator 531 check({}.iteritems(), size( h +'P2PPP'))592 check({}.iteritems(), size('P2PPP')) 532 593 # ellipses 533 check(Ellipsis, size( h +''))594 check(Ellipsis, size('')) 534 595 # EncodingMap 535 596 import codecs, encodings.iso8859_3 536 597 x = codecs.charmap_build(encodings.iso8859_3.decoding_table) 537 check(x, size( h +'32B2iB'))598 check(x, size('32B2iB')) 538 599 # enumerate 539 check(enumerate([]), size( h +'l3P'))600 check(enumerate([]), size('l3P')) 540 601 # file 541 check(self.file, size( h +'4P2i4P3i3P3i'))602 check(self.file, size('4P2i4P3i3P3i')) 542 603 # float 543 check(float(0), size( h +'d'))604 check(float(0), size('d')) 544 605 # sys.floatinfo 545 check(sys.float_info, size(vh) + self.P * len(sys.float_info))606 check(sys.float_info, vsize('') + self.P * len(sys.float_info)) 546 607 # frame 547 608 import inspect … … 552 613 extras = x.f_code.co_stacksize + x.f_code.co_nlocals +\ 553 614 ncells + nfrees - 1 554 check(x, size(vh +'12P3i' + CO_MAXBLOCKS*'3i' + 'P' + extras*'P'))615 check(x, vsize('12P3i' + CO_MAXBLOCKS*'3i' + 'P' + extras*'P')) 555 616 # function 556 617 def func(): pass 557 check(func, size( h +'9P'))618 check(func, size('9P')) 558 619 class c(): 559 620 @staticmethod … … 564 625 pass 565 626 # staticmethod 566 check(foo, size( h +'P'))627 check(foo, size('P')) 567 628 # classmethod 568 check(bar, size( h +'P'))629 check(bar, size('P')) 569 630 # generator 570 631 def get_gen(): yield 1 571 check(get_gen(), size( h +'Pi2P'))632 check(get_gen(), size('Pi2P')) 572 633 # integer 573 check(1, size( h +'l'))574 check(100, size( h +'l'))634 check(1, size('l')) 635 check(100, size('l')) 575 636 # iterator 576 check(iter('abc'), size( h +'lP'))637 check(iter('abc'), size('lP')) 577 638 # callable-iterator 578 639 import re 579 check(re.finditer('',''), size( h +'2P'))640 check(re.finditer('',''), size('2P')) 580 641 # list 581 642 samples = [[], [1,2,3], ['1', '2', '3']] 582 643 for sample in samples: 583 check(sample, size(vh +'PP') + len(sample)*self.P)644 check(sample, vsize('PP') + len(sample)*self.P) 584 645 # sortwrapper (list) 585 646 # XXX … … 587 648 # XXX 588 649 # listiterator (list) 589 check(iter([]), size( h +'lP'))650 check(iter([]), size('lP')) 590 651 # listreverseiterator (list) 591 check(reversed([]), size( h +'lP'))652 check(reversed([]), size('lP')) 592 653 # long 593 check(0L, size(vh + 'H') - self.H) 594 check(1L, size(vh + 'H')) 595 check(-1L, size(vh + 'H')) 596 check(32768L, size(vh + 'H') + self.H) 597 check(32768L*32768L-1, size(vh + 'H') + self.H) 598 check(32768L*32768L, size(vh + 'H') + 2*self.H) 654 check(0L, vsize('')) 655 check(1L, vsize('') + self.longdigit) 656 check(-1L, vsize('') + self.longdigit) 657 PyLong_BASE = 2**sys.long_info.bits_per_digit 658 check(long(PyLong_BASE), vsize('') + 2*self.longdigit) 659 check(long(PyLong_BASE**2-1), vsize('') + 2*self.longdigit) 660 check(long(PyLong_BASE**2), vsize('') + 3*self.longdigit) 599 661 # module 600 check(unittest, size( h +'P'))662 check(unittest, size('P')) 601 663 # None 602 check(None, size( h +''))664 check(None, size('')) 603 665 # object 604 check(object(), size( h +''))666 check(object(), size('')) 605 667 # property (descriptor object) 606 668 class C(object): … … 609 671 def delx(self): del self.__x 610 672 x = property(getx, setx, delx, "") 611 check(x, size( h +'4Pi'))673 check(x, size('4Pi')) 612 674 # PyCObject 675 # PyCapsule 613 676 # XXX 614 677 # rangeiterator 615 check(iter(xrange(1)), size( h +'4l'))678 check(iter(xrange(1)), size('4l')) 616 679 # reverse 617 check(reversed(''), size( h +'PP'))680 check(reversed(''), size('PP')) 618 681 # set 619 682 # frozenset 620 683 PySet_MINSIZE = 8 621 684 samples = [[], range(10), range(50)] 622 s = size( h +'3P2P' + PySet_MINSIZE*'lP' + 'lP')685 s = size('3P2P' + PySet_MINSIZE*'lP' + 'lP') 623 686 for sample in samples: 624 687 minused = len(sample) … … 637 700 check(frozenset(sample), s + newsize*struct.calcsize('lP')) 638 701 # setiterator 639 check(iter(set()), size( h +'P3P'))702 check(iter(set()), size('P3P')) 640 703 # slice 641 check(slice(1), size( h +'3P'))704 check(slice(1), size('3P')) 642 705 # str 643 check('', size(vh + 'lic')) 644 check('abc', size(vh + 'lic') + 3*self.c) 706 vh = test.test_support._vheader 707 check('', struct.calcsize(vh + 'lic')) 708 check('abc', struct.calcsize(vh + 'lic') + 3) 645 709 # super 646 check(super(int), size( h +'3P'))710 check(super(int), size('3P')) 647 711 # tuple 648 check((), size(vh))649 check((1,2,3), size(vh) + 3*self.P)712 check((), vsize('')) 713 check((1,2,3), vsize('') + 3*self.P) 650 714 # tupleiterator 651 check(iter(()), size( h +'lP'))715 check(iter(()), size('lP')) 652 716 # type 653 717 # (PyTypeObject + PyNumberMethods + PyMappingMethods + 654 718 # PySequenceMethods + PyBufferProcs) 655 s = size(vh + 'P2P15Pl4PP9PP11PI') +size('41P 10P 3P 6P')719 s = vsize('P2P15Pl4PP9PP11PI') + struct.calcsize('41P 10P 3P 6P') 656 720 class newstyleclass(object): 657 721 pass … … 668 732 # has been cached 669 733 for s in samples: 670 check(s, size( h +'PPlP') + usize * (len(s) + 1))734 check(s, size('PPlP') + usize * (len(s) + 1)) 671 735 # weakref 672 736 import weakref 673 check(weakref.ref(int), size( h +'2Pl2P'))737 check(weakref.ref(int), size('2Pl2P')) 674 738 # weakproxy 675 739 # XXX 676 740 # weakcallableproxy 677 check(weakref.proxy(int), size( h +'2Pl2P'))741 check(weakref.proxy(int), size('2Pl2P')) 678 742 # xrange 679 check(xrange(1), size( h +'3l'))680 check(xrange(66000), size( h +'3l'))743 check(xrange(1), size('3l')) 744 check(xrange(66000), size('3l')) 681 745 682 746 def test_pythontypes(self): 683 747 # check all types defined in Python/ 684 h = self.header 685 vh = self.vheader 686 size = self.calcsize 748 size = test.test_support.calcobjsize 749 vsize = test.test_support.calcvobjsize 687 750 check = self.check_sizeof 688 751 # _ast.AST 689 752 import _ast 690 check(_ast.AST(), size( h +''))753 check(_ast.AST(), size('')) 691 754 # imp.NullImporter 692 755 import imp 693 check(imp.NullImporter(self.file.name), size( h +''))756 check(imp.NullImporter(self.file.name), size('')) 694 757 try: 695 758 raise TypeError … … 698 761 # traceback 699 762 if tb != None: 700 check(tb, size( h +'2P2i'))763 check(tb, size('2P2i')) 701 764 # symtable entry 702 765 # XXX 703 766 # sys.flags 704 check(sys.flags, size(vh) + self.P * len(sys.flags))767 check(sys.flags, vsize('') + self.P * len(sys.flags)) 705 768 706 769
Note:
See TracChangeset
for help on using the changeset viewer.