blob: 076f4704e44d5aec77e0a2226cd351d0fae4292a [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
Jeremy Hylton56c807d2000-06-20 18:52:57 +00009def test_raise_catch(exc):
10 try:
11 raise exc, "spam"
12 except exc, err:
13 buf = str(err)
14 try:
15 raise exc("spam")
16 except exc, err:
17 buf = str(err)
18 print buf
19
Barry Warsaw6ed41a01997-08-29 21:58:25 +000020def r(thing):
Jeremy Hylton56c807d2000-06-20 18:52:57 +000021 test_raise_catch(thing)
Barry Warsaw3a9d0612000-09-01 06:53:52 +000022 if isinstance(thing, ClassType):
Guido van Rossum41360a41998-03-26 19:42:58 +000023 print thing.__name__
Barry Warsaw6ed41a01997-08-29 21:58:25 +000024 else:
Guido van Rossum41360a41998-03-26 19:42:58 +000025 print thing
Guido van Rossum3bead091992-01-27 17:00:37 +000026
27r(AttributeError)
28import sys
29try: x = sys.undefined_attribute
30except AttributeError: pass
31
32r(EOFError)
33import sys
34fp = open(TESTFN, 'w')
35fp.close()
36fp = open(TESTFN, 'r')
37savestdin = sys.stdin
38try:
Guido van Rossum41360a41998-03-26 19:42:58 +000039 try:
40 sys.stdin = fp
41 x = raw_input()
42 except EOFError:
43 pass
Guido van Rossum3bead091992-01-27 17:00:37 +000044finally:
Guido van Rossum41360a41998-03-26 19:42:58 +000045 sys.stdin = savestdin
46 fp.close()
Guido van Rossum3bead091992-01-27 17:00:37 +000047
48r(IOError)
49try: open('this file does not exist', 'r')
50except IOError: pass
51
52r(ImportError)
53try: import undefined_module
54except ImportError: pass
55
56r(IndexError)
57x = []
58try: a = x[10]
59except IndexError: pass
60
61r(KeyError)
62x = {}
63try: a = x['key']
64except KeyError: pass
65
66r(KeyboardInterrupt)
67print '(not testable in a script)'
68
69r(MemoryError)
70print '(not safe to test)'
71
72r(NameError)
73try: x = undefined_variable
74except NameError: pass
75
76r(OverflowError)
77x = 1
78try:
Guido van Rossum41360a41998-03-26 19:42:58 +000079 while 1: x = x+x
Guido van Rossum3bead091992-01-27 17:00:37 +000080except OverflowError: pass
81
82r(RuntimeError)
83print '(not used any more?)'
84
85r(SyntaxError)
Guido van Rossume2cb7271995-08-11 14:24:47 +000086try: exec '/\n'
Guido van Rossum3bead091992-01-27 17:00:37 +000087except SyntaxError: pass
88
Fred Drake72e48bd2000-09-08 16:32:34 +000089# make sure the right exception message is raised for each of these
90# code fragments:
91
92def ckmsg(src, msg):
93 try:
94 compile(src, '<fragment>', 'exec')
95 except SyntaxError, e:
96 print e.msg
97 if e.msg == msg:
98 print "ok"
99 else:
100 print "expected:", msg
101 else:
102 print "failed to get expected SyntaxError"
103
104s = '''\
105while 1:
106 try:
107 continue
108 except:
109 pass
110'''
111ckmsg(s, "'continue' not supported inside 'try' clause")
112s = '''\
113while 1:
114 try:
115 continue
116 finally:
117 pass
118'''
119ckmsg(s, "'continue' not supported inside 'try' clause")
120s = '''\
121while 1:
122 try:
123 if 1:
124 continue
125 finally:
126 pass
127'''
128ckmsg(s, "'continue' not supported inside 'try' clause")
129s = '''\
130try:
131 continue
132except:
133 pass
134'''
135ckmsg(s, "'continue' not properly in loop")
136ckmsg("continue\n", "'continue' not properly in loop")
137
Fred Drake85f36392000-07-11 17:53:00 +0000138r(IndentationError)
139
140r(TabError)
141# can only be tested under -tt, and is the only test for -tt
142#try: compile("try:\n\t1/0\n \t1/0\nfinally:\n pass\n", '<string>', 'exec')
143#except TabError: pass
144#else: raise TestFailed
145
Guido van Rossum3bead091992-01-27 17:00:37 +0000146r(SystemError)
147print '(hard to reproduce)'
148
149r(SystemExit)
150import sys
151try: sys.exit(0)
152except SystemExit: pass
153
154r(TypeError)
155try: [] + ()
156except TypeError: pass
157
158r(ValueError)
159try: x = chr(10000)
160except ValueError: pass
161
162r(ZeroDivisionError)
163try: x = 1/0
164except ZeroDivisionError: pass
165
Barry Warsaw992cb8a2000-05-25 23:16:54 +0000166r(Exception)
167try: x = 1/0
168except Exception, e: pass
169
Guido van Rossum3bead091992-01-27 17:00:37 +0000170unlink(TESTFN)