| 1 | #!/usr/bin/env python2
|
| 2 | from __future__ import print_function
|
| 3 | """Test exceptions for Byterun."""
|
| 4 |
|
| 5 | import unittest
|
| 6 |
|
| 7 | import vmtest
|
| 8 |
|
| 9 | import six
|
| 10 |
|
| 11 | PY3, PY2 = six.PY3, not six.PY3
|
| 12 |
|
| 13 |
|
| 14 | class TestExceptions(vmtest.VmTestCase):
|
| 15 | def test_catching_exceptions(self):
|
| 16 | # Catch the exception precisely
|
| 17 | self.assert_ok("""\
|
| 18 | try:
|
| 19 | [][1]
|
| 20 | print("Shouldn't be here...")
|
| 21 | except IndexError:
|
| 22 | print("caught it!")
|
| 23 | """)
|
| 24 | # Catch the exception by a parent class
|
| 25 | self.assert_ok("""\
|
| 26 | try:
|
| 27 | [][1]
|
| 28 | print("Shouldn't be here...")
|
| 29 | except Exception:
|
| 30 | print("caught it!")
|
| 31 | """)
|
| 32 | # Catch all exceptions
|
| 33 | self.assert_ok("""\
|
| 34 | try:
|
| 35 | [][1]
|
| 36 | print("Shouldn't be here...")
|
| 37 | except:
|
| 38 | print("caught it!")
|
| 39 | """)
|
| 40 |
|
| 41 | def test_raise_exception(self):
|
| 42 | self.assert_ok("raise Exception('oops')", raises=Exception)
|
| 43 |
|
| 44 | def test_raise_exception_class(self):
|
| 45 | self.assert_ok("raise ValueError", raises=ValueError)
|
| 46 |
|
| 47 | if PY2:
|
| 48 | def test_raise_exception_2args(self):
|
| 49 | self.assert_ok("raise ValueError, 'bad'", raises=ValueError)
|
| 50 |
|
| 51 | def test_raise_exception_3args(self):
|
| 52 | self.assert_ok("""\
|
| 53 | from sys import exc_info
|
| 54 | try:
|
| 55 | raise Exception
|
| 56 | except:
|
| 57 | _, _, tb = exc_info()
|
| 58 | raise ValueError, "message", tb
|
| 59 | """, raises=ValueError)
|
| 60 |
|
| 61 | def test_raise_and_catch_exception(self):
|
| 62 | self.assert_ok("""\
|
| 63 | try:
|
| 64 | raise ValueError("oops")
|
| 65 | except ValueError as e:
|
| 66 | print("Caught: %s" % e)
|
| 67 | print("All done")
|
| 68 | """)
|
| 69 |
|
| 70 | if PY3:
|
| 71 | def test_raise_exception_from(self):
|
| 72 | self.assert_ok(
|
| 73 | "raise ValueError from NameError",
|
| 74 | raises=ValueError
|
| 75 | )
|
| 76 |
|
| 77 | def test_raise_and_catch_exception_in_function(self):
|
| 78 | self.assert_ok("""\
|
| 79 | def fn():
|
| 80 | raise ValueError("oops")
|
| 81 |
|
| 82 | try:
|
| 83 | fn()
|
| 84 | except ValueError as e:
|
| 85 | print("Caught: %s" % e)
|
| 86 | print("done")
|
| 87 | """)
|
| 88 |
|
| 89 | def test_global_name_error(self):
|
| 90 | self.assert_ok("fooey", raises=NameError)
|
| 91 | self.assert_ok("""\
|
| 92 | try:
|
| 93 | fooey
|
| 94 | print("Yes fooey?")
|
| 95 | except NameError:
|
| 96 | print("No fooey")
|
| 97 | """)
|
| 98 |
|
| 99 | def test_local_name_error(self):
|
| 100 | self.assert_ok("""\
|
| 101 | def fn():
|
| 102 | fooey
|
| 103 | fn()
|
| 104 | """, raises=NameError)
|
| 105 |
|
| 106 | def test_catch_local_name_error(self):
|
| 107 | self.assert_ok("""\
|
| 108 | def fn():
|
| 109 | try:
|
| 110 | fooey
|
| 111 | print("Yes fooey?")
|
| 112 | except NameError:
|
| 113 | print("No fooey")
|
| 114 | fn()
|
| 115 | """)
|
| 116 |
|
| 117 | def test_reraise(self):
|
| 118 | self.assert_ok("""\
|
| 119 | def fn():
|
| 120 | try:
|
| 121 | fooey
|
| 122 | print("Yes fooey?")
|
| 123 | except NameError:
|
| 124 | print("No fooey")
|
| 125 | raise
|
| 126 | fn()
|
| 127 | """, raises=NameError)
|
| 128 |
|
| 129 | def test_reraise_explicit_exception(self):
|
| 130 | self.assert_ok("""\
|
| 131 | def fn():
|
| 132 | try:
|
| 133 | raise ValueError("ouch")
|
| 134 | except ValueError as e:
|
| 135 | print("Caught %s" % e)
|
| 136 | raise
|
| 137 | fn()
|
| 138 | """, raises=ValueError)
|
| 139 |
|
| 140 | def test_finally_while_throwing(self):
|
| 141 | self.assert_ok("""\
|
| 142 | def fn():
|
| 143 | try:
|
| 144 | print("About to..")
|
| 145 | raise ValueError("ouch")
|
| 146 | finally:
|
| 147 | print("Finally")
|
| 148 | fn()
|
| 149 | print("Done")
|
| 150 | """, raises=ValueError)
|
| 151 |
|
| 152 | def test_coverage_issue_92(self):
|
| 153 | self.assert_ok("""\
|
| 154 | l = []
|
| 155 | for i in range(3):
|
| 156 | try:
|
| 157 | l.append(i)
|
| 158 | finally:
|
| 159 | l.append('f')
|
| 160 | l.append('e')
|
| 161 | l.append('r')
|
| 162 | print(l)
|
| 163 | assert l == [0, 'f', 'e', 1, 'f', 'e', 2, 'f', 'e', 'r']
|
| 164 | """)
|
| 165 |
|
| 166 |
|
| 167 | if __name__ == '__main__':
|
| 168 | unittest.main()
|