summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--logilab/common/textutils.py4
-rw-r--r--test/unittest_textutils.py3
2 files changed, 5 insertions, 2 deletions
diff --git a/logilab/common/textutils.py b/logilab/common/textutils.py
index 356b1a8..95a5e00 100644
--- a/logilab/common/textutils.py
+++ b/logilab/common/textutils.py
@@ -371,8 +371,8 @@ def apply_units(string, units, inter=None, final=float, blank_reg=_BLANK_RE,
try:
value *= units[unit.lower()]
except KeyError:
- raise KeyError('invalid unit %s. valid units are %s' %
- (unit, units.keys()))
+ raise ValueError('invalid unit %s. valid units are %s' %
+ (unit, units.keys()))
values.append(value)
return final(sum(values))
diff --git a/test/unittest_textutils.py b/test/unittest_textutils.py
index 330d49c..2fa648a 100644
--- a/test/unittest_textutils.py
+++ b/test/unittest_textutils.py
@@ -190,6 +190,9 @@ class UnitsTC(TestCase):
self.assertRaises(ValueError, tu.apply_units, 'wrong input', self.units)
self.assertRaises(ValueError, tu.apply_units, 'wrong13 input', self.units)
self.assertRaises(ValueError, tu.apply_units, 'wrong input42', self.units)
+ with self.assertRaises(ValueError) as cm:
+ tu.apply_units('42 cakes', self.units)
+ self.assertIn('invalid unit cakes.', str(cm.exception))
RGX = re.compile('abcd')
class PrettyMatchTC(TestCase):