blob: 4fbee3e76a1d3a5b6abff176e2d6f75c5fbca3b3 [file] [log] [blame]
Guido van Rossum5c971671996-07-22 15:23:25 +00001# Python test set -- part 5, built-in exceptions
2
3from test_support import *
4
5print '5. Built-in exceptions'
6# XXX This is not really enough, each *operation* should be tested!
7
8def r(name): print name
9
10r(AttributeError)
11import sys
12try: x = sys.undefined_attribute
13except AttributeError: pass
14
15r(EOFError)
16import sys
17fp = open(TESTFN, 'w')
18fp.close()
19fp = open(TESTFN, 'r')
20savestdin = sys.stdin
21try:
22 try:
23 sys.stdin = fp
24 x = raw_input()
25 except EOFError:
26 pass
27finally:
28 sys.stdin = savestdin
29 fp.close()
30
31r(IOError)
32try: open('this file does not exist', 'r')
33except IOError: pass
34
35r(ImportError)
36try: import undefined_module
37except ImportError: pass
38
39r(IndexError)
40x = []
41try: a = x[10]
42except IndexError: pass
43
44r(KeyError)
45x = {}
46try: a = x['key']
47except KeyError: pass
48
49r(KeyboardInterrupt)
50print '(not testable in a script)'
51
52r(MemoryError)
53print '(not safe to test)'
54
55r(NameError)
56try: x = undefined_variable
57except NameError: pass
58
59r(OverflowError)
60x = 1
61try:
62 while 1: x = x+x
63except OverflowError: pass
64
65r(RuntimeError)
66print '(not used any more?)'
67
68r(SyntaxError)
69try: exec '/\n'
70except SyntaxError: pass
71
72r(SystemError)
73print '(hard to reproduce)'
74
75r(SystemExit)
76import sys
77try: sys.exit(0)
78except SystemExit: pass
79
80r(TypeError)
81try: [] + ()
82except TypeError: pass
83
84r(ValueError)
85try: x = chr(10000)
86except ValueError: pass
87
88r(ZeroDivisionError)
89try: x = 1/0
90except ZeroDivisionError: pass
91
92unlink(TESTFN)