From 69aaea218fe7e05f569752f29cb3663be385f623 Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Fri, 9 Jan 2004 00:13:44 +0000 Subject: [PATCH] Moved base command to Math (from Fun) and added a test for no escaping ValueError. --- plugins/Fun.py | 8 -------- plugins/Math.py | 18 ++++++++++++++++++ test/test_Math.py | 3 +++ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/plugins/Fun.py b/plugins/Fun.py index e95fd65fc..d669858d9 100644 --- a/plugins/Fun.py +++ b/plugins/Fun.py @@ -119,14 +119,6 @@ class Fun(callbacks.Privmsg): except ValueError: irc.error('That number doesn\'t map to an 8-bit character.') - def base(self, irc, msg, args): - """ - - Converts from base the number - """ - (base, number) = privmsgs.getArgs(args, required=2) - irc.reply(str(long(number, int(base)))) - def encode(self, irc, msg, args): """ diff --git a/plugins/Math.py b/plugins/Math.py index 9259a5e20..1984a02c1 100644 --- a/plugins/Math.py +++ b/plugins/Math.py @@ -69,6 +69,24 @@ class Math(callbacks.Privmsg): # Then we delete all square brackets, underscores, and whitespace, so no # one can do list comprehensions or call __...__ functions. ### + def base(self, irc, msg, args): + """ + + Converts from base the number + """ + (base, number) = privmsgs.getArgs(args, required=2) + try: + base = int(base) + if not (2 <= base <= 36): + raise ValueError + except ValueError: + irc.error(' must be a number between 2 and 36.') + return + try: + irc.reply(str(long(number, int(base)))) + except ValueError: + irc.error('Invalid for base %s: %s' % (base, number)) + _mathEnv = {'__builtins__': types.ModuleType('__builtins__'), 'i': 1j} _mathEnv.update(math.__dict__) _mathEnv.update(cmath.__dict__) diff --git a/test/test_Math.py b/test/test_Math.py index a4be517ad..c472e28c5 100644 --- a/test/test_Math.py +++ b/test/test_Math.py @@ -33,6 +33,9 @@ from testsupport import * class MathTestCase(PluginTestCase, PluginDocumentation): plugins = ('Math',) + def testBase(self): + self.assertNotRegexp('base 56 asdflkj', 'ValueError') + def testCalc(self): self.assertResponse('calc 5*0.06', str(5*0.06)) self.assertResponse('calc 2.0-7.0', str(2-7))