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