Have exnToString handle exceptions whose str representation is empty.

This commit is contained in:
Jeremy Fincher 2004-11-04 06:01:17 +00:00
parent 040888405c
commit 58b3268bdd
2 changed files with 15 additions and 1 deletions

View File

@ -537,7 +537,11 @@ def safeEval(s, namespace={'True': True, 'False': False, 'None': None}):
def exnToString(e): def exnToString(e):
"""Turns a simple exception instance into a string (better than str(e))""" """Turns a simple exception instance into a string (better than str(e))"""
return '%s: %s' % (e.__class__.__name__, e) strE = str(e)
if strE:
return '%s: %s' % (e.__class__.__name__, strE)
else:
return e.__class__.__name__
class IterableMap(object): class IterableMap(object):
"""Define .iteritems() in a class and subclass this to get the other iters. """Define .iteritems() in a class and subclass this to get the other iters.

View File

@ -33,6 +33,16 @@ import sets
import supybot.utils as utils import supybot.utils as utils
class UtilsTest(SupyTestCase): class UtilsTest(SupyTestCase):
def testExnToString(self):
try:
raise KeyError, 1
except Exception, e:
self.assertEqual(utils.exnToString(e), 'KeyError: 1')
try:
raise EOFError
except Exception, e:
self.assertEqual(utils.exnToString(e), 'EOFError')
def testMatchCase(self): def testMatchCase(self):
f = utils.matchCase f = utils.matchCase
self.assertEqual('bar', f('foo', 'bar')) self.assertEqual('bar', f('foo', 'bar'))