Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +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 | sys.stdin = fp |
| 23 | x = raw_input() |
| 24 | except EOFError: |
| 25 | pass |
| 26 | finally: |
| 27 | sys.stdin = savestdin |
| 28 | fp.close() |
| 29 | |
| 30 | r(IOError) |
| 31 | try: open('this file does not exist', 'r') |
| 32 | except IOError: pass |
| 33 | |
| 34 | r(ImportError) |
| 35 | try: import undefined_module |
| 36 | except ImportError: pass |
| 37 | |
| 38 | r(IndexError) |
| 39 | x = [] |
| 40 | try: a = x[10] |
| 41 | except IndexError: pass |
| 42 | |
| 43 | r(KeyError) |
| 44 | x = {} |
| 45 | try: a = x['key'] |
| 46 | except KeyError: pass |
| 47 | |
| 48 | r(KeyboardInterrupt) |
| 49 | print '(not testable in a script)' |
| 50 | |
| 51 | r(MemoryError) |
| 52 | print '(not safe to test)' |
| 53 | |
| 54 | r(NameError) |
| 55 | try: x = undefined_variable |
| 56 | except NameError: pass |
| 57 | |
| 58 | r(OverflowError) |
| 59 | x = 1 |
| 60 | try: |
| 61 | while 1: x = x+x |
| 62 | except OverflowError: pass |
| 63 | |
| 64 | r(RuntimeError) |
| 65 | print '(not used any more?)' |
| 66 | |
| 67 | r(SyntaxError) |
| 68 | try: exec('/\n') |
| 69 | except SyntaxError: pass |
| 70 | |
| 71 | r(SystemError) |
| 72 | print '(hard to reproduce)' |
| 73 | |
| 74 | r(SystemExit) |
| 75 | import sys |
| 76 | try: sys.exit(0) |
| 77 | except SystemExit: pass |
| 78 | |
| 79 | r(TypeError) |
| 80 | try: [] + () |
| 81 | except TypeError: pass |
| 82 | |
| 83 | r(ValueError) |
| 84 | try: x = chr(10000) |
| 85 | except ValueError: pass |
| 86 | |
| 87 | r(ZeroDivisionError) |
| 88 | try: x = 1/0 |
| 89 | except ZeroDivisionError: pass |
| 90 | |
| 91 | unlink(TESTFN) |