Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(90463)

Delta Between Two Patch Sets: Lib/test/test_sys.py

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 5 years, 9 months ago
Right Patch Set: Created 5 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_sysconfig.py ('k') | Lib/test/test_telnetlib.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 import unittest, test.support 1 import unittest, test.support
2 import sys, io, os 2 import sys, io, os
3 import struct 3 import struct
4 import subprocess 4 import subprocess
5 import textwrap 5 import textwrap
6 import warnings 6 import warnings
7 import operator 7 import operator
8 import codecs 8 import codecs
9 import gc 9 import gc
10 import sysconfig 10 import sysconfig
11 import platform
11 12
12 # count the number of test runs, used to create unique 13 # count the number of test runs, used to create unique
13 # strings to intern in test_intern() 14 # strings to intern in test_intern()
14 numruns = 0 15 numruns = 0
15 16
16 try: 17 try:
17 import threading 18 import threading
18 except ImportError: 19 except ImportError:
19 threading = None 20 threading = None
20 21
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after
424 self.assertIsInstance(sys.executable, str) 425 self.assertIsInstance(sys.executable, str)
425 self.assertEqual(len(sys.float_info), 11) 426 self.assertEqual(len(sys.float_info), 11)
426 self.assertEqual(sys.float_info.radix, 2) 427 self.assertEqual(sys.float_info.radix, 2)
427 self.assertEqual(len(sys.int_info), 2) 428 self.assertEqual(len(sys.int_info), 2)
428 self.assertEqual(sys.int_info.bits_per_digit % 5, 0) 429 self.assertEqual(sys.int_info.bits_per_digit % 5, 0)
429 self.assertGreaterEqual(sys.int_info.sizeof_digit, 1) 430 self.assertGreaterEqual(sys.int_info.sizeof_digit, 1)
430 self.assertEqual(type(sys.int_info.bits_per_digit), int) 431 self.assertEqual(type(sys.int_info.bits_per_digit), int)
431 self.assertEqual(type(sys.int_info.sizeof_digit), int) 432 self.assertEqual(type(sys.int_info.sizeof_digit), int)
432 self.assertIsInstance(sys.hexversion, int) 433 self.assertIsInstance(sys.hexversion, int)
433 434
434 self.assertEqual(len(sys.hash_info), 5) 435 self.assertEqual(len(sys.hash_info), 9)
435 self.assertLess(sys.hash_info.modulus, 2**sys.hash_info.width) 436 self.assertLess(sys.hash_info.modulus, 2**sys.hash_info.width)
436 # sys.hash_info.modulus should be a prime; we do a quick 437 # sys.hash_info.modulus should be a prime; we do a quick
437 # probable primality test (doesn't exclude the possibility of 438 # probable primality test (doesn't exclude the possibility of
438 # a Carmichael number) 439 # a Carmichael number)
439 for x in range(1, 100): 440 for x in range(1, 100):
440 self.assertEqual( 441 self.assertEqual(
441 pow(x, sys.hash_info.modulus-1, sys.hash_info.modulus), 442 pow(x, sys.hash_info.modulus-1, sys.hash_info.modulus),
442 1, 443 1,
443 "sys.hash_info.modulus {} is a non-prime".format( 444 "sys.hash_info.modulus {} is a non-prime".format(
444 sys.hash_info.modulus) 445 sys.hash_info.modulus)
445 ) 446 )
446 self.assertIsInstance(sys.hash_info.inf, int) 447 self.assertIsInstance(sys.hash_info.inf, int)
447 self.assertIsInstance(sys.hash_info.nan, int) 448 self.assertIsInstance(sys.hash_info.nan, int)
448 self.assertIsInstance(sys.hash_info.imag, int) 449 self.assertIsInstance(sys.hash_info.imag, int)
450 algo = sysconfig.get_config_var("Py_HASH_ALGORITHM")
451 if sys.hash_info.algorithm in {"fnv", "siphash24"}:
452 self.assertIn(sys.hash_info.hash_bits, {32, 64})
453 self.assertIn(sys.hash_info.seed_bits, {32, 64, 128})
454
455 if algo == 1:
456 self.assertEqual(sys.hash_info.algorithm, "siphash24")
457 elif algo == 2:
458 self.assertEqual(sys.hash_info.algorithm, "fnv")
459 else:
460 self.assertIn(sys.hash_info.algorithm, {"fnv", "siphash24"})
461 else:
462 # PY_HASH_EXTERNAL
463 self.assertEqual(algo, 0)
464 self.assertGreaterEqual(sys.hash_info.cutoff, 0)
465 self.assertLess(sys.hash_info.cutoff, 8)
449 466
450 self.assertIsInstance(sys.maxsize, int) 467 self.assertIsInstance(sys.maxsize, int)
451 self.assertIsInstance(sys.maxunicode, int) 468 self.assertIsInstance(sys.maxunicode, int)
452 self.assertEqual(sys.maxunicode, 0x10FFFF) 469 self.assertEqual(sys.maxunicode, 0x10FFFF)
453 self.assertIsInstance(sys.platform, str) 470 self.assertIsInstance(sys.platform, str)
454 self.assertIsInstance(sys.prefix, str) 471 self.assertIsInstance(sys.prefix, str)
455 self.assertIsInstance(sys.base_prefix, str) 472 self.assertIsInstance(sys.base_prefix, str)
456 self.assertIsInstance(sys.version, str) 473 self.assertIsInstance(sys.version, str)
457 vi = sys.version_info 474 vi = sys.version_info
458 self.assertIsInstance(vi[:], tuple) 475 self.assertIsInstance(vi[:], tuple)
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
641 self.assertEqual(sys.implementation.name, 658 self.assertEqual(sys.implementation.name,
642 sys.implementation.name.lower()) 659 sys.implementation.name.lower())
643 660
644 def test_debugmallocstats(self): 661 def test_debugmallocstats(self):
645 # Test sys._debugmallocstats() 662 # Test sys._debugmallocstats()
646 from test.script_helper import assert_python_ok 663 from test.script_helper import assert_python_ok
647 args = ['-c', 'import sys; sys._debugmallocstats()'] 664 args = ['-c', 'import sys; sys._debugmallocstats()']
648 ret, out, err = assert_python_ok(*args) 665 ret, out, err = assert_python_ok(*args)
649 self.assertIn(b"free PyDictObjects", err) 666 self.assertIn(b"free PyDictObjects", err)
650 667
668 # The function has no parameter
669 self.assertRaises(TypeError, sys._debugmallocstats, True)
670
651 @unittest.skipUnless(hasattr(sys, "getallocatedblocks"), 671 @unittest.skipUnless(hasattr(sys, "getallocatedblocks"),
652 "sys.getallocatedblocks unavailable on this build") 672 "sys.getallocatedblocks unavailable on this build")
653 def test_getallocatedblocks(self): 673 def test_getallocatedblocks(self):
654 # Some sanity checks 674 # Some sanity checks
655 with_pymalloc = sysconfig.get_config_var('WITH_PYMALLOC') 675 with_pymalloc = sysconfig.get_config_var('WITH_PYMALLOC')
656 a = sys.getallocatedblocks() 676 a = sys.getallocatedblocks()
657 self.assertIs(type(a), int) 677 self.assertIs(type(a), int)
658 if with_pymalloc: 678 if with_pymalloc:
659 self.assertGreater(a, 0) 679 self.assertGreater(a, 0)
660 else: 680 else:
(...skipping 10 matching lines...) Expand all
671 # gettotalrefcount() not available 691 # gettotalrefcount() not available
672 pass 692 pass
673 gc.collect() 693 gc.collect()
674 b = sys.getallocatedblocks() 694 b = sys.getallocatedblocks()
675 self.assertLessEqual(b, a) 695 self.assertLessEqual(b, a)
676 gc.collect() 696 gc.collect()
677 c = sys.getallocatedblocks() 697 c = sys.getallocatedblocks()
678 self.assertIn(c, range(b - 50, b + 50)) 698 self.assertIn(c, range(b - 50, b + 50))
679 699
680 700
701 @test.support.cpython_only
681 class SizeofTest(unittest.TestCase): 702 class SizeofTest(unittest.TestCase):
682 703
683 def setUp(self): 704 def setUp(self):
684 self.P = struct.calcsize('P') 705 self.P = struct.calcsize('P')
685 self.longdigit = sys.int_info.sizeof_digit 706 self.longdigit = sys.int_info.sizeof_digit
686 import _testcapi 707 import _testcapi
687 self.gc_headsize = _testcapi.SIZEOF_PYGC_HEAD 708 self.gc_headsize = _testcapi.SIZEOF_PYGC_HEAD
688 self.file = open(test.support.TESTFN, 'wb') 709 self.file = open(test.support.TESTFN, 'wb')
689 710
690 def tearDown(self): 711 def tearDown(self):
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
794 # sys.floatinfo 815 # sys.floatinfo
795 check(sys.float_info, vsize('') + self.P * len(sys.float_info)) 816 check(sys.float_info, vsize('') + self.P * len(sys.float_info))
796 # frame 817 # frame
797 import inspect 818 import inspect
798 CO_MAXBLOCKS = 20 819 CO_MAXBLOCKS = 20
799 x = inspect.currentframe() 820 x = inspect.currentframe()
800 ncells = len(x.f_code.co_cellvars) 821 ncells = len(x.f_code.co_cellvars)
801 nfrees = len(x.f_code.co_freevars) 822 nfrees = len(x.f_code.co_freevars)
802 extras = x.f_code.co_stacksize + x.f_code.co_nlocals +\ 823 extras = x.f_code.co_stacksize + x.f_code.co_nlocals +\
803 ncells + nfrees - 1 824 ncells + nfrees - 1
804 check(x, vsize('13P3ic' + CO_MAXBLOCKS*'3i' + 'P' + extras*'P')) 825 check(x, vsize('12P3ic' + CO_MAXBLOCKS*'3i' + 'P' + extras*'P'))
805 # function 826 # function
806 def func(): pass 827 def func(): pass
807 check(func, size('12P')) 828 check(func, size('12P'))
808 class c(): 829 class c():
809 @staticmethod 830 @staticmethod
810 def foo(): 831 def foo():
811 pass 832 pass
812 @classmethod 833 @classmethod
813 def bar(cls): 834 def bar(cls):
814 pass 835 pass
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
970 # XXX 991 # XXX
971 # sys.flags 992 # sys.flags
972 check(sys.flags, vsize('') + self.P * len(sys.flags)) 993 check(sys.flags, vsize('') + self.P * len(sys.flags))
973 994
974 995
975 def test_main(): 996 def test_main():
976 test.support.run_unittest(SysModuleTest, SizeofTest) 997 test.support.run_unittest(SysModuleTest, SizeofTest)
977 998
978 if __name__ == "__main__": 999 if __name__ == "__main__":
979 test_main() 1000 test_main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+