blob: dea88fee8b35cf0340e0fc740e5f7769d6beaf65 [file] [log] [blame]
Guido van Rossum3bead091992-01-27 17:00:37 +00001# Python test set -- part 5, built-in exceptions
2
3from test_support import *
Barry Warsaw6ed41a01997-08-29 21:58:25 +00004from types import ClassType
Guido van Rossum3bead091992-01-27 17:00:37 +00005
6print '5. Built-in exceptions'
7# XXX This is not really enough, each *operation* should be tested!
8
Barry Warsawb9c1d3d2001-08-13 23:07:00 +00009# Reloading the built-in exceptions module failed prior to Py2.2, while it
10# should act the same as reloading built-in sys.
11try:
12 import exceptions
13 reload(exceptions)
14except ImportError, e:
15 raise TestFailed, e
16
Jeremy Hylton56c807d2000-06-20 18:52:57 +000017def test_raise_catch(exc):
18 try:
19 raise exc, "spam"
20 except exc, err:
21 buf = str(err)
22 try:
23 raise exc("spam")
24 except exc, err:
25 buf = str(err)
26 print buf
27
Barry Warsaw6ed41a01997-08-29 21:58:25 +000028def r(thing):
Jeremy Hylton56c807d2000-06-20 18:52:57 +000029 test_raise_catch(thing)
Barry Warsaw3a9d0612000-09-01 06:53:52 +000030 if isinstance(thing, ClassType):
Guido van Rossum41360a41998-03-26 19:42:58 +000031 print thing.__name__
Barry Warsaw6ed41a01997-08-29 21:58:25 +000032 else:
Guido van Rossum41360a41998-03-26 19:42:58 +000033 print thing
Guido van Rossum3bead091992-01-27 17:00:37 +000034
35r(AttributeError)
36import sys
37try: x = sys.undefined_attribute
38except AttributeError: pass
39
40r(EOFError)
41import sys
42fp = open(TESTFN, 'w')
43fp.close()
44fp = open(TESTFN, 'r')
45savestdin = sys.stdin
46try:
Fred Drake2e6d25c2000-10-23 17:00:30 +000047 try:
48 sys.stdin = fp
49 x = raw_input()
50 except EOFError:
51 pass
Guido van Rossum3bead091992-01-27 17:00:37 +000052finally:
Fred Drake2e6d25c2000-10-23 17:00:30 +000053 sys.stdin = savestdin
54 fp.close()
Guido van Rossum3bead091992-01-27 17:00:37 +000055
56r(IOError)
57try: open('this file does not exist', 'r')
58except IOError: pass
59
60r(ImportError)
61try: import undefined_module
62except ImportError: pass
63
64r(IndexError)
65x = []
66try: a = x[10]
67except IndexError: pass
68
69r(KeyError)
70x = {}
71try: a = x['key']
72except KeyError: pass
73
74r(KeyboardInterrupt)
75print '(not testable in a script)'
76
77r(MemoryError)
78print '(not safe to test)'
79
80r(NameError)
81try: x = undefined_variable
82except NameError: pass
83
84r(OverflowError)
85x = 1
86try:
Fred Drake2e6d25c2000-10-23 17:00:30 +000087 while 1: x = x+x
Guido van Rossum3bead091992-01-27 17:00:37 +000088except OverflowError: pass
89
90r(RuntimeError)
91print '(not used any more?)'
92
93r(SyntaxError)
Guido van Rossume2cb7271995-08-11 14:24:47 +000094try: exec '/\n'
Guido van Rossum3bead091992-01-27 17:00:37 +000095except SyntaxError: pass
96
Fred Drake72e48bd2000-09-08 16:32:34 +000097# make sure the right exception message is raised for each of these
98# code fragments:
99
100def ckmsg(src, msg):
101 try:
102 compile(src, '<fragment>', 'exec')
103 except SyntaxError, e:
104 print e.msg
105 if e.msg == msg:
106 print "ok"
107 else:
108 print "expected:", msg
109 else:
110 print "failed to get expected SyntaxError"
111
112s = '''\
113while 1:
114 try:
Fred Drake72e48bd2000-09-08 16:32:34 +0000115 pass
Fred Drake72e48bd2000-09-08 16:32:34 +0000116 finally:
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000117 continue
Fred Drake72e48bd2000-09-08 16:32:34 +0000118'''
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000119ckmsg(s, "'continue' not supported inside 'finally' clause")
Fred Drake72e48bd2000-09-08 16:32:34 +0000120s = '''\
121try:
122 continue
123except:
124 pass
125'''
126ckmsg(s, "'continue' not properly in loop")
127ckmsg("continue\n", "'continue' not properly in loop")
128
Fred Drake85f36392000-07-11 17:53:00 +0000129r(IndentationError)
130
131r(TabError)
132# can only be tested under -tt, and is the only test for -tt
133#try: compile("try:\n\t1/0\n \t1/0\nfinally:\n pass\n", '<string>', 'exec')
134#except TabError: pass
135#else: raise TestFailed
136
Guido van Rossum3bead091992-01-27 17:00:37 +0000137r(SystemError)
138print '(hard to reproduce)'
139
140r(SystemExit)
141import sys
142try: sys.exit(0)
143except SystemExit: pass
144
145r(TypeError)
146try: [] + ()
147except TypeError: pass
148
149r(ValueError)
150try: x = chr(10000)
151except ValueError: pass
152
153r(ZeroDivisionError)
154try: x = 1/0
155except ZeroDivisionError: pass
156
Barry Warsaw992cb8a2000-05-25 23:16:54 +0000157r(Exception)
158try: x = 1/0
159except Exception, e: pass
160
Guido van Rossum3bead091992-01-27 17:00:37 +0000161unlink(TESTFN)