blob: 9f42659adc527da6c026f52ae93da8c47a4567c2 [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:
Fred Drake2e6d25c2000-10-23 17:00:30 +000039 try:
40 sys.stdin = fp
41 x = raw_input()
42 except EOFError:
43 pass
Guido van Rossum3bead091992-01-27 17:00:37 +000044finally:
Fred Drake2e6d25c2000-10-23 17:00:30 +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:
Fred Drake2e6d25c2000-10-23 17:00:30 +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:
Fred Drake72e48bd2000-09-08 16:32:34 +0000107 pass
Fred Drake72e48bd2000-09-08 16:32:34 +0000108 finally:
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000109 continue
Fred Drake72e48bd2000-09-08 16:32:34 +0000110'''
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000111ckmsg(s, "'continue' not supported inside 'finally' clause")
Fred Drake72e48bd2000-09-08 16:32:34 +0000112s = '''\
113try:
114 continue
115except:
116 pass
117'''
118ckmsg(s, "'continue' not properly in loop")
119ckmsg("continue\n", "'continue' not properly in loop")
120
Fred Drake85f36392000-07-11 17:53:00 +0000121r(IndentationError)
122
123r(TabError)
124# can only be tested under -tt, and is the only test for -tt
125#try: compile("try:\n\t1/0\n \t1/0\nfinally:\n pass\n", '<string>', 'exec')
126#except TabError: pass
127#else: raise TestFailed
128
Guido van Rossum3bead091992-01-27 17:00:37 +0000129r(SystemError)
130print '(hard to reproduce)'
131
132r(SystemExit)
133import sys
134try: sys.exit(0)
135except SystemExit: pass
136
137r(TypeError)
138try: [] + ()
139except TypeError: pass
140
141r(ValueError)
142try: x = chr(10000)
143except ValueError: pass
144
145r(ZeroDivisionError)
146try: x = 1/0
147except ZeroDivisionError: pass
148
Barry Warsaw992cb8a2000-05-25 23:16:54 +0000149r(Exception)
150try: x = 1/0
151except Exception, e: pass
152
Guido van Rossum3bead091992-01-27 17:00:37 +0000153unlink(TESTFN)