home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 April / PCWorld_2001-04_cd.bin / Software / TemaCD / webclean / !!!python!!! / BeOpen-Python-2.0.exe / TEST_EXCEPTIONS.PY < prev    next >
Encoding:
Python Source  |  2000-09-28  |  3.1 KB  |  171 lines

  1. # Python test set -- part 5, built-in exceptions
  2.  
  3. from test_support import *
  4. from types import ClassType
  5.  
  6. print '5. Built-in exceptions'
  7. # XXX This is not really enough, each *operation* should be tested!
  8.  
  9. def test_raise_catch(exc):
  10.     try:
  11.         raise exc, "spam"
  12.     except exc, err:
  13.         buf = str(err)
  14.     try:
  15.         raise exc("spam")
  16.     except exc, err:
  17.         buf = str(err)
  18.     print buf
  19.  
  20. def r(thing):
  21.     test_raise_catch(thing)
  22.     if isinstance(thing, ClassType):
  23.         print thing.__name__
  24.     else:
  25.         print thing
  26.  
  27. r(AttributeError)
  28. import sys
  29. try: x = sys.undefined_attribute
  30. except AttributeError: pass
  31.  
  32. r(EOFError)
  33. import sys
  34. fp = open(TESTFN, 'w')
  35. fp.close()
  36. fp = open(TESTFN, 'r')
  37. savestdin = sys.stdin
  38. try:
  39.         try:
  40.                 sys.stdin = fp
  41.                 x = raw_input()
  42.         except EOFError:
  43.                 pass
  44. finally:
  45.         sys.stdin = savestdin
  46.         fp.close()
  47.  
  48. r(IOError)
  49. try: open('this file does not exist', 'r')
  50. except IOError: pass
  51.  
  52. r(ImportError)
  53. try: import undefined_module
  54. except ImportError: pass
  55.  
  56. r(IndexError)
  57. x = []
  58. try: a = x[10]
  59. except IndexError: pass
  60.  
  61. r(KeyError)
  62. x = {}
  63. try: a = x['key']
  64. except KeyError: pass
  65.  
  66. r(KeyboardInterrupt)
  67. print '(not testable in a script)'
  68.  
  69. r(MemoryError)
  70. print '(not safe to test)'
  71.  
  72. r(NameError)
  73. try: x = undefined_variable
  74. except NameError: pass
  75.  
  76. r(OverflowError)
  77. x = 1
  78. try:
  79.         while 1: x = x+x
  80. except OverflowError: pass
  81.  
  82. r(RuntimeError)
  83. print '(not used any more?)'
  84.  
  85. r(SyntaxError)
  86. try: exec '/\n'
  87. except SyntaxError: pass
  88.  
  89. # make sure the right exception message is raised for each of these
  90. # code fragments:
  91.  
  92. def ckmsg(src, msg):
  93.     try:
  94.         compile(src, '<fragment>', 'exec')
  95.     except SyntaxError, e:
  96.         print e.msg
  97.         if e.msg == msg:
  98.             print "ok"
  99.         else:
  100.             print "expected:", msg
  101.     else:
  102.         print "failed to get expected SyntaxError"
  103.  
  104. s = '''\
  105. while 1:
  106.     try:
  107.         continue
  108.     except:
  109.         pass
  110. '''
  111. ckmsg(s, "'continue' not supported inside 'try' clause")
  112. s = '''\
  113. while 1:
  114.     try:
  115.         continue
  116.     finally:
  117.         pass
  118. '''
  119. ckmsg(s, "'continue' not supported inside 'try' clause")
  120. s = '''\
  121. while 1:
  122.     try:
  123.         if 1:
  124.             continue
  125.     finally:
  126.         pass
  127. '''
  128. ckmsg(s, "'continue' not supported inside 'try' clause")
  129. s = '''\
  130. try:
  131.     continue
  132. except:
  133.     pass
  134. '''
  135. ckmsg(s, "'continue' not properly in loop")
  136. ckmsg("continue\n", "'continue' not properly in loop")
  137.  
  138. r(IndentationError)
  139.  
  140. r(TabError)
  141. # can only be tested under -tt, and is the only test for -tt
  142. #try: compile("try:\n\t1/0\n    \t1/0\nfinally:\n pass\n", '<string>', 'exec')
  143. #except TabError: pass
  144. #else: raise TestFailed
  145.  
  146. r(SystemError)
  147. print '(hard to reproduce)'
  148.  
  149. r(SystemExit)
  150. import sys
  151. try: sys.exit(0)
  152. except SystemExit: pass
  153.  
  154. r(TypeError)
  155. try: [] + ()
  156. except TypeError: pass
  157.  
  158. r(ValueError)
  159. try: x = chr(10000)
  160. except ValueError: pass
  161.  
  162. r(ZeroDivisionError)
  163. try: x = 1/0
  164. except ZeroDivisionError: pass
  165.  
  166. r(Exception)
  167. try: x = 1/0
  168. except Exception, e: pass
  169.  
  170. unlink(TESTFN)
  171.