Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1 | # Python test set -- part 5, built-in exceptions |
| 2 | |
Barry Warsaw | 408b6d3 | 2002-07-30 23:27:12 +0000 | [diff] [blame] | 3 | from test.test_support import TestFailed, TESTFN, unlink |
Barry Warsaw | 6ed41a0 | 1997-08-29 21:58:25 +0000 | [diff] [blame] | 4 | from types import ClassType |
Guido van Rossum | 83b120d | 2001-08-23 03:23:03 +0000 | [diff] [blame] | 5 | import warnings |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 6 | import sys, traceback, os |
Guido van Rossum | 83b120d | 2001-08-23 03:23:03 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 8 | print '5. Built-in exceptions' |
| 9 | # XXX This is not really enough, each *operation* should be tested! |
| 10 | |
Barry Warsaw | b9c1d3d | 2001-08-13 23:07:00 +0000 | [diff] [blame] | 11 | # Reloading the built-in exceptions module failed prior to Py2.2, while it |
| 12 | # should act the same as reloading built-in sys. |
| 13 | try: |
| 14 | import exceptions |
| 15 | reload(exceptions) |
| 16 | except ImportError, e: |
| 17 | raise TestFailed, e |
| 18 | |
Jeremy Hylton | 56c807d | 2000-06-20 18:52:57 +0000 | [diff] [blame] | 19 | def test_raise_catch(exc): |
| 20 | try: |
| 21 | raise exc, "spam" |
| 22 | except exc, err: |
| 23 | buf = str(err) |
| 24 | try: |
| 25 | raise exc("spam") |
| 26 | except exc, err: |
| 27 | buf = str(err) |
| 28 | print buf |
| 29 | |
Barry Warsaw | 6ed41a0 | 1997-08-29 21:58:25 +0000 | [diff] [blame] | 30 | def r(thing): |
Jeremy Hylton | 56c807d | 2000-06-20 18:52:57 +0000 | [diff] [blame] | 31 | test_raise_catch(thing) |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 32 | print getattr(thing, '__name__', thing) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 33 | |
| 34 | r(AttributeError) |
| 35 | import sys |
| 36 | try: x = sys.undefined_attribute |
| 37 | except AttributeError: pass |
| 38 | |
| 39 | r(EOFError) |
| 40 | import sys |
| 41 | fp = open(TESTFN, 'w') |
| 42 | fp.close() |
| 43 | fp = open(TESTFN, 'r') |
| 44 | savestdin = sys.stdin |
| 45 | try: |
Fred Drake | 2e6d25c | 2000-10-23 17:00:30 +0000 | [diff] [blame] | 46 | try: |
Neal Norwitz | ce96f69 | 2006-03-17 06:49:51 +0000 | [diff] [blame] | 47 | import marshal |
| 48 | marshal.loads('') |
Fred Drake | 2e6d25c | 2000-10-23 17:00:30 +0000 | [diff] [blame] | 49 | except EOFError: |
| 50 | pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 51 | finally: |
Fred Drake | 2e6d25c | 2000-10-23 17:00:30 +0000 | [diff] [blame] | 52 | sys.stdin = savestdin |
| 53 | fp.close() |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 54 | |
| 55 | r(IOError) |
| 56 | try: open('this file does not exist', 'r') |
| 57 | except IOError: pass |
| 58 | |
| 59 | r(ImportError) |
| 60 | try: import undefined_module |
| 61 | except ImportError: pass |
| 62 | |
| 63 | r(IndexError) |
| 64 | x = [] |
| 65 | try: a = x[10] |
| 66 | except IndexError: pass |
| 67 | |
| 68 | r(KeyError) |
| 69 | x = {} |
| 70 | try: a = x['key'] |
| 71 | except KeyError: pass |
| 72 | |
| 73 | r(KeyboardInterrupt) |
| 74 | print '(not testable in a script)' |
| 75 | |
| 76 | r(MemoryError) |
| 77 | print '(not safe to test)' |
| 78 | |
| 79 | r(NameError) |
| 80 | try: x = undefined_variable |
| 81 | except NameError: pass |
| 82 | |
| 83 | r(OverflowError) |
| 84 | x = 1 |
Tim Peters | c885443 | 2004-08-25 02:14:08 +0000 | [diff] [blame] | 85 | for dummy in range(128): |
| 86 | x += x # this simply shouldn't blow up |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 87 | |
| 88 | r(RuntimeError) |
| 89 | print '(not used any more?)' |
| 90 | |
| 91 | r(SyntaxError) |
Guido van Rossum | e2cb727 | 1995-08-11 14:24:47 +0000 | [diff] [blame] | 92 | try: exec '/\n' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 93 | except SyntaxError: pass |
| 94 | |
Fred Drake | 72e48bd | 2000-09-08 16:32:34 +0000 | [diff] [blame] | 95 | # make sure the right exception message is raised for each of these |
| 96 | # code fragments: |
| 97 | |
| 98 | def ckmsg(src, msg): |
| 99 | try: |
| 100 | compile(src, '<fragment>', 'exec') |
| 101 | except SyntaxError, e: |
| 102 | print e.msg |
| 103 | if e.msg == msg: |
| 104 | print "ok" |
| 105 | else: |
| 106 | print "expected:", msg |
| 107 | else: |
| 108 | print "failed to get expected SyntaxError" |
| 109 | |
| 110 | s = '''\ |
| 111 | while 1: |
| 112 | try: |
Fred Drake | 72e48bd | 2000-09-08 16:32:34 +0000 | [diff] [blame] | 113 | pass |
Fred Drake | 72e48bd | 2000-09-08 16:32:34 +0000 | [diff] [blame] | 114 | finally: |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 115 | continue |
Fred Drake | 72e48bd | 2000-09-08 16:32:34 +0000 | [diff] [blame] | 116 | ''' |
Finn Bock | aa3dc45 | 2001-12-08 10:15:48 +0000 | [diff] [blame] | 117 | if sys.platform.startswith('java'): |
| 118 | print "'continue' not supported inside 'finally' clause" |
| 119 | print "ok" |
| 120 | else: |
| 121 | ckmsg(s, "'continue' not supported inside 'finally' clause") |
Fred Drake | 72e48bd | 2000-09-08 16:32:34 +0000 | [diff] [blame] | 122 | s = '''\ |
| 123 | try: |
| 124 | continue |
| 125 | except: |
| 126 | pass |
| 127 | ''' |
| 128 | ckmsg(s, "'continue' not properly in loop") |
| 129 | ckmsg("continue\n", "'continue' not properly in loop") |
| 130 | |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 131 | r(IndentationError) |
| 132 | |
| 133 | r(TabError) |
| 134 | # can only be tested under -tt, and is the only test for -tt |
| 135 | #try: compile("try:\n\t1/0\n \t1/0\nfinally:\n pass\n", '<string>', 'exec') |
| 136 | #except TabError: pass |
| 137 | #else: raise TestFailed |
| 138 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 139 | r(SystemError) |
| 140 | print '(hard to reproduce)' |
| 141 | |
| 142 | r(SystemExit) |
| 143 | import sys |
| 144 | try: sys.exit(0) |
| 145 | except SystemExit: pass |
| 146 | |
| 147 | r(TypeError) |
| 148 | try: [] + () |
| 149 | except TypeError: pass |
| 150 | |
| 151 | r(ValueError) |
| 152 | try: x = chr(10000) |
| 153 | except ValueError: pass |
| 154 | |
| 155 | r(ZeroDivisionError) |
| 156 | try: x = 1/0 |
| 157 | except ZeroDivisionError: pass |
| 158 | |
Barry Warsaw | 992cb8a | 2000-05-25 23:16:54 +0000 | [diff] [blame] | 159 | r(Exception) |
| 160 | try: x = 1/0 |
| 161 | except Exception, e: pass |
| 162 | |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 163 | # test that setting an exception at the C level works even if the |
| 164 | # exception object can't be constructed. |
| 165 | |
Thomas Wouters | 303de6a | 2006-04-20 22:42:37 +0000 | [diff] [blame] | 166 | class BadException(Exception): |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 167 | def __init__(self): |
| 168 | raise RuntimeError, "can't instantiate BadException" |
| 169 | |
Thomas Wouters | 303de6a | 2006-04-20 22:42:37 +0000 | [diff] [blame] | 170 | # Exceptions must inherit from BaseException, raising invalid exception |
| 171 | # should instead raise SystemError |
| 172 | class InvalidException: |
| 173 | pass |
| 174 | |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 175 | def test_capi1(): |
Finn Bock | aa3dc45 | 2001-12-08 10:15:48 +0000 | [diff] [blame] | 176 | import _testcapi |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 177 | try: |
| 178 | _testcapi.raise_exception(BadException, 1) |
| 179 | except TypeError, err: |
| 180 | exc, err, tb = sys.exc_info() |
| 181 | co = tb.tb_frame.f_code |
| 182 | assert co.co_name == "test_capi1" |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 183 | assert co.co_filename.endswith('test_exceptions'+os.extsep+'py') |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 184 | else: |
| 185 | print "Expected exception" |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 186 | |
| 187 | def test_capi2(): |
Finn Bock | aa3dc45 | 2001-12-08 10:15:48 +0000 | [diff] [blame] | 188 | import _testcapi |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 189 | try: |
| 190 | _testcapi.raise_exception(BadException, 0) |
| 191 | except RuntimeError, err: |
| 192 | exc, err, tb = sys.exc_info() |
| 193 | co = tb.tb_frame.f_code |
| 194 | assert co.co_name == "__init__" |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 195 | assert co.co_filename.endswith('test_exceptions'+os.extsep+'py') |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 196 | co2 = tb.tb_frame.f_back.f_code |
| 197 | assert co2.co_name == "test_capi2" |
| 198 | else: |
| 199 | print "Expected exception" |
Finn Bock | aa3dc45 | 2001-12-08 10:15:48 +0000 | [diff] [blame] | 200 | |
Thomas Wouters | 303de6a | 2006-04-20 22:42:37 +0000 | [diff] [blame] | 201 | def test_capi3(): |
| 202 | import _testcapi |
| 203 | try: |
| 204 | _testcapi.raise_exception(InvalidException, 1) |
| 205 | except SystemError: |
| 206 | pass |
| 207 | except InvalidException: |
| 208 | raise AssertionError("Managed to raise InvalidException"); |
| 209 | else: |
| 210 | print "Expected SystemError exception" |
| 211 | |
| 212 | |
Finn Bock | aa3dc45 | 2001-12-08 10:15:48 +0000 | [diff] [blame] | 213 | if not sys.platform.startswith('java'): |
| 214 | test_capi1() |
| 215 | test_capi2() |
Thomas Wouters | 303de6a | 2006-04-20 22:42:37 +0000 | [diff] [blame] | 216 | test_capi3() |
Jeremy Hylton | ede049b | 2001-09-26 20:01:13 +0000 | [diff] [blame] | 217 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 218 | unlink(TESTFN) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 219 | |
| 220 | # test that exception attributes are happy. |
| 221 | try: str(u'Hello \u00E1') |
| 222 | except Exception, e: sampleUnicodeEncodeError = e |
| 223 | try: unicode('\xff') |
| 224 | except Exception, e: sampleUnicodeDecodeError = e |
| 225 | exceptionList = [ |
| 226 | ( BaseException, (), { 'message' : '', 'args' : () }), |
| 227 | ( BaseException, (1, ), { 'message' : 1, 'args' : ( 1, ) }), |
| 228 | ( BaseException, ('foo', ), { 'message' : 'foo', 'args' : ( 'foo', ) }), |
| 229 | ( BaseException, ('foo', 1), { 'message' : '', 'args' : ( 'foo', 1 ) }), |
| 230 | ( SystemExit, ('foo',), { 'message' : 'foo', 'args' : ( 'foo', ), |
| 231 | 'code' : 'foo' }), |
| 232 | ( IOError, ('foo',), { 'message' : 'foo', 'args' : ( 'foo', ), }), |
| 233 | ( IOError, ('foo', 'bar'), { 'message' : '', |
| 234 | 'args' : ('foo', 'bar'), }), |
| 235 | ( IOError, ('foo', 'bar', 'baz'), |
| 236 | { 'message' : '', 'args' : ('foo', 'bar'), }), |
| 237 | ( EnvironmentError, ('errnoStr', 'strErrorStr', 'filenameStr'), |
| 238 | { 'message' : '', 'args' : ('errnoStr', 'strErrorStr'), |
| 239 | 'strerror' : 'strErrorStr', |
| 240 | 'errno' : 'errnoStr', 'filename' : 'filenameStr' }), |
| 241 | ( EnvironmentError, (1, 'strErrorStr', 'filenameStr'), |
| 242 | { 'message' : '', 'args' : (1, 'strErrorStr'), |
| 243 | 'strerror' : 'strErrorStr', 'errno' : 1, |
| 244 | 'filename' : 'filenameStr' }), |
| 245 | ( SyntaxError, ('msgStr',), |
| 246 | { 'message' : 'msgStr', 'args' : ('msgStr', ), |
| 247 | 'print_file_and_line' : None, 'msg' : 'msgStr', |
| 248 | 'filename' : None, 'lineno' : None, 'offset' : None, |
| 249 | 'text' : None }), |
| 250 | ( SyntaxError, ('msgStr', ('filenameStr', 'linenoStr', 'offsetStr', |
| 251 | 'textStr')), |
| 252 | { 'message' : '', 'args' : ('msgStr', ('filenameStr', |
| 253 | 'linenoStr', 'offsetStr', 'textStr' )), |
| 254 | 'print_file_and_line' : None, 'msg' : 'msgStr', |
| 255 | 'filename' : 'filenameStr', 'lineno' : 'linenoStr', |
| 256 | 'offset' : 'offsetStr', 'text' : 'textStr' }), |
| 257 | ( SyntaxError, ('msgStr', 'filenameStr', 'linenoStr', 'offsetStr', |
| 258 | 'textStr', 'print_file_and_lineStr'), |
| 259 | { 'message' : '', 'args' : ('msgStr', 'filenameStr', |
| 260 | 'linenoStr', 'offsetStr', 'textStr', |
| 261 | 'print_file_and_lineStr'), |
| 262 | 'print_file_and_line' : None, 'msg' : 'msgStr', |
| 263 | 'filename' : None, 'lineno' : None, 'offset' : None, |
| 264 | 'text' : None }), |
| 265 | ( UnicodeError, (), |
| 266 | { 'message' : '', 'args' : (), }), |
| 267 | ( sampleUnicodeEncodeError, |
| 268 | { 'message' : '', 'args' : ('ascii', u'Hello \xe1', 6, 7, |
| 269 | 'ordinal not in range(128)'), |
| 270 | 'encoding' : 'ascii', 'object' : u'Hello \xe1', |
| 271 | 'start' : 6, 'reason' : 'ordinal not in range(128)' }), |
| 272 | ( sampleUnicodeDecodeError, |
| 273 | { 'message' : '', 'args' : ('ascii', '\xff', 0, 1, |
| 274 | 'ordinal not in range(128)'), |
| 275 | 'encoding' : 'ascii', 'object' : '\xff', |
| 276 | 'start' : 0, 'reason' : 'ordinal not in range(128)' }), |
| 277 | ( UnicodeTranslateError, (u"\u3042", 0, 1, "ouch"), |
| 278 | { 'message' : '', 'args' : (u'\u3042', 0, 1, 'ouch'), |
| 279 | 'object' : u'\u3042', 'reason' : 'ouch', |
| 280 | 'start' : 0, 'end' : 1 }), |
| 281 | ] |
| 282 | try: |
| 283 | exceptionList.append( |
| 284 | ( WindowsError, (1, 'strErrorStr', 'filenameStr'), |
| 285 | { 'message' : '', 'args' : (1, 'strErrorStr'), |
| 286 | 'strerror' : 'strErrorStr', |
| 287 | 'errno' : 22, 'filename' : 'filenameStr', |
| 288 | 'winerror' : 1 })) |
| 289 | except NameError: pass |
| 290 | |
| 291 | for args in exceptionList: |
| 292 | expected = args[-1] |
| 293 | try: |
| 294 | if len(args) == 2: raise args[0] |
| 295 | else: raise apply(args[0], args[1]) |
| 296 | except BaseException, e: |
| 297 | for checkArgName in expected.keys(): |
| 298 | if repr(getattr(e, checkArgName)) != repr(expected[checkArgName]): |
| 299 | raise TestFailed('Checking exception arguments, exception ' |
| 300 | '"%s", attribute "%s" expected %s got %s.' % |
| 301 | ( repr(e), checkArgName, |
| 302 | repr(expected[checkArgName]), |
| 303 | repr(getattr(e, checkArgName)) )) |