Raise ValueError in the case of bad input.

This commit is contained in:
Jeremy Fincher 2003-09-08 06:10:06 +00:00
parent 66b62a42cd
commit 6aa3e06402
2 changed files with 6 additions and 2 deletions

View File

@ -175,10 +175,11 @@ _soundextrans = string.maketrans(string.ascii_uppercase,
_notUpper = string.ascii.translate(string.ascii, string.ascii_uppercase)
def soundex(s, length=4):
"""Returns the soundex hash of a given string."""
assert s
s = s.upper() # Make everything uppercase.
firstChar = s[0] # Save the first character.
s = s.translate(string.ascii, _notUpper) # Delete non-letters.
if not s:
raise ValueError, 'Invalid string for soundex: %s'
firstChar = s[0] # Save the first character.
s = s.translate(_soundextrans) # Convert to soundex numbers.
s = s.lstrip(s[0]) # Remove all repeated first characters.
L = [firstChar]

View File

@ -98,6 +98,9 @@ class UtilsTest(unittest.TestCase):
soundex = utils.soundex(name)
self.assertEqual(soundex, key,
'%s was %s, not %s' % (name, soundex, key))
self.assertRaises(ValueError, utils.soundex, '3')
self.assertRaises(ValueError, utils.soundex, "'")
def testDQRepr(self):
L = ['foo', 'foo\'bar', 'foo"bar', '"', '\\', '', '\x00']