Math: Allow 'factorial()' in icalc.

Signed-off-by: James McCoy <jamessan@users.sourceforge.net>
This commit is contained in:
Valentin Lorentz 2012-05-07 17:52:02 +02:00 committed by Daniel Folkinshteyn
parent 27857ff6f8
commit 031b09307e
2 changed files with 5 additions and 2 deletions

View File

@ -103,7 +103,8 @@ class Math(callbacks.Plugin):
_mathEnv['abs'] = abs
_mathEnv['max'] = max
_mathEnv['min'] = min
_mathEnv.pop('factorial')
_mathSafeEnv = dict([(x,y) for x,y in _mathEnv.items()
if x not in ['factorial']])
_mathRe = re.compile(r'((?:(?<![A-Fa-f\d)])-)?'
r'(?:0x[A-Fa-f\d]+|'
r'0[0-7]+|'
@ -192,7 +193,7 @@ class Math(callbacks.Plugin):
text = self._mathRe.sub(handleMatch, text)
try:
self.log.info('evaluating %q from %s', text, msg.prefix)
x = complex(eval(text, self._mathEnv, self._mathEnv))
x = complex(eval(text, self._mathSafeEnv, self._mathSafeEnv))
irc.reply(self._complexToString(x))
except OverflowError:
maxFloat = math.ldexp(0.9999999999999999, 1024)

View File

@ -110,6 +110,7 @@ class MathTestCase(PluginTestCase):
self.assertNotError('calc (1600 * 1200) - 2*(1024*1280)')
self.assertNotError('calc 3-2*4')
self.assertNotError('calc (1600 * 1200)-2*(1024*1280)')
self.assertError('calc factorial(99)')
def testCalcNoNameError(self):
self.assertNotRegexp('calc foobar(x)', 'NameError')
@ -134,6 +135,7 @@ class MathTestCase(PluginTestCase):
self.assertResponse('icalc 1^1', '0')
self.assertResponse('icalc 10**24', '1' + '0'*24)
self.assertRegexp('icalc 49/6', '8.16')
self.assertNotError('icalc factorial(99)')
def testRpn(self):
self.assertResponse('rpn 5 2 +', '7')