Back to index

lightning-sunbird  0.9+nobinonly
test_com_exceptions.py
Go to the documentation of this file.
00001 # ***** BEGIN LICENSE BLOCK *****
00002 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003 #
00004 # The contents of this file are subject to the Mozilla Public License Version
00005 # 1.1 (the "License"); you may not use this file except in compliance with
00006 # the License. You may obtain a copy of the License at
00007 # http://www.mozilla.org/MPL/
00008 #
00009 # Software distributed under the License is distributed on an "AS IS" basis,
00010 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011 # for the specific language governing rights and limitations under the
00012 # License.
00013 #
00014 # The Original Code is the Python XPCOM language bindings.
00015 #
00016 # The Initial Developer of the Original Code is ActiveState Tool Corp.
00017 # Portions created by ActiveState Tool Corp. are Copyright (C) 2000, 2001
00018 # ActiveState Tool Corp.  All Rights Reserved.
00019 #
00020 # Contributor(s):
00021 #   Mark Hammond <MarkH@ActiveState.com> (original author)
00022 #
00023 # Alternatively, the contents of this file may be used under the terms of
00024 # either the GNU General Public License Version 2 or later (the "GPL"), or
00025 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026 # in which case the provisions of the GPL or the LGPL are applicable instead
00027 # of those above. If you wish to allow use of your version of this file only
00028 # under the terms of either the GPL or the LGPL, and not to allow others to
00029 # use your version of this file under the terms of the MPL, indicate your
00030 # decision by deleting the provisions above and replace them with the notice
00031 # and other provisions required by the GPL or the LGPL. If you do not delete
00032 # the provisions above, a recipient may use your version of this file under
00033 # the terms of any one of the MPL, the GPL or the LGPL.
00034 #
00035 # ***** END LICENSE BLOCK *****
00036 
00037 # Test pyxpcom exception.
00038 
00039 from xpcom import components, nsError, ServerException, COMException
00040 from xpcom.server import WrapObject
00041 
00042 class PythonFailingComponent:
00043     # Re-use the test interface for this test.
00044     _com_interfaces_ = components.interfaces.nsIPythonTestInterfaceExtra
00045 
00046     def do_boolean(self, p1, p2):
00047         # This should cause the caller to see a "silent" NS_ERROR_FAILURE exception.
00048         raise ServerException()
00049 
00050     def do_octet(self, p1, p2):
00051         # This should cause the caller to see a "silent" NS_ERROR_NOT_IMPLEMENTED exception.
00052         raise ServerException(nsError.NS_ERROR_NOT_IMPLEMENTED)
00053 
00054     def do_short(self, p1, p2):
00055         # This should cause the caller to see a "debug" NS_ERROR_FAILURE exception.
00056         raise COMException(nsError.NS_ERROR_NOT_IMPLEMENTED)
00057 
00058     def do_unsigned_short(self, p1, p2):
00059         # This should cause the caller to see a "debug" NS_ERROR_FAILURE exception.
00060         raise "Foo"
00061 
00062     def do_long(self, p1, p2):
00063         # This should cause the caller to see a "silent" NS_ERROR_FAILURE exception.
00064         raise ServerException
00065 
00066     def do_unsigned_long(self, p1, p2):
00067         # This should cause the caller to see a "silent" NS_ERROR_NOT_IMPLEMENTED exception.
00068         raise ServerException, nsError.NS_ERROR_NOT_IMPLEMENTED
00069 
00070     def do_long_long(self, p1, p2):
00071         # This should cause the caller to see a "silent" NS_ERROR_NOT_IMPLEMENTED exception.
00072         raise ServerException, (nsError.NS_ERROR_NOT_IMPLEMENTED, "testing")
00073 
00074     def do_unsigned_long_long(self, p1, p2):
00075         # Report of a crash in this case - test it!
00076         raise ServerException, "A bad exception param"
00077 
00078 def _testit(expected_errno, func, *args):
00079     try:
00080         apply(func, args)
00081     except COMException, what:
00082         if what.errno != expected_errno:
00083             raise
00084 
00085 def test():
00086     # For the benefit of the test suite, we print some reassuring messages.
00087     import sys
00088     sys.__stderr__.write("***** NOTE: Three tracebacks below this is normal\n")
00089     ob = WrapObject( PythonFailingComponent(), components.interfaces.nsIPythonTestInterfaceExtra)
00090     _testit(nsError.NS_ERROR_FAILURE, ob.do_boolean, 0, 0)
00091     _testit(nsError.NS_ERROR_NOT_IMPLEMENTED, ob.do_octet, 0, 0)
00092     _testit(nsError.NS_ERROR_FAILURE, ob.do_short, 0, 0)
00093     _testit(nsError.NS_ERROR_FAILURE, ob.do_unsigned_short, 0, 0)
00094     _testit(nsError.NS_ERROR_FAILURE, ob.do_long, 0, 0)
00095     _testit(nsError.NS_ERROR_NOT_IMPLEMENTED, ob.do_unsigned_long, 0, 0)
00096     _testit(nsError.NS_ERROR_NOT_IMPLEMENTED, ob.do_long_long, 0, 0)
00097     _testit(nsError.NS_ERROR_FAILURE, ob.do_unsigned_long_long, 0, 0)
00098     print "The xpcom exception tests passed"
00099     # For the benefit of the test suite, some more reassuring messages.
00100     sys.__stderr__.write("***** NOTE: Three tracebacks printed above this is normal\n")
00101     sys.__stderr__.write("*****       It is testing the Python XPCOM Exception semantics\n")
00102 
00103 test()