Added test for firewall, fixed implementation so it actually works.

This commit is contained in:
Jeremy Fincher 2008-06-16 11:52:37 +00:00 committed by James Vega
parent 9d474f7824
commit 526e71e104
3 changed files with 111 additions and 13 deletions

View File

@ -185,7 +185,7 @@ if __name__ == '__main__':
pluginModule = plugin.loadPluginModule(pluginName) pluginModule = plugin.loadPluginModule(pluginName)
except (ImportError, callbacks.Error), e: except (ImportError, callbacks.Error), e:
sys.stderr.write('Failed to load plugin %s: %s\n' % (pluginName,e)) sys.stderr.write('Failed to load plugin %s: %s\n' % (pluginName,e))
sys.stderr.write('(pluginDirs: %s)' % sys.stderr.write('(pluginDirs: %s)\n' %
conf.supybot.directories.plugins()) conf.supybot.directories.plugins())
continue continue
if hasattr(pluginModule, 'test'): if hasattr(pluginModule, 'test'):

View File

@ -355,24 +355,35 @@ def firewall(f, errorHandler=None):
logException(self) logException(self)
if errorHandler is not None: if errorHandler is not None:
try: try:
errorHandler(self, *args, **kwargs) return errorHandler(self, *args, **kwargs)
except Exception, e: except Exception, e:
logException(self, 'Uncaught exception in errorHandler') logException(self, 'Uncaught exception in errorHandler')
m = utils.python.changeFunctionName(m, f.func_name, f.__doc__) m = utils.python.changeFunctionName(m, f.func_name, f.__doc__)
return m return m
class MetaFirewall(type): class MetaFirewall(type):
def __new__(cls, name, bases, dict): def __new__(cls, name, bases, classdict):
if '__firewalled__' in dict: firewalled = {}
for attr in dict['__firewalled__']: for base in bases:
try: if hasattr(base, '__firewalled__'):
errorHandler = firewalled[attr] cls.updateFirewalled(firewalled, base.__firewalled__)
except: # This is raw here so people can still use tuples. cls.updateFirewalled(firewalled, classdict.get('__firewalled__', []))
errorHandler = None for (attr, errorHandler) in firewalled.iteritems():
dict[attr] = firewall(dict[attr], errorHandler) if attr in classdict:
return super(MetaFirewall, cls).__new__(cls, name, bases, dict) classdict[attr] = firewall(classdict[attr], errorHandler)
#return type.__new__(cls, name, bases, dict) return super(MetaFirewall, cls).__new__(cls, name, bases, classdict)
def getErrorHandler(cls, dictOrTuple, name):
if isinstance(dictOrTuple, dict):
return dictOrTuple[name]
else:
return None
getErrorHandler = classmethod(getErrorHandler)
def updateFirewalled(cls, firewalled, __firewalled__):
for attr in __firewalled__:
firewalled[attr] = cls.getErrorHandler(__firewalled__, attr)
updateFirewalled = classmethod(updateFirewalled)
class PluginLogFilter(logging.Filter): class PluginLogFilter(logging.Filter):

87
test/test_firewall.py Normal file
View File

@ -0,0 +1,87 @@
###
# Copyright (c) 2008, Jeremiah Fincher
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions, and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions, and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the author of this software nor the name of
# contributors to this software may be used to endorse or promote products
# derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###
from supybot.test import *
from supybot import log
class FirewallTestCase(SupyTestCase):
def setUp(self):
log.testing = False
def tearDown(self):
log.testing = True
class C(object):
__metaclass__ = log.MetaFirewall
__firewalled__ = {'foo': None}
class MyException(Exception):
pass
def foo(self):
raise self.MyException()
def testCFooDoesNotRaise(self):
c = self.C()
self.assertEqual(c.foo(), None)
class D(C):
def foo(self):
raise self.MyException()
def testDFooDoesNotRaise(self):
d = self.D()
self.assertEqual(d.foo(), None)
class E(C):
__firewalled__ = {'bar': None}
def foo(self):
raise self.MyException()
def bar(self):
raise self.MyException()
def testEFooDoesNotRaise(self):
e = self.E()
self.assertEqual(e.foo(), None)
def testEBarDoesNotRaise(self):
e = self.E()
self.assertEqual(e.bar(), None)
class F(C):
__firewalled__ = {'bar': lambda self: 2}
def bar(self):
raise self.MyException()
def testFBarReturns2(self):
f = self.F()
self.assertEqual(f.bar(), 2)
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: