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 | ae5f2f4 | 2010-03-07 17:10:51 +0000 | [diff] [blame] | 9 | from test.support import (TESTFN, unlink, run_unittest, captured_output, |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 10 | gc_collect, cpython_only, no_tracing) |
Guido van Rossum | 83b120d | 2001-08-23 03:23:03 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 12 | # XXX This is not really enough, each *operation* should be tested! |
| 13 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 14 | class ExceptionTests(unittest.TestCase): |
Barry Warsaw | b9c1d3d | 2001-08-13 23:07:00 +0000 | [diff] [blame] | 15 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 16 | def raise_catch(self, exc, excname): |
| 17 | try: |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 18 | raise exc("spam") |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 19 | except exc as err: |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 20 | buf1 = str(err) |
| 21 | try: |
| 22 | raise exc("spam") |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 23 | except exc as err: |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 24 | buf2 = str(err) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 25 | self.assertEqual(buf1, buf2) |
| 26 | self.assertEqual(exc.__name__, excname) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 27 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 28 | def testRaising(self): |
| 29 | self.raise_catch(AttributeError, "AttributeError") |
| 30 | self.assertRaises(AttributeError, getattr, sys, "undefined_attribute") |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 31 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 32 | self.raise_catch(EOFError, "EOFError") |
| 33 | fp = open(TESTFN, 'w') |
| 34 | fp.close() |
| 35 | fp = open(TESTFN, 'r') |
| 36 | savestdin = sys.stdin |
| 37 | try: |
| 38 | try: |
| 39 | import marshal |
| 40 | marshal.loads('') |
| 41 | except EOFError: |
| 42 | pass |
| 43 | finally: |
| 44 | sys.stdin = savestdin |
| 45 | fp.close() |
| 46 | unlink(TESTFN) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 47 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 48 | self.raise_catch(IOError, "IOError") |
| 49 | self.assertRaises(IOError, open, 'this file does not exist', 'r') |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 50 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 51 | self.raise_catch(ImportError, "ImportError") |
| 52 | self.assertRaises(ImportError, __import__, "undefined_module") |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 53 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 54 | self.raise_catch(IndexError, "IndexError") |
| 55 | x = [] |
| 56 | self.assertRaises(IndexError, x.__getitem__, 10) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 57 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 58 | self.raise_catch(KeyError, "KeyError") |
| 59 | x = {} |
| 60 | self.assertRaises(KeyError, x.__getitem__, 'key') |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 61 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 62 | self.raise_catch(KeyboardInterrupt, "KeyboardInterrupt") |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 63 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 64 | self.raise_catch(MemoryError, "MemoryError") |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 65 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 66 | self.raise_catch(NameError, "NameError") |
| 67 | try: x = undefined_variable |
| 68 | except NameError: pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 69 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 70 | self.raise_catch(OverflowError, "OverflowError") |
| 71 | x = 1 |
| 72 | for dummy in range(128): |
| 73 | x += x # this simply shouldn't blow up |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 74 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 75 | self.raise_catch(RuntimeError, "RuntimeError") |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 76 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 77 | self.raise_catch(SyntaxError, "SyntaxError") |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 78 | try: exec('/\n') |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 79 | except SyntaxError: pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 80 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 81 | self.raise_catch(IndentationError, "IndentationError") |
Fred Drake | 72e48bd | 2000-09-08 16:32:34 +0000 | [diff] [blame] | 82 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 83 | self.raise_catch(TabError, "TabError") |
Georg Brandl | e1b5ac6 | 2008-06-04 13:06:58 +0000 | [diff] [blame] | 84 | try: compile("try:\n\t1/0\n \t1/0\nfinally:\n pass\n", |
| 85 | '<string>', 'exec') |
| 86 | except TabError: pass |
| 87 | else: self.fail("TabError not raised") |
Fred Drake | 72e48bd | 2000-09-08 16:32:34 +0000 | [diff] [blame] | 88 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 89 | self.raise_catch(SystemError, "SystemError") |
Fred Drake | 72e48bd | 2000-09-08 16:32:34 +0000 | [diff] [blame] | 90 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 91 | self.raise_catch(SystemExit, "SystemExit") |
| 92 | self.assertRaises(SystemExit, sys.exit, 0) |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 93 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 94 | self.raise_catch(TypeError, "TypeError") |
| 95 | try: [] + () |
| 96 | except TypeError: pass |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 97 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 98 | self.raise_catch(ValueError, "ValueError") |
Guido van Rossum | e63bae6 | 2007-07-17 00:34:25 +0000 | [diff] [blame] | 99 | self.assertRaises(ValueError, chr, 17<<16) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 100 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 101 | self.raise_catch(ZeroDivisionError, "ZeroDivisionError") |
| 102 | try: x = 1/0 |
| 103 | except ZeroDivisionError: pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 104 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 105 | self.raise_catch(Exception, "Exception") |
| 106 | try: x = 1/0 |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 107 | except Exception as e: pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 108 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 109 | def testSyntaxErrorMessage(self): |
| 110 | # make sure the right exception message is raised for each of |
| 111 | # these code fragments |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 112 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 113 | def ckmsg(src, msg): |
| 114 | try: |
| 115 | compile(src, '<fragment>', 'exec') |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 116 | except SyntaxError as e: |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 117 | if e.msg != msg: |
| 118 | self.fail("expected %s, got %s" % (msg, e.msg)) |
| 119 | else: |
| 120 | self.fail("failed to get expected SyntaxError") |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 121 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 122 | s = '''while 1: |
| 123 | try: |
| 124 | pass |
| 125 | finally: |
| 126 | continue''' |
Barry Warsaw | 992cb8a | 2000-05-25 23:16:54 +0000 | [diff] [blame] | 127 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 128 | if not sys.platform.startswith('java'): |
| 129 | ckmsg(s, "'continue' not supported inside 'finally' clause") |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 130 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 131 | s = '''if 1: |
| 132 | try: |
| 133 | continue |
| 134 | except: |
| 135 | pass''' |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 136 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 137 | ckmsg(s, "'continue' not properly in loop") |
| 138 | ckmsg("continue\n", "'continue' not properly in loop") |
Thomas Wouters | 303de6a | 2006-04-20 22:42:37 +0000 | [diff] [blame] | 139 | |
Benjamin Peterson | 17e0bbc | 2010-06-28 15:39:55 +0000 | [diff] [blame] | 140 | @cpython_only |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 141 | def testSettingException(self): |
| 142 | # test that setting an exception at the C level works even if the |
| 143 | # exception object can't be constructed. |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 144 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 145 | class BadException(Exception): |
| 146 | def __init__(self_): |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 147 | raise RuntimeError("can't instantiate BadException") |
Finn Bock | aa3dc45 | 2001-12-08 10:15:48 +0000 | [diff] [blame] | 148 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 149 | class InvalidException: |
| 150 | pass |
Thomas Wouters | 303de6a | 2006-04-20 22:42:37 +0000 | [diff] [blame] | 151 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 152 | def test_capi1(): |
| 153 | import _testcapi |
| 154 | try: |
| 155 | _testcapi.raise_exception(BadException, 1) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 156 | except TypeError as err: |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 157 | exc, err, tb = sys.exc_info() |
| 158 | co = tb.tb_frame.f_code |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 159 | self.assertEqual(co.co_name, "test_capi1") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 160 | self.assertTrue(co.co_filename.endswith('test_exceptions.py')) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 161 | else: |
| 162 | self.fail("Expected exception") |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 163 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 164 | def test_capi2(): |
| 165 | import _testcapi |
| 166 | try: |
| 167 | _testcapi.raise_exception(BadException, 0) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 168 | except RuntimeError as err: |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 169 | exc, err, tb = sys.exc_info() |
| 170 | co = tb.tb_frame.f_code |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 171 | self.assertEqual(co.co_name, "__init__") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 172 | self.assertTrue(co.co_filename.endswith('test_exceptions.py')) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 173 | co2 = tb.tb_frame.f_back.f_code |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 174 | self.assertEqual(co2.co_name, "test_capi2") |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 175 | else: |
| 176 | self.fail("Expected exception") |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 177 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 178 | def test_capi3(): |
| 179 | import _testcapi |
| 180 | self.assertRaises(SystemError, _testcapi.raise_exception, |
| 181 | InvalidException, 1) |
| 182 | |
| 183 | if not sys.platform.startswith('java'): |
| 184 | test_capi1() |
| 185 | test_capi2() |
| 186 | test_capi3() |
| 187 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 188 | def test_WindowsError(self): |
| 189 | try: |
| 190 | WindowsError |
| 191 | except NameError: |
| 192 | pass |
| 193 | else: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 194 | self.assertEqual(str(WindowsError(1001)), "1001") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 195 | self.assertEqual(str(WindowsError(1001, "message")), |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 196 | "[Error 1001] message") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 197 | self.assertEqual(WindowsError(1001, "message").errno, 22) |
| 198 | self.assertEqual(WindowsError(1001, "message").winerror, 1001) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 199 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 200 | def testAttributes(self): |
| 201 | # test that exception attributes are happy |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 202 | |
| 203 | exceptionList = [ |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 204 | (BaseException, (), {'args' : ()}), |
| 205 | (BaseException, (1, ), {'args' : (1,)}), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 206 | (BaseException, ('foo',), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 207 | {'args' : ('foo',)}), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 208 | (BaseException, ('foo', 1), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 209 | {'args' : ('foo', 1)}), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 210 | (SystemExit, ('foo',), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 211 | {'args' : ('foo',), 'code' : 'foo'}), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 212 | (IOError, ('foo',), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 213 | {'args' : ('foo',), 'filename' : None, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 214 | 'errno' : None, 'strerror' : None}), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 215 | (IOError, ('foo', 'bar'), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 216 | {'args' : ('foo', 'bar'), 'filename' : None, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 217 | 'errno' : 'foo', 'strerror' : 'bar'}), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 218 | (IOError, ('foo', 'bar', 'baz'), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 219 | {'args' : ('foo', 'bar'), 'filename' : 'baz', |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 220 | 'errno' : 'foo', 'strerror' : 'bar'}), |
| 221 | (IOError, ('foo', 'bar', 'baz', 'quux'), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 222 | {'args' : ('foo', 'bar', 'baz', 'quux')}), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 223 | (EnvironmentError, ('errnoStr', 'strErrorStr', 'filenameStr'), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 224 | {'args' : ('errnoStr', 'strErrorStr'), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 225 | 'strerror' : 'strErrorStr', 'errno' : 'errnoStr', |
| 226 | 'filename' : 'filenameStr'}), |
| 227 | (EnvironmentError, (1, 'strErrorStr', 'filenameStr'), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 228 | {'args' : (1, 'strErrorStr'), 'errno' : 1, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 229 | 'strerror' : 'strErrorStr', 'filename' : 'filenameStr'}), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 230 | (SyntaxError, (), {'msg' : None, 'text' : None, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 231 | 'filename' : None, 'lineno' : None, 'offset' : None, |
| 232 | 'print_file_and_line' : None}), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 233 | (SyntaxError, ('msgStr',), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 234 | {'args' : ('msgStr',), 'text' : None, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 235 | 'print_file_and_line' : None, 'msg' : 'msgStr', |
| 236 | 'filename' : None, 'lineno' : None, 'offset' : None}), |
| 237 | (SyntaxError, ('msgStr', ('filenameStr', 'linenoStr', 'offsetStr', |
| 238 | 'textStr')), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 239 | {'offset' : 'offsetStr', 'text' : 'textStr', |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 240 | 'args' : ('msgStr', ('filenameStr', 'linenoStr', |
| 241 | 'offsetStr', 'textStr')), |
| 242 | 'print_file_and_line' : None, 'msg' : 'msgStr', |
| 243 | 'filename' : 'filenameStr', 'lineno' : 'linenoStr'}), |
| 244 | (SyntaxError, ('msgStr', 'filenameStr', 'linenoStr', 'offsetStr', |
| 245 | 'textStr', 'print_file_and_lineStr'), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 246 | {'text' : None, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 247 | 'args' : ('msgStr', 'filenameStr', 'linenoStr', 'offsetStr', |
| 248 | 'textStr', 'print_file_and_lineStr'), |
| 249 | 'print_file_and_line' : None, 'msg' : 'msgStr', |
| 250 | 'filename' : None, 'lineno' : None, 'offset' : None}), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 251 | (UnicodeError, (), {'args' : (),}), |
Walter Dörwald | eceb0fb | 2007-05-24 17:49:56 +0000 | [diff] [blame] | 252 | (UnicodeEncodeError, ('ascii', 'a', 0, 1, |
| 253 | 'ordinal not in range'), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 254 | {'args' : ('ascii', 'a', 0, 1, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 255 | 'ordinal not in range'), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 256 | 'encoding' : 'ascii', 'object' : 'a', |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 257 | 'start' : 0, 'reason' : 'ordinal not in range'}), |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 258 | (UnicodeDecodeError, ('ascii', bytearray(b'\xff'), 0, 1, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 259 | 'ordinal not in range'), |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 260 | {'args' : ('ascii', bytearray(b'\xff'), 0, 1, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 261 | 'ordinal not in range'), |
| 262 | 'encoding' : 'ascii', 'object' : b'\xff', |
| 263 | 'start' : 0, 'reason' : 'ordinal not in range'}), |
Walter Dörwald | eceb0fb | 2007-05-24 17:49:56 +0000 | [diff] [blame] | 264 | (UnicodeDecodeError, ('ascii', b'\xff', 0, 1, |
| 265 | 'ordinal not in range'), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 266 | {'args' : ('ascii', b'\xff', 0, 1, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 267 | 'ordinal not in range'), |
Guido van Rossum | b8142c3 | 2007-05-08 17:49:10 +0000 | [diff] [blame] | 268 | 'encoding' : 'ascii', 'object' : b'\xff', |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 269 | 'start' : 0, 'reason' : 'ordinal not in range'}), |
Walter Dörwald | eceb0fb | 2007-05-24 17:49:56 +0000 | [diff] [blame] | 270 | (UnicodeTranslateError, ("\u3042", 0, 1, "ouch"), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 271 | {'args' : ('\u3042', 0, 1, 'ouch'), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 272 | 'object' : '\u3042', 'reason' : 'ouch', |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 273 | 'start' : 0, 'end' : 1}), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 274 | ] |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 275 | try: |
| 276 | exceptionList.append( |
| 277 | (WindowsError, (1, 'strErrorStr', 'filenameStr'), |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 278 | {'args' : (1, 'strErrorStr'), |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 279 | 'strerror' : 'strErrorStr', 'winerror' : 1, |
| 280 | 'errno' : 22, 'filename' : 'filenameStr'}) |
| 281 | ) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 282 | except NameError: |
| 283 | pass |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 284 | |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 285 | for exc, args, expected in exceptionList: |
| 286 | try: |
| 287 | e = exc(*args) |
| 288 | except: |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 289 | print("\nexc=%r, args=%r" % (exc, args), file=sys.stderr) |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 290 | raise |
| 291 | else: |
| 292 | # Verify module name |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 293 | self.assertEqual(type(e).__module__, 'builtins') |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 294 | # Verify no ref leaks in Exc_str() |
| 295 | s = str(e) |
| 296 | for checkArgName in expected: |
| 297 | value = getattr(e, checkArgName) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 298 | self.assertEqual(repr(value), |
| 299 | repr(expected[checkArgName]), |
| 300 | '%r.%s == %r, expected %r' % ( |
| 301 | e, checkArgName, |
| 302 | value, expected[checkArgName])) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 303 | |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 304 | # test for pickling support |
Guido van Rossum | 99603b0 | 2007-07-20 00:22:32 +0000 | [diff] [blame] | 305 | for p in [pickle]: |
Guido van Rossum | ebe3e16 | 2007-05-17 18:20:34 +0000 | [diff] [blame] | 306 | for protocol in range(p.HIGHEST_PROTOCOL + 1): |
| 307 | s = p.dumps(e, protocol) |
| 308 | new = p.loads(s) |
| 309 | for checkArgName in expected: |
| 310 | got = repr(getattr(new, checkArgName)) |
| 311 | want = repr(expected[checkArgName]) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 312 | self.assertEqual(got, want, |
| 313 | 'pickled "%r", attribute "%s' % |
| 314 | (e, checkArgName)) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 315 | |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 316 | def testWithTraceback(self): |
| 317 | try: |
| 318 | raise IndexError(4) |
| 319 | except: |
| 320 | tb = sys.exc_info()[2] |
| 321 | |
| 322 | e = BaseException().with_traceback(tb) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 323 | self.assertIsInstance(e, BaseException) |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 324 | self.assertEqual(e.__traceback__, tb) |
| 325 | |
| 326 | e = IndexError(5).with_traceback(tb) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 327 | self.assertIsInstance(e, IndexError) |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 328 | self.assertEqual(e.__traceback__, tb) |
| 329 | |
| 330 | class MyException(Exception): |
| 331 | pass |
| 332 | |
| 333 | e = MyException().with_traceback(tb) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 334 | self.assertIsInstance(e, MyException) |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 335 | self.assertEqual(e.__traceback__, tb) |
| 336 | |
| 337 | def testInvalidTraceback(self): |
| 338 | try: |
| 339 | Exception().__traceback__ = 5 |
| 340 | except TypeError as e: |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 341 | self.assertIn("__traceback__ must be a traceback", str(e)) |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 342 | else: |
| 343 | self.fail("No exception raised") |
| 344 | |
Georg Brandl | ab6f2f6 | 2009-03-31 04:16:10 +0000 | [diff] [blame] | 345 | def testInvalidAttrs(self): |
| 346 | self.assertRaises(TypeError, setattr, Exception(), '__cause__', 1) |
| 347 | self.assertRaises(TypeError, delattr, Exception(), '__cause__') |
| 348 | self.assertRaises(TypeError, setattr, Exception(), '__context__', 1) |
| 349 | self.assertRaises(TypeError, delattr, Exception(), '__context__') |
| 350 | |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 351 | def testNoneClearsTracebackAttr(self): |
| 352 | try: |
| 353 | raise IndexError(4) |
| 354 | except: |
| 355 | tb = sys.exc_info()[2] |
| 356 | |
| 357 | e = Exception() |
| 358 | e.__traceback__ = tb |
| 359 | e.__traceback__ = None |
| 360 | self.assertEqual(e.__traceback__, None) |
| 361 | |
| 362 | def testChainingAttrs(self): |
| 363 | e = Exception() |
| 364 | self.assertEqual(e.__context__, None) |
| 365 | self.assertEqual(e.__cause__, None) |
| 366 | |
| 367 | e = TypeError() |
| 368 | self.assertEqual(e.__context__, None) |
| 369 | self.assertEqual(e.__cause__, None) |
| 370 | |
| 371 | class MyException(EnvironmentError): |
| 372 | pass |
| 373 | |
| 374 | e = MyException() |
| 375 | self.assertEqual(e.__context__, None) |
| 376 | self.assertEqual(e.__cause__, None) |
| 377 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 378 | def testKeywordArgs(self): |
| 379 | # test that builtin exception don't take keyword args, |
| 380 | # but user-defined subclasses can if they want |
| 381 | self.assertRaises(TypeError, BaseException, a=1) |
| 382 | |
| 383 | class DerivedException(BaseException): |
| 384 | def __init__(self, fancy_arg): |
| 385 | BaseException.__init__(self) |
| 386 | self.fancy_arg = fancy_arg |
| 387 | |
| 388 | x = DerivedException(fancy_arg=42) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 389 | self.assertEqual(x.fancy_arg, 42) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 390 | |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 391 | @no_tracing |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 392 | def testInfiniteRecursion(self): |
| 393 | def f(): |
| 394 | return f() |
| 395 | self.assertRaises(RuntimeError, f) |
| 396 | |
| 397 | def g(): |
| 398 | try: |
| 399 | return g() |
| 400 | except ValueError: |
| 401 | return -1 |
| 402 | self.assertRaises(RuntimeError, g) |
| 403 | |
Ezio Melotti | 2f5a78c | 2009-12-24 22:54:06 +0000 | [diff] [blame] | 404 | def test_str(self): |
| 405 | # Make sure both instances and classes have a str representation. |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 406 | self.assertTrue(str(Exception)) |
| 407 | self.assertTrue(str(Exception('a'))) |
Ezio Melotti | 2f5a78c | 2009-12-24 22:54:06 +0000 | [diff] [blame] | 408 | self.assertTrue(str(Exception('a', 'b'))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 409 | |
Barry Warsaw | 8d109cb | 2008-05-08 04:26:35 +0000 | [diff] [blame] | 410 | def testExceptionCleanupNames(self): |
| 411 | # Make sure the local variable bound to the exception instance by |
| 412 | # an "except" statement is only visible inside the except block. |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 413 | try: |
| 414 | raise Exception() |
| 415 | except Exception as e: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 416 | self.assertTrue(e) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 417 | del e |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 418 | self.assertNotIn('e', locals()) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 419 | |
Barry Warsaw | 8d109cb | 2008-05-08 04:26:35 +0000 | [diff] [blame] | 420 | def testExceptionCleanupState(self): |
| 421 | # Make sure exception state is cleaned up as soon as the except |
| 422 | # block is left. See #2507 |
| 423 | |
| 424 | class MyException(Exception): |
| 425 | def __init__(self, obj): |
| 426 | self.obj = obj |
| 427 | class MyObj: |
| 428 | pass |
| 429 | |
| 430 | def inner_raising_func(): |
| 431 | # Create some references in exception value and traceback |
| 432 | local_ref = obj |
| 433 | raise MyException(obj) |
| 434 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 435 | # Qualified "except" with "as" |
Barry Warsaw | 8d109cb | 2008-05-08 04:26:35 +0000 | [diff] [blame] | 436 | obj = MyObj() |
| 437 | wr = weakref.ref(obj) |
| 438 | try: |
| 439 | inner_raising_func() |
| 440 | except MyException as e: |
| 441 | pass |
| 442 | obj = None |
| 443 | obj = wr() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 444 | self.assertTrue(obj is None, "%s" % obj) |
Barry Warsaw | 8d109cb | 2008-05-08 04:26:35 +0000 | [diff] [blame] | 445 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 446 | # Qualified "except" without "as" |
| 447 | obj = MyObj() |
| 448 | wr = weakref.ref(obj) |
| 449 | try: |
| 450 | inner_raising_func() |
| 451 | except MyException: |
| 452 | pass |
| 453 | obj = None |
| 454 | obj = wr() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 455 | self.assertTrue(obj is None, "%s" % obj) |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 456 | |
| 457 | # Bare "except" |
| 458 | obj = MyObj() |
| 459 | wr = weakref.ref(obj) |
| 460 | try: |
| 461 | inner_raising_func() |
| 462 | except: |
| 463 | pass |
| 464 | obj = None |
| 465 | obj = wr() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 466 | self.assertTrue(obj is None, "%s" % obj) |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 467 | |
| 468 | # "except" with premature block leave |
| 469 | obj = MyObj() |
| 470 | wr = weakref.ref(obj) |
| 471 | for i in [0]: |
| 472 | try: |
| 473 | inner_raising_func() |
| 474 | except: |
| 475 | break |
| 476 | obj = None |
| 477 | obj = wr() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 478 | self.assertTrue(obj is None, "%s" % obj) |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 479 | |
| 480 | # "except" block raising another exception |
| 481 | obj = MyObj() |
| 482 | wr = weakref.ref(obj) |
| 483 | try: |
| 484 | try: |
| 485 | inner_raising_func() |
| 486 | except: |
| 487 | raise KeyError |
Guido van Rossum | b4fb6e4 | 2008-06-14 20:20:24 +0000 | [diff] [blame] | 488 | except KeyError as e: |
| 489 | # We want to test that the except block above got rid of |
| 490 | # the exception raised in inner_raising_func(), but it |
| 491 | # also ends up in the __context__ of the KeyError, so we |
| 492 | # must clear the latter manually for our test to succeed. |
| 493 | e.__context__ = None |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 494 | obj = None |
| 495 | obj = wr() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 496 | self.assertTrue(obj is None, "%s" % obj) |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 497 | |
| 498 | # Some complicated construct |
| 499 | obj = MyObj() |
| 500 | wr = weakref.ref(obj) |
| 501 | try: |
| 502 | inner_raising_func() |
| 503 | except MyException: |
| 504 | try: |
| 505 | try: |
| 506 | raise |
| 507 | finally: |
| 508 | raise |
| 509 | except MyException: |
| 510 | pass |
| 511 | obj = None |
| 512 | obj = wr() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 513 | self.assertTrue(obj is None, "%s" % obj) |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 514 | |
| 515 | # Inside an exception-silencing "with" block |
| 516 | class Context: |
| 517 | def __enter__(self): |
| 518 | return self |
| 519 | def __exit__ (self, exc_type, exc_value, exc_tb): |
| 520 | return True |
| 521 | obj = MyObj() |
| 522 | wr = weakref.ref(obj) |
| 523 | with Context(): |
| 524 | inner_raising_func() |
| 525 | obj = None |
| 526 | obj = wr() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 527 | self.assertTrue(obj is None, "%s" % obj) |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 528 | |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 529 | def test_exception_target_in_nested_scope(self): |
| 530 | # issue 4617: This used to raise a SyntaxError |
| 531 | # "can not delete variable 'e' referenced in nested scope" |
| 532 | def print_error(): |
| 533 | e |
| 534 | try: |
| 535 | something |
| 536 | except Exception as e: |
| 537 | print_error() |
| 538 | # implicit "del e" here |
| 539 | |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 540 | def test_generator_leaking(self): |
| 541 | # Test that generator exception state doesn't leak into the calling |
| 542 | # frame |
| 543 | def yield_raise(): |
| 544 | try: |
| 545 | raise KeyError("caught") |
| 546 | except KeyError: |
| 547 | yield sys.exc_info()[0] |
| 548 | yield sys.exc_info()[0] |
| 549 | yield sys.exc_info()[0] |
| 550 | g = yield_raise() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 551 | self.assertEqual(next(g), KeyError) |
| 552 | self.assertEqual(sys.exc_info()[0], None) |
| 553 | self.assertEqual(next(g), KeyError) |
| 554 | self.assertEqual(sys.exc_info()[0], None) |
| 555 | self.assertEqual(next(g), None) |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 556 | |
| 557 | # Same test, but inside an exception handler |
| 558 | try: |
| 559 | raise TypeError("foo") |
| 560 | except TypeError: |
| 561 | g = yield_raise() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 562 | self.assertEqual(next(g), KeyError) |
| 563 | self.assertEqual(sys.exc_info()[0], TypeError) |
| 564 | self.assertEqual(next(g), KeyError) |
| 565 | self.assertEqual(sys.exc_info()[0], TypeError) |
| 566 | self.assertEqual(next(g), TypeError) |
Benjamin Peterson | eec3d71 | 2008-06-11 15:59:43 +0000 | [diff] [blame] | 567 | del g |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 568 | self.assertEqual(sys.exc_info()[0], TypeError) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 569 | |
Benjamin Peterson | ae5f2f4 | 2010-03-07 17:10:51 +0000 | [diff] [blame] | 570 | def test_generator_finalizing_and_exc_info(self): |
| 571 | # See #7173 |
| 572 | def simple_gen(): |
| 573 | yield 1 |
| 574 | def run_gen(): |
| 575 | gen = simple_gen() |
| 576 | try: |
| 577 | raise RuntimeError |
| 578 | except RuntimeError: |
| 579 | return next(gen) |
| 580 | run_gen() |
| 581 | gc_collect() |
| 582 | self.assertEqual(sys.exc_info(), (None, None, None)) |
| 583 | |
Benjamin Peterson | 27d6367 | 2008-06-15 20:09:12 +0000 | [diff] [blame] | 584 | def test_3114(self): |
| 585 | # Bug #3114: in its destructor, MyObject retrieves a pointer to |
| 586 | # obsolete and/or deallocated objects. |
Benjamin Peterson | 979f311 | 2008-06-15 00:05:44 +0000 | [diff] [blame] | 587 | class MyObject: |
| 588 | def __del__(self): |
| 589 | nonlocal e |
| 590 | e = sys.exc_info() |
| 591 | e = () |
| 592 | try: |
| 593 | raise Exception(MyObject()) |
| 594 | except: |
| 595 | pass |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 596 | self.assertEqual(e, (None, None, None)) |
Benjamin Peterson | 979f311 | 2008-06-15 00:05:44 +0000 | [diff] [blame] | 597 | |
Eric Smith | 0facd77 | 2010-02-24 15:42:29 +0000 | [diff] [blame] | 598 | def testUnicodeChangeAttributes(self): |
| 599 | # See issue 7309. This was a crasher. |
| 600 | |
| 601 | u = UnicodeEncodeError('baz', 'xxxxx', 1, 5, 'foo') |
| 602 | self.assertEqual(str(u), "'baz' codec can't encode characters in position 1-4: foo") |
| 603 | u.end = 2 |
| 604 | self.assertEqual(str(u), "'baz' codec can't encode character '\\x78' in position 1: foo") |
| 605 | u.end = 5 |
| 606 | u.reason = 0x345345345345345345 |
| 607 | self.assertEqual(str(u), "'baz' codec can't encode characters in position 1-4: 965230951443685724997") |
| 608 | u.encoding = 4000 |
| 609 | self.assertEqual(str(u), "'4000' codec can't encode characters in position 1-4: 965230951443685724997") |
| 610 | u.start = 1000 |
| 611 | self.assertEqual(str(u), "'4000' codec can't encode characters in position 1000-4: 965230951443685724997") |
| 612 | |
| 613 | u = UnicodeDecodeError('baz', b'xxxxx', 1, 5, 'foo') |
| 614 | self.assertEqual(str(u), "'baz' codec can't decode bytes in position 1-4: foo") |
| 615 | u.end = 2 |
| 616 | self.assertEqual(str(u), "'baz' codec can't decode byte 0x78 in position 1: foo") |
| 617 | u.end = 5 |
| 618 | u.reason = 0x345345345345345345 |
| 619 | self.assertEqual(str(u), "'baz' codec can't decode bytes in position 1-4: 965230951443685724997") |
| 620 | u.encoding = 4000 |
| 621 | self.assertEqual(str(u), "'4000' codec can't decode bytes in position 1-4: 965230951443685724997") |
| 622 | u.start = 1000 |
| 623 | self.assertEqual(str(u), "'4000' codec can't decode bytes in position 1000-4: 965230951443685724997") |
| 624 | |
| 625 | u = UnicodeTranslateError('xxxx', 1, 5, 'foo') |
| 626 | self.assertEqual(str(u), "can't translate characters in position 1-4: foo") |
| 627 | u.end = 2 |
| 628 | self.assertEqual(str(u), "can't translate character '\\x78' in position 1: foo") |
| 629 | u.end = 5 |
| 630 | u.reason = 0x345345345345345345 |
| 631 | self.assertEqual(str(u), "can't translate characters in position 1-4: 965230951443685724997") |
| 632 | u.start = 1000 |
| 633 | self.assertEqual(str(u), "can't translate characters in position 1000-4: 965230951443685724997") |
Benjamin Peterson | 6e7740c | 2008-08-20 23:23:34 +0000 | [diff] [blame] | 634 | |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 635 | @no_tracing |
Benjamin Peterson | 69c88f7 | 2008-07-31 01:47:08 +0000 | [diff] [blame] | 636 | def test_badisinstance(self): |
| 637 | # Bug #2542: if issubclass(e, MyException) raises an exception, |
| 638 | # it should be ignored |
| 639 | class Meta(type): |
| 640 | def __subclasscheck__(cls, subclass): |
| 641 | raise ValueError() |
| 642 | class MyException(Exception, metaclass=Meta): |
| 643 | pass |
| 644 | |
| 645 | with captured_output("stderr") as stderr: |
| 646 | try: |
| 647 | raise KeyError() |
| 648 | except MyException as e: |
| 649 | self.fail("exception should not be a MyException") |
| 650 | except KeyError: |
| 651 | pass |
| 652 | except: |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 653 | self.fail("Should have raised KeyError") |
Benjamin Peterson | 69c88f7 | 2008-07-31 01:47:08 +0000 | [diff] [blame] | 654 | else: |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 655 | self.fail("Should have raised KeyError") |
| 656 | |
| 657 | def g(): |
| 658 | try: |
| 659 | return g() |
| 660 | except RuntimeError: |
| 661 | return sys.exc_info() |
| 662 | e, v, tb = g() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 663 | self.assertTrue(isinstance(v, RuntimeError), type(v)) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 664 | self.assertIn("maximum recursion depth exceeded", str(v)) |
Benjamin Peterson | 69c88f7 | 2008-07-31 01:47:08 +0000 | [diff] [blame] | 665 | |
Amaury Forgeot d'Arc | e19cadb | 2008-07-31 22:56:02 +0000 | [diff] [blame] | 666 | |
| 667 | def test_MemoryError(self): |
| 668 | # PyErr_NoMemory always raises the same exception instance. |
| 669 | # Check that the traceback is not doubled. |
| 670 | import traceback |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 671 | from _testcapi import raise_memoryerror |
Amaury Forgeot d'Arc | e19cadb | 2008-07-31 22:56:02 +0000 | [diff] [blame] | 672 | def raiseMemError(): |
| 673 | try: |
Benjamin Peterson | 0067bd6 | 2008-08-16 16:11:03 +0000 | [diff] [blame] | 674 | raise_memoryerror() |
Amaury Forgeot d'Arc | e19cadb | 2008-07-31 22:56:02 +0000 | [diff] [blame] | 675 | except MemoryError as e: |
| 676 | tb = e.__traceback__ |
| 677 | else: |
| 678 | self.fail("Should have raises a MemoryError") |
| 679 | return traceback.format_tb(tb) |
| 680 | |
| 681 | tb1 = raiseMemError() |
| 682 | tb2 = raiseMemError() |
| 683 | self.assertEqual(tb1, tb2) |
| 684 | |
Benjamin Peterson | 17e0bbc | 2010-06-28 15:39:55 +0000 | [diff] [blame] | 685 | @cpython_only |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 686 | def test_exception_with_doc(self): |
| 687 | import _testcapi |
| 688 | doc2 = "This is a test docstring." |
| 689 | doc4 = "This is another test docstring." |
| 690 | |
| 691 | self.assertRaises(SystemError, _testcapi.make_exception_with_doc, |
| 692 | "error1") |
| 693 | |
| 694 | # test basic usage of PyErr_NewException |
| 695 | error1 = _testcapi.make_exception_with_doc("_testcapi.error1") |
| 696 | self.assertIs(type(error1), type) |
| 697 | self.assertTrue(issubclass(error1, Exception)) |
| 698 | self.assertIsNone(error1.__doc__) |
| 699 | |
| 700 | # test with given docstring |
| 701 | error2 = _testcapi.make_exception_with_doc("_testcapi.error2", doc2) |
| 702 | self.assertEqual(error2.__doc__, doc2) |
| 703 | |
| 704 | # test with explicit base (without docstring) |
| 705 | error3 = _testcapi.make_exception_with_doc("_testcapi.error3", |
| 706 | base=error2) |
| 707 | self.assertTrue(issubclass(error3, error2)) |
| 708 | |
| 709 | # test with explicit base tuple |
| 710 | class C(object): |
| 711 | pass |
| 712 | error4 = _testcapi.make_exception_with_doc("_testcapi.error4", doc4, |
| 713 | (error3, C)) |
| 714 | self.assertTrue(issubclass(error4, error3)) |
| 715 | self.assertTrue(issubclass(error4, C)) |
| 716 | self.assertEqual(error4.__doc__, doc4) |
| 717 | |
| 718 | # test with explicit dictionary |
| 719 | error5 = _testcapi.make_exception_with_doc("_testcapi.error5", "", |
| 720 | error4, {'a': 1}) |
| 721 | self.assertTrue(issubclass(error5, error4)) |
| 722 | self.assertEqual(error5.a, 1) |
| 723 | self.assertEqual(error5.__doc__, "") |
| 724 | |
Antoine Pitrou | 07e20ef | 2010-10-28 22:56:58 +0000 | [diff] [blame] | 725 | def test_memory_error_cleanup(self): |
| 726 | # Issue #5437: preallocated MemoryError instances should not keep |
| 727 | # traceback objects alive. |
| 728 | from _testcapi import raise_memoryerror |
| 729 | class C: |
| 730 | pass |
| 731 | wr = None |
| 732 | def inner(): |
| 733 | nonlocal wr |
| 734 | c = C() |
| 735 | wr = weakref.ref(c) |
| 736 | raise_memoryerror() |
| 737 | # We cannot use assertRaises since it manually deletes the traceback |
| 738 | try: |
| 739 | inner() |
| 740 | except MemoryError as e: |
| 741 | self.assertNotEqual(wr(), None) |
| 742 | else: |
| 743 | self.fail("MemoryError not raised") |
| 744 | self.assertEqual(wr(), None) |
| 745 | |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 746 | @no_tracing |
Antoine Pitrou | 07e20ef | 2010-10-28 22:56:58 +0000 | [diff] [blame] | 747 | def test_recursion_error_cleanup(self): |
| 748 | # Same test as above, but with "recursion exceeded" errors |
| 749 | class C: |
| 750 | pass |
| 751 | wr = None |
| 752 | def inner(): |
| 753 | nonlocal wr |
| 754 | c = C() |
| 755 | wr = weakref.ref(c) |
| 756 | inner() |
| 757 | # We cannot use assertRaises since it manually deletes the traceback |
| 758 | try: |
| 759 | inner() |
| 760 | except RuntimeError as e: |
| 761 | self.assertNotEqual(wr(), None) |
| 762 | else: |
| 763 | self.fail("RuntimeError not raised") |
| 764 | self.assertEqual(wr(), None) |
Georg Brandl | 1e28a27 | 2009-12-28 08:41:01 +0000 | [diff] [blame] | 765 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 766 | def test_main(): |
| 767 | run_unittest(ExceptionTests) |
| 768 | |
| 769 | if __name__ == '__main__': |
Guido van Rossum | b8142c3 | 2007-05-08 17:49:10 +0000 | [diff] [blame] | 770 | unittest.main() |