home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 July / maximum-cd-2011-07.iso / DiscContents / LibO_3.3.2_Win_x86_install_multi.exe / libreoffice1.cab / test_exceptions.py < prev    next >
Encoding:
Python Source  |  2011-03-15  |  14.9 KB  |  398 lines

  1. # Python test set -- part 5, built-in exceptions
  2.  
  3. import os
  4. import sys
  5. import unittest
  6. import pickle, cPickle
  7. import warnings
  8.  
  9. from test.test_support import TESTFN, unlink, run_unittest, captured_output
  10. from test.test_pep352 import ignore_message_warning
  11.  
  12. # XXX This is not really enough, each *operation* should be tested!
  13.  
  14. class ExceptionTests(unittest.TestCase):
  15.  
  16.     def testReload(self):
  17.         # Reloading the built-in exceptions module failed prior to Py2.2, while it
  18.         # should act the same as reloading built-in sys.
  19.         try:
  20.             import exceptions
  21.             reload(exceptions)
  22.         except ImportError, e:
  23.             self.fail("reloading exceptions: %s" % e)
  24.  
  25.     def raise_catch(self, exc, excname):
  26.         try:
  27.             raise exc, "spam"
  28.         except exc, err:
  29.             buf1 = str(err)
  30.         try:
  31.             raise exc("spam")
  32.         except exc, err:
  33.             buf2 = str(err)
  34.         self.assertEquals(buf1, buf2)
  35.         self.assertEquals(exc.__name__, excname)
  36.  
  37.     def testRaising(self):
  38.         self.raise_catch(AttributeError, "AttributeError")
  39.         self.assertRaises(AttributeError, getattr, sys, "undefined_attribute")
  40.  
  41.         self.raise_catch(EOFError, "EOFError")
  42.         fp = open(TESTFN, 'w')
  43.         fp.close()
  44.         fp = open(TESTFN, 'r')
  45.         savestdin = sys.stdin
  46.         try:
  47.             try:
  48.                 sys.stdin = fp
  49.                 x = raw_input()
  50.             except EOFError:
  51.                 pass
  52.         finally:
  53.             sys.stdin = savestdin
  54.             fp.close()
  55.             unlink(TESTFN)
  56.  
  57.         self.raise_catch(IOError, "IOError")
  58.         self.assertRaises(IOError, open, 'this file does not exist', 'r')
  59.  
  60.         self.raise_catch(ImportError, "ImportError")
  61.         self.assertRaises(ImportError, __import__, "undefined_module")
  62.  
  63.         self.raise_catch(IndexError, "IndexError")
  64.         x = []
  65.         self.assertRaises(IndexError, x.__getitem__, 10)
  66.  
  67.         self.raise_catch(KeyError, "KeyError")
  68.         x = {}
  69.         self.assertRaises(KeyError, x.__getitem__, 'key')
  70.  
  71.         self.raise_catch(KeyboardInterrupt, "KeyboardInterrupt")
  72.  
  73.         self.raise_catch(MemoryError, "MemoryError")
  74.  
  75.         self.raise_catch(NameError, "NameError")
  76.         try: x = undefined_variable
  77.         except NameError: pass
  78.  
  79.         self.raise_catch(OverflowError, "OverflowError")
  80.         x = 1
  81.         for dummy in range(128):
  82.             x += x  # this simply shouldn't blow up
  83.  
  84.         self.raise_catch(RuntimeError, "RuntimeError")
  85.  
  86.         self.raise_catch(SyntaxError, "SyntaxError")
  87.         try: exec '/\n'
  88.         except SyntaxError: pass
  89.  
  90.         self.raise_catch(IndentationError, "IndentationError")
  91.  
  92.         self.raise_catch(TabError, "TabError")
  93.         # can only be tested under -tt, and is the only test for -tt
  94.         #try: compile("try:\n\t1/0\n    \t1/0\nfinally:\n pass\n", '<string>', 'exec')
  95.         #except TabError: pass
  96.         #else: self.fail("TabError not raised")
  97.  
  98.         self.raise_catch(SystemError, "SystemError")
  99.  
  100.         self.raise_catch(SystemExit, "SystemExit")
  101.         self.assertRaises(SystemExit, sys.exit, 0)
  102.  
  103.         self.raise_catch(TypeError, "TypeError")
  104.         try: [] + ()
  105.         except TypeError: pass
  106.  
  107.         self.raise_catch(ValueError, "ValueError")
  108.         self.assertRaises(ValueError, chr, 10000)
  109.  
  110.         self.raise_catch(ZeroDivisionError, "ZeroDivisionError")
  111.         try: x = 1/0
  112.         except ZeroDivisionError: pass
  113.  
  114.         self.raise_catch(Exception, "Exception")
  115.         try: x = 1/0
  116.         except Exception, e: pass
  117.  
  118.     def testSyntaxErrorMessage(self):
  119.         # make sure the right exception message is raised for each of
  120.         # these code fragments
  121.  
  122.         def ckmsg(src, msg):
  123.             try:
  124.                 compile(src, '<fragment>', 'exec')
  125.             except SyntaxError, e:
  126.                 if e.msg != msg:
  127.                     self.fail("expected %s, got %s" % (msg, e.msg))
  128.             else:
  129.                 self.fail("failed to get expected SyntaxError")
  130.  
  131.         s = '''while 1:
  132.             try:
  133.                 pass
  134.             finally:
  135.                 continue'''
  136.  
  137.         if not sys.platform.startswith('java'):
  138.             ckmsg(s, "'continue' not supported inside 'finally' clause")
  139.  
  140.         s = '''if 1:
  141.         try:
  142.             continue
  143.         except:
  144.             pass'''
  145.  
  146.         ckmsg(s, "'continue' not properly in loop")
  147.         ckmsg("continue\n", "'continue' not properly in loop")
  148.  
  149.     def testSettingException(self):
  150.         # test that setting an exception at the C level works even if the
  151.         # exception object can't be constructed.
  152.  
  153.         class BadException:
  154.             def __init__(self_):
  155.                 raise RuntimeError, "can't instantiate BadException"
  156.  
  157.         def test_capi1():
  158.             import _testcapi
  159.             try:
  160.                 _testcapi.raise_exception(BadException, 1)
  161.             except TypeError, err:
  162.                 exc, err, tb = sys.exc_info()
  163.                 co = tb.tb_frame.f_code
  164.                 self.assertEquals(co.co_name, "test_capi1")
  165.                 self.assert_(co.co_filename.endswith('test_exceptions'+os.extsep+'py'))
  166.             else:
  167.                 self.fail("Expected exception")
  168.  
  169.         def test_capi2():
  170.             import _testcapi
  171.             try:
  172.                 _testcapi.raise_exception(BadException, 0)
  173.             except RuntimeError, err:
  174.                 exc, err, tb = sys.exc_info()
  175.                 co = tb.tb_frame.f_code
  176.                 self.assertEquals(co.co_name, "__init__")
  177.                 self.assert_(co.co_filename.endswith('test_exceptions'+os.extsep+'py'))
  178.                 co2 = tb.tb_frame.f_back.f_code
  179.                 self.assertEquals(co2.co_name, "test_capi2")
  180.             else:
  181.                 self.fail("Expected exception")
  182.  
  183.         if not sys.platform.startswith('java'):
  184.             test_capi1()
  185.             test_capi2()
  186.  
  187.     def test_WindowsError(self):
  188.         try:
  189.             WindowsError
  190.         except NameError:
  191.             pass
  192.         else:
  193.             self.failUnlessEqual(str(WindowsError(1001)),
  194.                                  "1001")
  195.             self.failUnlessEqual(str(WindowsError(1001, "message")),
  196.                                  "[Error 1001] message")
  197.             self.failUnlessEqual(WindowsError(1001, "message").errno, 22)
  198.             self.failUnlessEqual(WindowsError(1001, "message").winerror, 1001)
  199.  
  200.     def testAttributes(self):
  201.         # test that exception attributes are happy
  202.  
  203.         exceptionList = [
  204.             (BaseException, (), {'message' : '', 'args' : ()}),
  205.             (BaseException, (1, ), {'message' : 1, 'args' : (1,)}),
  206.             (BaseException, ('foo',),
  207.                 {'message' : 'foo', 'args' : ('foo',)}),
  208.             (BaseException, ('foo', 1),
  209.                 {'message' : '', 'args' : ('foo', 1)}),
  210.             (SystemExit, ('foo',),
  211.                 {'message' : 'foo', 'args' : ('foo',), 'code' : 'foo'}),
  212.             (IOError, ('foo',),
  213.                 {'message' : 'foo', 'args' : ('foo',), 'filename' : None,
  214.                  'errno' : None, 'strerror' : None}),
  215.             (IOError, ('foo', 'bar'),
  216.                 {'message' : '', 'args' : ('foo', 'bar'), 'filename' : None,
  217.                  'errno' : 'foo', 'strerror' : 'bar'}),
  218.             (IOError, ('foo', 'bar', 'baz'),
  219.                 {'message' : '', 'args' : ('foo', 'bar'), 'filename' : 'baz',
  220.                  'errno' : 'foo', 'strerror' : 'bar'}),
  221.             (IOError, ('foo', 'bar', 'baz', 'quux'),
  222.                 {'message' : '', 'args' : ('foo', 'bar', 'baz', 'quux')}),
  223.             (EnvironmentError, ('errnoStr', 'strErrorStr', 'filenameStr'),
  224.                 {'message' : '', 'args' : ('errnoStr', 'strErrorStr'),
  225.                  'strerror' : 'strErrorStr', 'errno' : 'errnoStr',
  226.                  'filename' : 'filenameStr'}),
  227.             (EnvironmentError, (1, 'strErrorStr', 'filenameStr'),
  228.                 {'message' : '', 'args' : (1, 'strErrorStr'), 'errno' : 1,
  229.                  'strerror' : 'strErrorStr', 'filename' : 'filenameStr'}),
  230.             (SyntaxError, (), {'message' : '', 'msg' : None, 'text' : None,
  231.                 'filename' : None, 'lineno' : None, 'offset' : None,
  232.                 'print_file_and_line' : None}),
  233.             (SyntaxError, ('msgStr',),
  234.                 {'message' : 'msgStr', 'args' : ('msgStr',), 'text' : None,
  235.                  'print_file_and_line' : None, 'msg' : 'msgStr',
  236.                  'filename' : None, 'lineno' : None, 'offset' : None}),
  237.             (SyntaxError, ('msgStr', ('filenameStr', 'linenoStr', 'offsetStr',
  238.                            'textStr')),
  239.                 {'message' : '', 'offset' : 'offsetStr', 'text' : 'textStr',
  240.                  'args' : ('msgStr', ('filenameStr', 'linenoStr',
  241.                                       'offsetStr', 'textStr')),
  242.                  'print_file_and_line' : None, 'msg' : 'msgStr',
  243.                  'filename' : 'filenameStr', 'lineno' : 'linenoStr'}),
  244.             (SyntaxError, ('msgStr', 'filenameStr', 'linenoStr', 'offsetStr',
  245.                            'textStr', 'print_file_and_lineStr'),
  246.                 {'message' : '', 'text' : None,
  247.                  'args' : ('msgStr', 'filenameStr', 'linenoStr', 'offsetStr',
  248.                            'textStr', 'print_file_and_lineStr'),
  249.                  'print_file_and_line' : None, 'msg' : 'msgStr',
  250.                  'filename' : None, 'lineno' : None, 'offset' : None}),
  251.             (UnicodeError, (), {'message' : '', 'args' : (),}),
  252.             (UnicodeEncodeError, ('ascii', u'a', 0, 1, 'ordinal not in range'),
  253.                 {'message' : '', 'args' : ('ascii', u'a', 0, 1,
  254.                                            'ordinal not in range'),
  255.                  'encoding' : 'ascii', 'object' : u'a',
  256.                  'start' : 0, 'reason' : 'ordinal not in range'}),
  257.             (UnicodeDecodeError, ('ascii', '\xff', 0, 1, 'ordinal not in range'),
  258.                 {'message' : '', 'args' : ('ascii', '\xff', 0, 1,
  259.                                            'ordinal not in range'),
  260.                  'encoding' : 'ascii', 'object' : '\xff',
  261.                  'start' : 0, 'reason' : 'ordinal not in range'}),
  262.             (UnicodeTranslateError, (u"\u3042", 0, 1, "ouch"),
  263.                 {'message' : '', 'args' : (u'\u3042', 0, 1, 'ouch'),
  264.                  'object' : u'\u3042', 'reason' : 'ouch',
  265.                  'start' : 0, 'end' : 1}),
  266.         ]
  267.         try:
  268.             exceptionList.append(
  269.                 (WindowsError, (1, 'strErrorStr', 'filenameStr'),
  270.                     {'message' : '', 'args' : (1, 'strErrorStr'),
  271.                      'strerror' : 'strErrorStr', 'winerror' : 1,
  272.                      'errno' : 22, 'filename' : 'filenameStr'})
  273.             )
  274.         except NameError:
  275.             pass
  276.  
  277.         with warnings.catch_warnings():
  278.             ignore_message_warning()
  279.             for exc, args, expected in exceptionList:
  280.                 try:
  281.                     raise exc(*args)
  282.                 except BaseException, e:
  283.                     if type(e) is not exc:
  284.                         raise
  285.                     # Verify module name
  286.                     self.assertEquals(type(e).__module__, 'exceptions')
  287.                     # Verify no ref leaks in Exc_str()
  288.                     s = str(e)
  289.                     for checkArgName in expected:
  290.                         self.assertEquals(repr(getattr(e, checkArgName)),
  291.                                           repr(expected[checkArgName]),
  292.                                           'exception "%s", attribute "%s"' %
  293.                                            (repr(e), checkArgName))
  294.  
  295.                     # test for pickling support
  296.                     for p in pickle, cPickle:
  297.                         for protocol in range(p.HIGHEST_PROTOCOL + 1):
  298.                             new = p.loads(p.dumps(e, protocol))
  299.                             for checkArgName in expected:
  300.                                 got = repr(getattr(new, checkArgName))
  301.                                 want = repr(expected[checkArgName])
  302.                                 self.assertEquals(got, want,
  303.                                                   'pickled "%r", attribute "%s"' %
  304.                                                   (e, checkArgName))
  305.  
  306.     def testSlicing(self):
  307.         # Test that you can slice an exception directly instead of requiring
  308.         # going through the 'args' attribute.
  309.         args = (1, 2, 3)
  310.         exc = BaseException(*args)
  311.         self.failUnlessEqual(exc[:], args)
  312.  
  313.     def testKeywordArgs(self):
  314.         # test that builtin exception don't take keyword args,
  315.         # but user-defined subclasses can if they want
  316.         self.assertRaises(TypeError, BaseException, a=1)
  317.  
  318.         class DerivedException(BaseException):
  319.             def __init__(self, fancy_arg):
  320.                 BaseException.__init__(self)
  321.                 self.fancy_arg = fancy_arg
  322.  
  323.         x = DerivedException(fancy_arg=42)
  324.         self.assertEquals(x.fancy_arg, 42)
  325.  
  326.     def testInfiniteRecursion(self):
  327.         def f():
  328.             return f()
  329.         self.assertRaises(RuntimeError, f)
  330.  
  331.         def g():
  332.             try:
  333.                 return g()
  334.             except ValueError:
  335.                 return -1
  336.  
  337.         # The test prints an unraisable recursion error when
  338.         # doing "except ValueError", this is because subclass
  339.         # checking has recursion checking too.
  340.         with captured_output("stderr"):
  341.             try:
  342.                 g()
  343.             except RuntimeError:
  344.                 pass
  345.             except:
  346.                 self.fail("Should have raised KeyError")
  347.             else:
  348.                 self.fail("Should have raised KeyError")
  349.  
  350.     def testUnicodeStrUsage(self):
  351.         # Make sure both instances and classes have a str and unicode
  352.         # representation.
  353.         self.failUnless(str(Exception))
  354.         self.failUnless(unicode(Exception))
  355.         self.failUnless(str(Exception('a')))
  356.         self.failUnless(unicode(Exception(u'a')))
  357.         self.failUnless(unicode(Exception(u'\xe1')))
  358.  
  359.     def test_badisinstance(self):
  360.         # Bug #2542: if issubclass(e, MyException) raises an exception,
  361.         # it should be ignored
  362.         class Meta(type):
  363.             def __subclasscheck__(cls, subclass):
  364.                 raise ValueError()
  365.  
  366.         class MyException(Exception):
  367.             __metaclass__ = Meta
  368.             pass
  369.  
  370.         with captured_output("stderr") as stderr:
  371.             try:
  372.                 raise KeyError()
  373.             except MyException, e:
  374.                 self.fail("exception should not be a MyException")
  375.             except KeyError:
  376.                 pass
  377.             except:
  378.                 self.fail("Should have raised KeyError")
  379.             else:
  380.                 self.fail("Should have raised KeyError")
  381.  
  382.         with captured_output("stderr") as stderr:
  383.             def g():
  384.                 try:
  385.                     return g()
  386.                 except RuntimeError:
  387.                     return sys.exc_info()
  388.             e, v, tb = g()
  389.             self.assert_(e is RuntimeError, e)
  390.             self.assert_("maximum recursion depth exceeded" in str(v), v)
  391.  
  392.  
  393. def test_main():
  394.     run_unittest(ExceptionTests)
  395.  
  396. if __name__ == '__main__':
  397.     test_main()
  398.