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

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

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