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

Side by Side Diff: Lib/test/test_sys.py

Issue 16510: Using appropriate checks in tests
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:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_sysconfig.py ('k') | Lib/test/test_telnetlib.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
(...skipping 27 matching lines...) Expand all
38 sys.stdout = out 38 sys.stdout = out
39 39
40 dh = sys.__displayhook__ 40 dh = sys.__displayhook__
41 41
42 self.assertRaises(TypeError, dh) 42 self.assertRaises(TypeError, dh)
43 if hasattr(builtins, "_"): 43 if hasattr(builtins, "_"):
44 del builtins._ 44 del builtins._
45 45
46 dh(None) 46 dh(None)
47 self.assertEqual(out.getvalue(), "") 47 self.assertEqual(out.getvalue(), "")
48 self.assertTrue(not hasattr(builtins, "_")) 48 self.assertFalse(hasattr(builtins, "_"))
49 dh(42) 49 dh(42)
50 self.assertEqual(out.getvalue(), "42\n") 50 self.assertEqual(out.getvalue(), "42\n")
51 self.assertEqual(builtins._, 42) 51 self.assertEqual(builtins._, 42)
52 52
53 del sys.stdout 53 del sys.stdout
54 self.assertRaises(RuntimeError, dh, 42) 54 self.assertRaises(RuntimeError, dh, 42)
55 55
56 def test_lost_displayhook(self): 56 def test_lost_displayhook(self):
57 del sys.displayhook 57 del sys.displayhook
58 code = compile("42", "<string>", "single") 58 code = compile("42", "<string>", "single")
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 self.assertEqual(sys.getcheckinterval(), n) 194 self.assertEqual(sys.getcheckinterval(), n)
195 195
196 @unittest.skipUnless(threading, 'Threading required for this test.') 196 @unittest.skipUnless(threading, 'Threading required for this test.')
197 def test_switchinterval(self): 197 def test_switchinterval(self):
198 self.assertRaises(TypeError, sys.setswitchinterval) 198 self.assertRaises(TypeError, sys.setswitchinterval)
199 self.assertRaises(TypeError, sys.setswitchinterval, "a") 199 self.assertRaises(TypeError, sys.setswitchinterval, "a")
200 self.assertRaises(ValueError, sys.setswitchinterval, -1.0) 200 self.assertRaises(ValueError, sys.setswitchinterval, -1.0)
201 self.assertRaises(ValueError, sys.setswitchinterval, 0.0) 201 self.assertRaises(ValueError, sys.setswitchinterval, 0.0)
202 orig = sys.getswitchinterval() 202 orig = sys.getswitchinterval()
203 # sanity check 203 # sanity check
204 self.assertTrue(orig < 0.5, orig) 204 self.assertLess(orig, 0.5)
205 try: 205 try:
206 for n in 0.00001, 0.05, 3.0, orig: 206 for n in 0.00001, 0.05, 3.0, orig:
207 sys.setswitchinterval(n) 207 sys.setswitchinterval(n)
208 self.assertAlmostEqual(sys.getswitchinterval(), n) 208 self.assertAlmostEqual(sys.getswitchinterval(), n)
209 finally: 209 finally:
210 sys.setswitchinterval(orig) 210 sys.setswitchinterval(orig)
211 211
212 def test_recursionlimit(self): 212 def test_recursionlimit(self):
213 self.assertRaises(TypeError, sys.getrecursionlimit, 42) 213 self.assertRaises(TypeError, sys.getrecursionlimit, 42)
214 oldlimit = sys.getrecursionlimit() 214 oldlimit = sys.getrecursionlimit()
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 thread_id = thread_info[0] 374 thread_id = thread_info[0]
375 375
376 d = sys._current_frames() 376 d = sys._current_frames()
377 377
378 main_id = threading.get_ident() 378 main_id = threading.get_ident()
379 self.assertIn(main_id, d) 379 self.assertIn(main_id, d)
380 self.assertIn(thread_id, d) 380 self.assertIn(thread_id, d)
381 381
382 # Verify that the captured main-thread frame is _this_ frame. 382 # Verify that the captured main-thread frame is _this_ frame.
383 frame = d.pop(main_id) 383 frame = d.pop(main_id)
384 self.assertTrue(frame is sys._getframe()) 384 self.assertIs(frame, sys._getframe())
385 385
386 # Verify that the captured thread frame is blocked in g456, called 386 # Verify that the captured thread frame is blocked in g456, called
387 # from f123. This is a litte tricky, since various bits of 387 # from f123. This is a litte tricky, since various bits of
388 # threading.py are also in the thread's call stack. 388 # threading.py are also in the thread's call stack.
389 frame = d.pop(thread_id) 389 frame = d.pop(thread_id)
390 stack = traceback.extract_stack(frame) 390 stack = traceback.extract_stack(frame)
391 for i, (filename, lineno, funcname, sourceline) in enumerate(stack): 391 for i, (filename, lineno, funcname, sourceline) in enumerate(stack):
392 if funcname == "f123": 392 if funcname == "f123":
393 break 393 break
394 else: 394 else:
(...skipping 10 matching lines...) Expand all
405 leave_g.set() 405 leave_g.set()
406 t.join() 406 t.join()
407 407
408 # Test sys._current_frames() when thread support doesn't exist. 408 # Test sys._current_frames() when thread support doesn't exist.
409 def current_frames_without_threads(self): 409 def current_frames_without_threads(self):
410 # Not much happens here: there is only one thread, with artificial 410 # Not much happens here: there is only one thread, with artificial
411 # "thread id" 0. 411 # "thread id" 0.
412 d = sys._current_frames() 412 d = sys._current_frames()
413 self.assertEqual(len(d), 1) 413 self.assertEqual(len(d), 1)
414 self.assertIn(0, d) 414 self.assertIn(0, d)
415 self.assertTrue(d[0] is sys._getframe()) 415 self.assertIs(d[0], sys._getframe())
416 416
417 def test_attributes(self): 417 def test_attributes(self):
418 self.assertIsInstance(sys.api_version, int) 418 self.assertIsInstance(sys.api_version, int)
419 self.assertIsInstance(sys.argv, list) 419 self.assertIsInstance(sys.argv, list)
420 self.assertIn(sys.byteorder, ("little", "big")) 420 self.assertIn(sys.byteorder, ("little", "big"))
421 self.assertIsInstance(sys.builtin_module_names, tuple) 421 self.assertIsInstance(sys.builtin_module_names, tuple)
422 self.assertIsInstance(sys.copyright, str) 422 self.assertIsInstance(sys.copyright, str)
423 self.assertIsInstance(sys.exec_prefix, str) 423 self.assertIsInstance(sys.exec_prefix, str)
424 self.assertIsInstance(sys.base_exec_prefix, str) 424 self.assertIsInstance(sys.base_exec_prefix, str)
425 self.assertIsInstance(sys.executable, str) 425 self.assertIsInstance(sys.executable, str)
426 self.assertEqual(len(sys.float_info), 11) 426 self.assertEqual(len(sys.float_info), 11)
427 self.assertEqual(sys.float_info.radix, 2) 427 self.assertEqual(sys.float_info.radix, 2)
428 self.assertEqual(len(sys.int_info), 2) 428 self.assertEqual(len(sys.int_info), 2)
429 self.assertTrue(sys.int_info.bits_per_digit % 5 == 0) 429 self.assertEqual(sys.int_info.bits_per_digit % 5, 0)
430 self.assertTrue(sys.int_info.sizeof_digit >= 1) 430 self.assertGreaterEqual(sys.int_info.sizeof_digit, 1)
431 self.assertEqual(type(sys.int_info.bits_per_digit), int) 431 self.assertEqual(type(sys.int_info.bits_per_digit), int)
432 self.assertEqual(type(sys.int_info.sizeof_digit), int) 432 self.assertEqual(type(sys.int_info.sizeof_digit), int)
433 self.assertIsInstance(sys.hexversion, int) 433 self.assertIsInstance(sys.hexversion, int)
434 434
435 self.assertEqual(len(sys.hash_info), 9) 435 self.assertEqual(len(sys.hash_info), 9)
436 self.assertLess(sys.hash_info.modulus, 2**sys.hash_info.width) 436 self.assertLess(sys.hash_info.modulus, 2**sys.hash_info.width)
437 # 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
438 # probable primality test (doesn't exclude the possibility of 438 # probable primality test (doesn't exclude the possibility of
439 # a Carmichael number) 439 # a Carmichael number)
440 for x in range(1, 100): 440 for x in range(1, 100):
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
482 self.assertIsInstance(vi.major, int) 482 self.assertIsInstance(vi.major, int)
483 self.assertIsInstance(vi.minor, int) 483 self.assertIsInstance(vi.minor, int)
484 self.assertIsInstance(vi.micro, int) 484 self.assertIsInstance(vi.micro, int)
485 self.assertIn(vi.releaselevel, ("alpha", "beta", "candidate", "final")) 485 self.assertIn(vi.releaselevel, ("alpha", "beta", "candidate", "final"))
486 self.assertIsInstance(vi.serial, int) 486 self.assertIsInstance(vi.serial, int)
487 self.assertEqual(vi[0], vi.major) 487 self.assertEqual(vi[0], vi.major)
488 self.assertEqual(vi[1], vi.minor) 488 self.assertEqual(vi[1], vi.minor)
489 self.assertEqual(vi[2], vi.micro) 489 self.assertEqual(vi[2], vi.micro)
490 self.assertEqual(vi[3], vi.releaselevel) 490 self.assertEqual(vi[3], vi.releaselevel)
491 self.assertEqual(vi[4], vi.serial) 491 self.assertEqual(vi[4], vi.serial)
492 self.assertTrue(vi > (1,0,0)) 492 self.assertGreater(vi, (1,0,0))
493 self.assertIsInstance(sys.float_repr_style, str) 493 self.assertIsInstance(sys.float_repr_style, str)
494 self.assertIn(sys.float_repr_style, ('short', 'legacy')) 494 self.assertIn(sys.float_repr_style, ('short', 'legacy'))
495 if not sys.platform.startswith('win'): 495 if not sys.platform.startswith('win'):
496 self.assertIsInstance(sys.abiflags, str) 496 self.assertIsInstance(sys.abiflags, str)
497 497
498 @unittest.skipUnless(hasattr(sys, 'thread_info'), 498 @unittest.skipUnless(hasattr(sys, 'thread_info'),
499 'Threading required for this test.') 499 'Threading required for this test.')
500 def test_thread_info(self): 500 def test_thread_info(self):
501 info = sys.thread_info 501 info = sys.thread_info
502 self.assertEqual(len(info), 3) 502 self.assertEqual(len(info), 3)
503 self.assertIn(info.name, ('nt', 'pthread', 'solaris', None)) 503 self.assertIn(info.name, ('nt', 'pthread', 'solaris', None))
504 self.assertIn(info.lock, ('semaphore', 'mutex+cond', None)) 504 self.assertIn(info.lock, ('semaphore', 'mutex+cond', None))
505 505
506 def test_43581(self): 506 def test_43581(self):
507 # Can't use sys.stdout, as this is a StringIO object when 507 # Can't use sys.stdout, as this is a StringIO object when
508 # the test runs under regrtest. 508 # the test runs under regrtest.
509 self.assertEqual(sys.__stdout__.encoding, sys.__stderr__.encoding) 509 self.assertEqual(sys.__stdout__.encoding, sys.__stderr__.encoding)
510 510
511 def test_intern(self): 511 def test_intern(self):
512 global numruns 512 global numruns
513 numruns += 1 513 numruns += 1
514 self.assertRaises(TypeError, sys.intern) 514 self.assertRaises(TypeError, sys.intern)
515 s = "never interned before" + str(numruns) 515 s = "never interned before" + str(numruns)
516 self.assertTrue(sys.intern(s) is s) 516 self.assertIs(sys.intern(s), s)
517 s2 = s.swapcase().swapcase() 517 s2 = s.swapcase().swapcase()
518 self.assertTrue(sys.intern(s2) is s) 518 self.assertIs(sys.intern(s2), s)
519 519
520 # Subclasses of string can't be interned, because they 520 # Subclasses of string can't be interned, because they
521 # provide too much opportunity for insane things to happen. 521 # provide too much opportunity for insane things to happen.
522 # We don't want them in the interned dict and if they aren't 522 # We don't want them in the interned dict and if they aren't
523 # actually interned, we don't want to create the appearance 523 # actually interned, we don't want to create the appearance
524 # that they are by allowing intern() to succeed. 524 # that they are by allowing intern() to succeed.
525 class S(str): 525 class S(str):
526 def __hash__(self): 526 def __hash__(self):
527 return 123 527 return 123
528 528
(...skipping 462 matching lines...) Expand 10 before | Expand all | Expand 10 after
991 # XXX 991 # XXX
992 # sys.flags 992 # sys.flags
993 check(sys.flags, vsize('') + self.P * len(sys.flags)) 993 check(sys.flags, vsize('') + self.P * len(sys.flags))
994 994
995 995
996 def test_main(): 996 def test_main():
997 test.support.run_unittest(SysModuleTest, SizeofTest) 997 test.support.run_unittest(SysModuleTest, SizeofTest)
998 998
999 if __name__ == "__main__": 999 if __name__ == "__main__":
1000 test_main() 1000 test_main()
OLDNEW
« no previous file with comments | « Lib/test/test_sysconfig.py ('k') | Lib/test/test_telnetlib.py » ('j') | no next file with comments »

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