test_exceptions.py

来自「minimal python variant for small footpri」· Python 代码 · 共 98 行

PY
98
字号
# Python test set -- part 5, built-in exceptionsfrom test_support import *from types import ClassTypeprint '5. Built-in exceptions'# XXX This is not really enough, each *operation* should be tested!def r(thing):    if type(thing) == ClassType:        print thing.__name__    else:        print thingr(AttributeError)import systry: x = sys.undefined_attributeexcept AttributeError: passr(EOFError)import sysfp = open(TESTFN, 'w')fp.close()fp = open(TESTFN, 'r')savestdin = sys.stdintry:        try:                sys.stdin = fp                x = raw_input()        except EOFError:                passfinally:        sys.stdin = savestdin        fp.close()r(IOError)try: open('this file does not exist', 'r')except IOError: passr(ImportError)try: import undefined_moduleexcept ImportError: passr(IndexError)x = []try: a = x[10]except IndexError: passr(KeyError)x = {}try: a = x['key']except KeyError: passr(KeyboardInterrupt)print '(not testable in a script)'r(MemoryError)print '(not safe to test)'r(NameError)try: x = undefined_variableexcept NameError: passr(OverflowError)x = 1try:        while 1: x = x+xexcept OverflowError: passr(RuntimeError)print '(not used any more?)'r(SyntaxError)try: exec '/\n'except SyntaxError: passr(SystemError)print '(hard to reproduce)'r(SystemExit)import systry: sys.exit(0)except SystemExit: passr(TypeError)try: [] + ()except TypeError: passr(ValueError)try: x = chr(10000)except ValueError: passr(ZeroDivisionError)try: x = 1/0except ZeroDivisionError: passunlink(TESTFN)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?