Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1 | # Python test set -- part 5, built-in exceptions |
| 2 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3 | import os |
| 4 | import sys |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 5 | import unittest |
Guido van Rossum | bf12cdb | 2006-08-17 20:24:18 +0000 | [diff] [blame] | 6 | import pickle |
Barry Warsaw | 8d109cb | 2008-05-08 04:26:35 +0000 | [diff] [blame] | 7 | import weakref |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 8 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 9 | from test.support import TESTFN, unlink, run_unittest |
Guido van Rossum | 83b120d | 2001-08-23 03:23:03 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 11 | # XXX This is not really enough, each *operation* should be tested! |
| 12 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 13 | class ExceptionTests(unittest.TestCase): |
Barry Warsaw | b9c1d3d | 2001-08-13 23:07:00 +0000 | [diff] [blame] | 14 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 15 | def raise_catch(self, exc, excname): |
| 16 | try: |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 17 | raise exc("spam") |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 18 | except exc as err: |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 19 | buf1 = str(err) |
| 20 | try: |
| 21 | raise exc("spam") |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 22 | except exc as err: |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 23 | buf2 = str(err) |
| 24 | self.assertEquals(buf1, buf2) |
| 25 | self.assertEquals(exc.__name__, excname) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 26 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 27 | def testRaising(self): |
| 28 | self.raise_catch(AttributeError, "AttributeError") |
| 29 | self.assertRaises(AttributeError, getattr, sys, "undefined_attribute") |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 30 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 31 | self.raise_catch(EOFError, "EOFError") |
| 32 | fp = open(TESTFN, 'w') |
| 33 | fp.close() |
| 34 | fp = open(TESTFN, 'r') |
| 35 | savestdin = sys.stdin |
| 36 | try: |
| 37 | try: |
| 38 | import marshal |
| 39 | marshal.loads('') |
| 40 | except EOFError: |
| 41 | pass |
| 42 | finally: |
| 43 | sys.stdin = savestdin |
| 44 | fp.close() |
| 45 | unlink(TESTFN) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 46 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 47 | self.raise_catch(IOError, "IOError") |
| 48 | self.assertRaises(IOError, open, 'this file does not exist', 'r') |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 49 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 50 | self.raise_catch(ImportError, "ImportError") |
| 51 | self.assertRaises(ImportError, __import__, "undefined_module") |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 52 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 53 | self.raise_catch(IndexError, "IndexError") |
| 54 | x = [] |
| 55 | self.assertRaises(IndexError, x.__getitem__, 10) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 56 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 57 | self.raise_catch(KeyError, "KeyError") |
| 58 | x = {} |
| 59 | self.assertRaises(KeyError, x.__getitem__, 'key') |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 60 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 61 | self.raise_catch(KeyboardInterrupt, "KeyboardInterrupt") |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 62 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 63 | self.raise_catch(MemoryError, "MemoryError") |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 64 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 65 | self.raise_catch(NameError, "NameError") |
| 66 | try: x = undefined_variable |
| 67 | except NameError: pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 68 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 69 | self.raise_catch(OverflowError, "OverflowError") |
| 70 | x = 1 |
| 71 | for dummy in range(128): |
| 72 | x += x # this simply shouldn't blow up |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 73 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 74 | self.raise_catch(RuntimeError, "RuntimeError") |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 75 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 76 | self.raise_catch(SyntaxError, "SyntaxError") |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 77 | try: exec('/\n') |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 78 | except SyntaxError: pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 79 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 80 | self.raise_catch(IndentationError, "IndentationError") |
Fred Drake | 72e48bd | 2000-09-08 16:32:34 +0000 | [diff] [blame] | 81 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 82 | self.raise_catch(TabError, "TabError") |
| 83 | # can only be tested under -tt, and is the only test for -tt |
| 84 | #try: compile("try:\n\t1/0\n \t1/0\nfinally:\n pass\n", '<string>', 'exec') |
| 85 | #except TabError: pass |
| 86 | #else: self.fail("TabError not raised") |
Fred Drake | 72e48bd | 2000-09-08 16:32:34 +0000 | [diff] [blame] | 87 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 88 | self.raise_catch(SystemError, "SystemError") |
Fred Drake | 72e48bd | 2000-09-08 16:32:34 +0000 | [diff] [blame] | 89 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 90 | self.raise_catch(SystemExit, "SystemExit") |
| 91 | self.assertRaises(SystemExit, sys.exit, 0) |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 92 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 93 | self.raise_catch(TypeError, "TypeError") |
| 94 | try: [] + () |
| 95 | except TypeError: pass |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 96 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 97 | self.raise_catch(ValueError, "ValueError") |
Guido van Rossum | e63bae6 | 2007-07-17 00:34:25 +0000 | [diff] [blame] | 98 | self.assertRaises(ValueError, chr, 17<<16) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 99 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 100 | self.raise_catch(ZeroDivisionError, "ZeroDivisionError") |
| 101 | try: x = 1/0 |
| 102 | except ZeroDivisionError: pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 103 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 104 | self.raise_catch(Exception, "Exception") |
| 105 | try: x = 1/0 |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 106 | except Exception as e: pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 107 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 108 | def testSyntaxErrorMessage(self): |
| 109 | # make sure the right exception message is raised for each of |
| 110 | # these code fragments |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 111 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 112 | def ckmsg(src, msg): |
| 113 | try: |
| 114 | compile(src, '<fragment>', 'exec') |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 115 | except SyntaxError as e: |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 116 | if e.msg != msg: |
| 117 | self.fail("expected %s, got %s" % (msg, e.msg)) |
| 118 | else: |
| 119 | self.fail("failed to get expected SyntaxError") |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 120 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 121 | s = '''while 1: |
| 122 | try: |
| 123 | pass |
| 124 | finally: |
| 125 | continue''' |
Barry Warsaw | 992cb8a | 2000-05-25 23:16:54 +0000 | [diff] [blame] | 126 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 127 | if not sys.platform.startswith('java'): |
| 128 | ckmsg(s, "'continue' not supported inside 'finally' clause") |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 129 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 130 | s = '''if 1: |
| 131 | try: |
| 132 | continue |
| 133 | except: |
| 134 | pass''' |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 135 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 136 | ckmsg(s, "'continue' not properly in loop") |
| 137 | ckmsg("continue\n", "'continue' not properly in loop") |
Thomas Wouters | 303de6a | 2006-04-20 22:42:37 +0000 | [diff] [blame] | 138 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 139 | def testSettingException(self): |
| 140 | # test that setting an exception at the C level works even if the |
| 141 | # exception object can't be constructed. |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 142 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 143 | class BadException(Exception): |
| 144 | def __init__(self_): |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 145 | raise RuntimeError("can't instantiate BadException") |
Finn Bock | aa3dc45 | 2001-12-08 10:15:48 +0000 | [diff] [blame] | 146 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 147 | class InvalidException: |
| 148 | pass |
Thomas Wouters | 303de6a | 2006-04-20 22:42:37 +0000 | [diff] [blame] | 149 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 150 | def test_capi1(): |
| 151 | import _testcapi |
| 152 | try: |
| 153 | _testcapi.raise_exception(BadException, 1) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 154 | except TypeError as err: |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 155 | exc, err, tb = sys.exc_info() |
| 156 | co = tb.tb_frame.f_code |
| 157 | self.assertEquals(co.co_name, "test_capi1") |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 158 | self.assert_(co.co_filename.endswith('test_exceptions.py')) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 159 | else: |
| 160 | self.fail("Expected exception") |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 161 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 162 | def test_capi2(): |
| 163 | import _testcapi |
| 164 | try: |
| 165 | _testcapi.raise_exception(BadException, 0) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 166 | except RuntimeError as err: |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 167 | exc, err, tb = sys.exc_info() |
| 168 | co = tb.tb_frame.f_code |
| 169 | self.assertEquals(co.co_name, "__init__") |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 170 | self.assert_(co.co_filename.endswith('test_exceptions.py')) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 171 | co2 = tb.tb_frame.f_back.f_code |
| 172 | self.assertEquals(co2.co_name, "test_capi2") |
| 173 | else: |
| 174 | self.fail("Expected exception") |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 175 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 176 | def test_capi3(): |
| 177 | import _testcapi |
| 178 | self.assertRaises(SystemError, _testcapi.raise_exception, |
| 179 | InvalidException, 1) |
| 180 | |
| 181 | if not sys.platform.startswith('java'): |
| 182 | test_capi1() |
| 183 | test_capi2() |
| 184 | test_capi3() |
| 185 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 186 | def test_WindowsError(self): |
| 187 | try: |
| 188 | WindowsError |
| 189 | except NameError: |
| 190 | pass |
| 191 | else: |
| 192 | self.failUnlessEqual(str(WindowsError(1001)), |
| 193 | "1001") |
| 194 | self.failUnlessEqual(str(WindowsError(1001, "message")), |
| 195 | "[Error 1001] message") |
| 196 | self.failUnlessEqual(WindowsError(1001, "message").errno, 22) |
| 197 | self.failUnlessEqual(WindowsError(1001, "message").winerror, 1001) |
| 198 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 199 | def testAttributes(self): |
| 200 | # test that exception attributes are happy |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 201 | |
| 202 | exceptionList = [ |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 203 | (BaseException, (), {'args' : ()}), |
| 204 | (BaseException, (1, ), {'args' : (1,)}), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 205 | (BaseException, ('foo',), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 206 | {'args' : ('foo',)}), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 207 | (BaseException, ('foo', 1), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 208 | {'args' : ('foo', 1)}), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 209 | (SystemExit, ('foo',), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 210 | {'args' : ('foo',), 'code' : 'foo'}), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 211 | (IOError, ('foo',), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 212 | {'args' : ('foo',), 'filename' : None, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 213 | 'errno' : None, 'strerror' : None}), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 214 | (IOError, ('foo', 'bar'), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 215 | {'args' : ('foo', 'bar'), 'filename' : None, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 216 | 'errno' : 'foo', 'strerror' : 'bar'}), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 217 | (IOError, ('foo', 'bar', 'baz'), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 218 | {'args' : ('foo', 'bar'), 'filename' : 'baz', |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 219 | 'errno' : 'foo', 'strerror' : 'bar'}), |
| 220 | (IOError, ('foo', 'bar', 'baz', 'quux'), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 221 | {'args' : ('foo', 'bar', 'baz', 'quux')}), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 222 | (EnvironmentError, ('errnoStr', 'strErrorStr', 'filenameStr'), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 223 | {'args' : ('errnoStr', 'strErrorStr'), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 224 | 'strerror' : 'strErrorStr', 'errno' : 'errnoStr', |
| 225 | 'filename' : 'filenameStr'}), |
| 226 | (EnvironmentError, (1, 'strErrorStr', 'filenameStr'), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 227 | {'args' : (1, 'strErrorStr'), 'errno' : 1, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 228 | 'strerror' : 'strErrorStr', 'filename' : 'filenameStr'}), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 229 | (SyntaxError, (), {'msg' : None, 'text' : None, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 230 | 'filename' : None, 'lineno' : None, 'offset' : None, |
| 231 | 'print_file_and_line' : None}), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 232 | (SyntaxError, ('msgStr',), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 233 | {'args' : ('msgStr',), 'text' : None, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 234 | 'print_file_and_line' : None, 'msg' : 'msgStr', |
| 235 | 'filename' : None, 'lineno' : None, 'offset' : None}), |
| 236 | (SyntaxError, ('msgStr', ('filenameStr', 'linenoStr', 'offsetStr', |
| 237 | 'textStr')), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 238 | {'offset' : 'offsetStr', 'text' : 'textStr', |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 239 | 'args' : ('msgStr', ('filenameStr', 'linenoStr', |
| 240 | 'offsetStr', 'textStr')), |
| 241 | 'print_file_and_line' : None, 'msg' : 'msgStr', |
| 242 | 'filename' : 'filenameStr', 'lineno' : 'linenoStr'}), |
| 243 | (SyntaxError, ('msgStr', 'filenameStr', 'linenoStr', 'offsetStr', |
| 244 | 'textStr', 'print_file_and_lineStr'), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 245 | {'text' : None, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 246 | 'args' : ('msgStr', 'filenameStr', 'linenoStr', 'offsetStr', |
| 247 | 'textStr', 'print_file_and_lineStr'), |
| 248 | 'print_file_and_line' : None, 'msg' : 'msgStr', |
| 249 | 'filename' : None, 'lineno' : None, 'offset' : None}), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 250 | (UnicodeError, (), {'args' : (),}), |
Walter Dörwald | eceb0fb | 2007-05-24 17:49:56 +0000 | [diff] [blame] | 251 | (UnicodeEncodeError, ('ascii', 'a', 0, 1, |
| 252 | 'ordinal not in range'), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 253 | {'args' : ('ascii', 'a', 0, 1, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 254 | 'ordinal not in range'), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 255 | 'encoding' : 'ascii', 'object' : 'a', |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 256 | 'start' : 0, 'reason' : 'ordinal not in range'}), |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 257 | (UnicodeDecodeError, ('ascii', bytearray(b'\xff'), 0, 1, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 258 | 'ordinal not in range'), |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 259 | {'args' : ('ascii', bytearray(b'\xff'), 0, 1, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 260 | 'ordinal not in range'), |
| 261 | 'encoding' : 'ascii', 'object' : b'\xff', |
| 262 | 'start' : 0, 'reason' : 'ordinal not in range'}), |
Walter Dörwald | eceb0fb | 2007-05-24 17:49:56 +0000 | [diff] [blame] | 263 | (UnicodeDecodeError, ('ascii', b'\xff', 0, 1, |
| 264 | 'ordinal not in range'), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 265 | {'args' : ('ascii', b'\xff', 0, 1, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 266 | 'ordinal not in range'), |
Guido van Rossum | b8142c3 | 2007-05-08 17:49:10 +0000 | [diff] [blame] | 267 | 'encoding' : 'ascii', 'object' : b'\xff', |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 268 | 'start' : 0, 'reason' : 'ordinal not in range'}), |
Walter Dörwald | eceb0fb | 2007-05-24 17:49:56 +0000 | [diff] [blame] | 269 | (UnicodeTranslateError, ("\u3042", 0, 1, "ouch"), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 270 | {'args' : ('\u3042', 0, 1, 'ouch'), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 271 | 'object' : '\u3042', 'reason' : 'ouch', |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 272 | 'start' : 0, 'end' : 1}), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 273 | ] |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 274 | try: |
| 275 | exceptionList.append( |
| 276 | (WindowsError, (1, 'strErrorStr', 'filenameStr'), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 277 | {'args' : (1, 'strErrorStr'), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 278 | 'strerror' : 'strErrorStr', 'winerror' : 1, |
| 279 | 'errno' : 22, 'filename' : 'filenameStr'}) |
| 280 | ) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 281 | except NameError: |
| 282 | pass |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 283 | |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 284 | for exc, args, expected in exceptionList: |
| 285 | try: |
| 286 | e = exc(*args) |
| 287 | except: |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 288 | print("\nexc=%r, args=%r" % (exc, args), file=sys.stderr) |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 289 | raise |
| 290 | else: |
| 291 | # Verify module name |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 292 | self.assertEquals(type(e).__module__, 'builtins') |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 293 | # Verify no ref leaks in Exc_str() |
| 294 | s = str(e) |
| 295 | for checkArgName in expected: |
| 296 | value = getattr(e, checkArgName) |
| 297 | self.assertEquals(repr(value), |
| 298 | repr(expected[checkArgName]), |
| 299 | '%r.%s == %r, expected %r' % ( |
| 300 | e, checkArgName, |
| 301 | value, expected[checkArgName])) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 302 | |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 303 | # test for pickling support |
Guido van Rossum | 99603b0 | 2007-07-20 00:22:32 +0000 | [diff] [blame] | 304 | for p in [pickle]: |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 305 | for protocol in range(p.HIGHEST_PROTOCOL + 1): |
| 306 | s = p.dumps(e, protocol) |
| 307 | new = p.loads(s) |
| 308 | for checkArgName in expected: |
| 309 | got = repr(getattr(new, checkArgName)) |
| 310 | want = repr(expected[checkArgName]) |
| 311 | self.assertEquals(got, want, |
| 312 | 'pickled "%r", attribute "%s' % |
| 313 | (e, checkArgName)) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 314 | |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 315 | def testWithTraceback(self): |
| 316 | try: |
| 317 | raise IndexError(4) |
| 318 | except: |
| 319 | tb = sys.exc_info()[2] |
| 320 | |
| 321 | e = BaseException().with_traceback(tb) |
| 322 | self.failUnless(isinstance(e, BaseException)) |
| 323 | self.assertEqual(e.__traceback__, tb) |
| 324 | |
| 325 | e = IndexError(5).with_traceback(tb) |
| 326 | self.failUnless(isinstance(e, IndexError)) |
| 327 | self.assertEqual(e.__traceback__, tb) |
| 328 | |
| 329 | class MyException(Exception): |
| 330 | pass |
| 331 | |
| 332 | e = MyException().with_traceback(tb) |
| 333 | self.failUnless(isinstance(e, MyException)) |
| 334 | self.assertEqual(e.__traceback__, tb) |
| 335 | |
| 336 | def testInvalidTraceback(self): |
| 337 | try: |
| 338 | Exception().__traceback__ = 5 |
| 339 | except TypeError as e: |
| 340 | self.failUnless("__traceback__ must be a traceback" in str(e)) |
| 341 | else: |
| 342 | self.fail("No exception raised") |
| 343 | |
| 344 | def testNoneClearsTracebackAttr(self): |
| 345 | try: |
| 346 | raise IndexError(4) |
| 347 | except: |
| 348 | tb = sys.exc_info()[2] |
| 349 | |
| 350 | e = Exception() |
| 351 | e.__traceback__ = tb |
| 352 | e.__traceback__ = None |
| 353 | self.assertEqual(e.__traceback__, None) |
| 354 | |
| 355 | def testChainingAttrs(self): |
| 356 | e = Exception() |
| 357 | self.assertEqual(e.__context__, None) |
| 358 | self.assertEqual(e.__cause__, None) |
| 359 | |
| 360 | e = TypeError() |
| 361 | self.assertEqual(e.__context__, None) |
| 362 | self.assertEqual(e.__cause__, None) |
| 363 | |
| 364 | class MyException(EnvironmentError): |
| 365 | pass |
| 366 | |
| 367 | e = MyException() |
| 368 | self.assertEqual(e.__context__, None) |
| 369 | self.assertEqual(e.__cause__, None) |
| 370 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 371 | def testKeywordArgs(self): |
| 372 | # test that builtin exception don't take keyword args, |
| 373 | # but user-defined subclasses can if they want |
| 374 | self.assertRaises(TypeError, BaseException, a=1) |
| 375 | |
| 376 | class DerivedException(BaseException): |
| 377 | def __init__(self, fancy_arg): |
| 378 | BaseException.__init__(self) |
| 379 | self.fancy_arg = fancy_arg |
| 380 | |
| 381 | x = DerivedException(fancy_arg=42) |
| 382 | self.assertEquals(x.fancy_arg, 42) |
| 383 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 384 | def testInfiniteRecursion(self): |
| 385 | def f(): |
| 386 | return f() |
| 387 | self.assertRaises(RuntimeError, f) |
| 388 | |
| 389 | def g(): |
| 390 | try: |
| 391 | return g() |
| 392 | except ValueError: |
| 393 | return -1 |
| 394 | self.assertRaises(RuntimeError, g) |
| 395 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 396 | def testUnicodeStrUsage(self): |
| 397 | # Make sure both instances and classes have a str and unicode |
| 398 | # representation. |
| 399 | self.failUnless(str(Exception)) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 400 | self.failUnless(str(Exception)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 401 | self.failUnless(str(Exception('a'))) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 402 | self.failUnless(str(Exception('a'))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 403 | |
Barry Warsaw | 8d109cb | 2008-05-08 04:26:35 +0000 | [diff] [blame] | 404 | def testExceptionCleanupNames(self): |
| 405 | # Make sure the local variable bound to the exception instance by |
| 406 | # an "except" statement is only visible inside the except block. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 407 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 408 | try: |
| 409 | raise Exception() |
| 410 | except Exception as e: |
| 411 | self.failUnless(e) |
| 412 | del e |
| 413 | self.failIf('e' in locals()) |
| 414 | |
Barry Warsaw | 8d109cb | 2008-05-08 04:26:35 +0000 | [diff] [blame] | 415 | def testExceptionCleanupState(self): |
| 416 | # Make sure exception state is cleaned up as soon as the except |
| 417 | # block is left. See #2507 |
| 418 | |
| 419 | class MyException(Exception): |
| 420 | def __init__(self, obj): |
| 421 | self.obj = obj |
| 422 | class MyObj: |
| 423 | pass |
| 424 | |
| 425 | def inner_raising_func(): |
| 426 | # Create some references in exception value and traceback |
| 427 | local_ref = obj |
| 428 | raise MyException(obj) |
| 429 | |
| 430 | obj = MyObj() |
| 431 | wr = weakref.ref(obj) |
| 432 | try: |
| 433 | inner_raising_func() |
| 434 | except MyException as e: |
| 435 | pass |
| 436 | obj = None |
| 437 | obj = wr() |
| 438 | self.failUnless(obj is None, "%s" % obj) |
| 439 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 440 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 441 | def test_main(): |
| 442 | run_unittest(ExceptionTests) |
| 443 | |
| 444 | if __name__ == '__main__': |
Guido van Rossum | b8142c3 | 2007-05-08 17:49:10 +0000 | [diff] [blame] | 445 | unittest.main() |