Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_exceptions.py @ 5817:48b94de85bf3 legacy-trunk
Extensive changes by AMK.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 17 Jul 1997 16:29:42 +0000 |
| parents | cfc66b8ca202 |
| children | cff105a3a384 |
| rev | line source |
|---|---|
| 653 | 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: | |
|
853
4d58e2c50887
Unmerged except and finally clauses
Guido van Rossum <guido@python.org>
parents:
653
diff
changeset
|
22 try: |
|
4d58e2c50887
Unmerged except and finally clauses
Guido van Rossum <guido@python.org>
parents:
653
diff
changeset
|
23 sys.stdin = fp |
|
4d58e2c50887
Unmerged except and finally clauses
Guido van Rossum <guido@python.org>
parents:
653
diff
changeset
|
24 x = raw_input() |
|
4d58e2c50887
Unmerged except and finally clauses
Guido van Rossum <guido@python.org>
parents:
653
diff
changeset
|
25 except EOFError: |
|
4d58e2c50887
Unmerged except and finally clauses
Guido van Rossum <guido@python.org>
parents:
653
diff
changeset
|
26 pass |
| 653 | 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) | |
| 2970 | 69 try: exec '/\n' |
| 653 | 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) |
