test_exceptions.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import unittest
  2. class ExceptionTest(unittest.TestCase):
  3. def test_finally(self):
  4. finally_ran = False
  5. try:
  6. pass
  7. finally:
  8. finally_ran = True
  9. self.assertTrue(finally_ran)
  10. finally_ran = False
  11. try:
  12. raise Exception()
  13. except:
  14. pass
  15. else:
  16. self.assertFalse(True, "'else' should not fire if exception raised")
  17. finally:
  18. finally_ran = True
  19. self.assertTrue(finally_ran)
  20. finally_ran = False
  21. try:
  22. try:
  23. raise Exception()
  24. finally:
  25. finally_ran = True
  26. self.assertTrue(False, "No re-raise after 'finally'")
  27. except:
  28. pass
  29. self.assertTrue(finally_ran)
  30. def test_finally_return(self):
  31. finally_ran = [False]
  32. def r1():
  33. try:
  34. return 42
  35. self.assertTrue(False, "Execution continued after return")
  36. finally:
  37. finally_ran[0] = True
  38. self.assertEqual(r1(), 42)
  39. self.assertTrue(finally_ran[0], "'finally' block did not run")
  40. def r2():
  41. try:
  42. return 42
  43. self.assertTrue(False, "Execution continued after return")
  44. finally:
  45. return 43
  46. self.assertEqual(r2(), 43)
  47. def r3():
  48. try:
  49. raise Exception()
  50. finally:
  51. return 42
  52. self.assertEqual(r3(), 42)
  53. finally_ran = [False]
  54. def r4():
  55. try:
  56. raise Exception()
  57. except:
  58. return 42
  59. finally:
  60. finally_ran[0] = True
  61. self.assertEqual(r4(), 42);
  62. self.assertTrue(finally_ran[0], "'finally' block did not run")
  63. def r5():
  64. try:
  65. raise Exception()
  66. except:
  67. return 42
  68. finally:
  69. return 43
  70. self.assertEqual(r5(), 43)
  71. finally_ran = [False, False]
  72. def r6():
  73. try:
  74. try:
  75. raise Exception()
  76. finally:
  77. finally_ran[0] = True
  78. return 42
  79. finally:
  80. finally_ran[1] = True
  81. self.assertEqual(r6(), 42)
  82. self.assertEqual(finally_ran, [True, True])
  83. def r7():
  84. try:
  85. return 42
  86. finally:
  87. raise Exception()
  88. self.assertRaises(Exception, r7)
  89. def test_finally_break_continue(self):
  90. finally_ran = [False,False]
  91. normal_execution_continued = False
  92. try:
  93. while True:
  94. try:
  95. try:
  96. break
  97. finally:
  98. finally_ran[0] = True
  99. self.assertFalse(True, "Execution got past 'break' statement")
  100. finally:
  101. finally_ran[1] = True
  102. self.assertFalse(True, "Execution got past 'break' statement")
  103. normal_execution_continued = True
  104. finally:
  105. self.assertEqual(finally_ran, [True, True]);
  106. self.assertTrue(normal_execution_continued, "'break' skipped too many finallies")
  107. finally_ran = [False,False]
  108. normal_execution_continued = False
  109. try:
  110. while not finally_ran[0]:
  111. try:
  112. try:
  113. continue
  114. finally:
  115. finally_ran[0] = True
  116. self.assertFalse(True, "Execution got past 'continue' statement")
  117. finally:
  118. finally_ran[1] = True
  119. self.assertFalse(True, "Execution got past 'continue' statement")
  120. normal_execution_continued = True
  121. finally:
  122. self.assertEqual(finally_ran, [True, True]);
  123. self.assertTrue(normal_execution_continued, "'continue' skipped too many finallies")
  124. if __name__ == "__main__":
  125. unittest.main()