diff --git a/src/utils.py b/src/utils.py index bf225484e..a09f4a62c 100755 --- a/src/utils.py +++ b/src/utils.py @@ -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] diff --git a/test/test_utils.py b/test/test_utils.py index 830e9f678..cb2f7b178 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -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']