Permalink
Browse files

Math: Allow 'factorial()' in icalc.

  • Loading branch information...
ProgVal committed May 7, 2012
1 parent 695078e commit 1f60a9487ca4114f040135fb14cabc155a041918
Showing with 5 additions and 2 deletions.
  1. +3 −2 plugins/Math/plugin.py
  2. +2 −0 plugins/Math/test.py
View
@@ -106,7 +106,8 @@ def _sqrt(x):
_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]+|'
@@ -196,7 +197,7 @@ def handleMatch(m):
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
@@ -110,6 +110,7 @@ def testCalc(self):
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 @@ def testICalc(self):
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')

0 comments on commit 1f60a94

Please sign in to comment.