blob: 104258a549dfe7b6bba381b7f377b39633c69b8b [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 Rossum83b120d2001-08-23 03:23:03 +00005import warnings
6
7warnings.filterwarnings("error", "", OverflowWarning, __name__)
Guido van Rossum3bead091992-01-27 17:00:37 +00008
9print '5. Built-in exceptions'
10# XXX This is not really enough, each *operation* should be tested!
11
Barry Warsawb9c1d3d2001-08-13 23:07:00 +000012# Reloading the built-in exceptions module failed prior to Py2.2, while it
13# should act the same as reloading built-in sys.
14try:
15 import exceptions
16 reload(exceptions)
17except ImportError, e:
18 raise TestFailed, e
19
Jeremy Hylton56c807d2000-06-20 18:52:57 +000020def test_raise_catch(exc):
21 try:
22 raise exc, "spam"
23 except exc, err:
24 buf = str(err)
25 try:
26 raise exc("spam")
27 except exc, err:
28 buf = str(err)
29 print buf
30
Barry Warsaw6ed41a01997-08-29 21:58:25 +000031def r(thing):
Jeremy Hylton56c807d2000-06-20 18:52:57 +000032 test_raise_catch(thing)
Barry Warsaw3a9d0612000-09-01 06:53:52 +000033 if isinstance(thing, ClassType):
Guido van Rossum41360a41998-03-26 19:42:58 +000034 print thing.__name__
Barry Warsaw6ed41a01997-08-29 21:58:25 +000035 else:
Guido van Rossum41360a41998-03-26 19:42:58 +000036 print thing
Guido van Rossum3bead091992-01-27 17:00:37 +000037
38r(AttributeError)
39import sys
40try: x = sys.undefined_attribute
41except AttributeError: pass
42
43r(EOFError)
44import sys
45fp = open(TESTFN, 'w')
46fp.close()
47fp = open(TESTFN, 'r')
48savestdin = sys.stdin
49try:
Fred Drake2e6d25c2000-10-23 17:00:30 +000050 try:
51 sys.stdin = fp
52 x = raw_input()
53 except EOFError:
54 pass
Guido van Rossum3bead091992-01-27 17:00:37 +000055finally:
Fred Drake2e6d25c2000-10-23 17:00:30 +000056 sys.stdin = savestdin
57 fp.close()
Guido van Rossum3bead091992-01-27 17:00:37 +000058
59r(IOError)
60try: open('this file does not exist', 'r')
61except IOError: pass
62
63r(ImportError)
64try: import undefined_module
65except ImportError: pass
66
67r(IndexError)
68x = []
69try: a = x[10]
70except IndexError: pass
71
72r(KeyError)
73x = {}
74try: a = x['key']
75except KeyError: pass
76
77r(KeyboardInterrupt)
78print '(not testable in a script)'
79
80r(MemoryError)
81print '(not safe to test)'
82
83r(NameError)
84try: x = undefined_variable
85except NameError: pass
86
87r(OverflowError)
88x = 1
89try:
Fred Drake2e6d25c2000-10-23 17:00:30 +000090 while 1: x = x+x
Guido van Rossum3bead091992-01-27 17:00:37 +000091except OverflowError: pass
92
93r(RuntimeError)
94print '(not used any more?)'
95
96r(SyntaxError)
Guido van Rossume2cb7271995-08-11 14:24:47 +000097try: exec '/\n'
Guido van Rossum3bead091992-01-27 17:00:37 +000098except SyntaxError: pass
99
Fred Drake72e48bd2000-09-08 16:32:34 +0000100# make sure the right exception message is raised for each of these
101# code fragments:
102
103def ckmsg(src, msg):
104 try:
105 compile(src, '<fragment>', 'exec')
106 except SyntaxError, e:
107 print e.msg
108 if e.msg == msg:
109 print "ok"
110 else:
111 print "expected:", msg
112 else:
113 print "failed to get expected SyntaxError"
114
115s = '''\
116while 1:
117 try:
Fred Drake72e48bd2000-09-08 16:32:34 +0000118 pass
Fred Drake72e48bd2000-09-08 16:32:34 +0000119 finally:
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000120 continue
Fred Drake72e48bd2000-09-08 16:32:34 +0000121'''
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000122ckmsg(s, "'continue' not supported inside 'finally' clause")
Fred Drake72e48bd2000-09-08 16:32:34 +0000123s = '''\
124try:
125 continue
126except:
127 pass
128'''
129ckmsg(s, "'continue' not properly in loop")
130ckmsg("continue\n", "'continue' not properly in loop")
131
Fred Drake85f36392000-07-11 17:53:00 +0000132r(IndentationError)
133
134r(TabError)
135# can only be tested under -tt, and is the only test for -tt
136#try: compile("try:\n\t1/0\n \t1/0\nfinally:\n pass\n", '<string>', 'exec')
137#except TabError: pass
138#else: raise TestFailed
139
Guido van Rossum3bead091992-01-27 17:00:37 +0000140r(SystemError)
141print '(hard to reproduce)'
142
143r(SystemExit)
144import sys
145try: sys.exit(0)
146except SystemExit: pass
147
148r(TypeError)
149try: [] + ()
150except TypeError: pass
151
152r(ValueError)
153try: x = chr(10000)
154except ValueError: pass
155
156r(ZeroDivisionError)
157try: x = 1/0
158except ZeroDivisionError: pass
159
Barry Warsaw992cb8a2000-05-25 23:16:54 +0000160r(Exception)
161try: x = 1/0
162except Exception, e: pass
163
Guido van Rossum3bead091992-01-27 17:00:37 +0000164unlink(TESTFN)