Fixed bug found by G-LiTe and added a test.

This commit is contained in:
Jeremy Fincher 2003-10-13 03:47:33 +00:00
parent 68a84e17b0
commit 6c3e193b93
2 changed files with 7 additions and 1 deletions

View File

@ -127,6 +127,7 @@ class Math(callbacks.Privmsg):
text = privmsgs.getArgs(args) text = privmsgs.getArgs(args)
text = text.translate(string.ascii, '_[] \t') text = text.translate(string.ascii, '_[] \t')
text = text.replace('lambda', '') text = text.replace('lambda', '')
#debug.printf(text)
def handleMatch(m): def handleMatch(m):
s = m.group(1) s = m.group(1)
if s.startswith('0x'): if s.startswith('0x'):
@ -138,8 +139,12 @@ class Math(callbacks.Privmsg):
i = int(s) i = int(s)
else: else:
i = float(s) i = float(s)
return str(complex(i)) x = complex(i)
if x == abs(x):
x = abs(x)
return str(x)
text = self._mathRe.sub(handleMatch, text) text = self._mathRe.sub(handleMatch, text)
#debug.printf(text)
try: try:
x = complex(eval(text, self._mathEnv, self._mathEnv)) x = complex(eval(text, self._mathEnv, self._mathEnv))
irc.reply(msg, self._complexToString(x)) irc.reply(msg, self._complexToString(x))

View File

@ -42,6 +42,7 @@ class MathTestCase(PluginTestCase, PluginDocumentation):
self.assertResponse('calc -((-5)**.5)', '-2.2360679775i') self.assertResponse('calc -((-5)**.5)', '-2.2360679775i')
self.assertNotRegexp('calc [9, 5] + [9, 10]', 'TypeError') self.assertNotRegexp('calc [9, 5] + [9, 10]', 'TypeError')
self.assertError('calc [9, 5] + [9, 10]') self.assertError('calc [9, 5] + [9, 10]')
self.assertNotError('calc degrees(2)')
def testRpn(self): def testRpn(self):
self.assertResponse('rpn 5 2 +', '7') self.assertResponse('rpn 5 2 +', '7')