diff --git a/plugins/Math.py b/plugins/Math.py index d88e7e44a..01ce0fb20 100644 --- a/plugins/Math.py +++ b/plugins/Math.py @@ -152,6 +152,8 @@ class Math(callbacks.Privmsg): irc.error(msg, 'Go get scanez, this is a *real* math problem!') except TypeError: irc.error(msg, 'Something in there wasn\'t a valid number.') + except NameError, e: + irc.error(msg, '%s is not a defined function.' % str(e).split()[1]) except Exception, e: irc.error(msg, debug.exnToString(e)) @@ -194,7 +196,11 @@ class Math(callbacks.Privmsg): arg2 = stack.pop() arg1 = stack.pop() s = '%s%s%s' % (arg1, arg, arg2) - stack.append(eval(s, self._mathEnv, self._mathEnv)) + try: + stack.append(eval(s, self._mathEnv, self._mathEnv)) + except SyntaxError: + irc.error(msg, '%r is not a defined function.' % arg) + return if len(stack) == 1: irc.reply(msg, str(self._complexToString(complex(stack[0])))) else: diff --git a/test/test_Math.py b/test/test_Math.py index b0df510e1..564a279e9 100644 --- a/test/test_Math.py +++ b/test/test_Math.py @@ -47,12 +47,18 @@ class MathTestCase(PluginTestCase, PluginDocumentation): ## self.assertNotError('calc log(8,2)') ## self.assertNotError('calc log(8,2)') + def testCalcNoNameError(self): + self.assertNotRegexp('calc foobar(x)', 'NameError') + def testRpn(self): self.assertResponse('rpn 5 2 +', '7') self.assertResponse('rpn 1 2 3 +', 'Stack: [1, 5]') self.assertResponse('rpn 1 dup', 'Stack: [1, 1]') self.assertResponse('rpn 2 3 4 + -', str(2-7)) self.assertNotError('rpn 2 degrees') + + def testRpmNoSyntaxError(self): + self.assertNotRegexp('rpn 2 3 foobar', 'SyntaxError') def testConvert(self): self.assertResponse('convert 1 m to cm', '100.0 cm')