diff options
author | Damien Garaud <damien.garaud@logilab.fr> | 2012-02-20 10:57:06 +0100 |
---|---|---|
committer | Damien Garaud <damien.garaud@logilab.fr> | 2012-02-20 10:57:06 +0100 |
commit | 6b986a63c48267069b982797fad0e4bf3ba12949 (patch) | |
tree | f50d1a305e7e554b8c673c24b14a1d2ddb7f38e0 | |
parent | 23639ce58d487f1783d12b3507ae9369c70e3739 (diff) | |
download | logilab-common-6b986a63c48267069b982797fad0e4bf3ba12949.tar.gz |
Fix bug in textutils.apply_units, raise an Exception if string does not match (closes #88808).
Update the dedicated unit test.
-rw-r--r-- | ChangeLog | 3 | ||||
-rw-r--r-- | test/unittest_textutils.py | 11 | ||||
-rw-r--r-- | textutils.py | 10 |
3 files changed, 17 insertions, 7 deletions
@@ -1,6 +1,9 @@ ChangeLog for logilab.common ============================ + -- + * texutils: apply_units raise ValueError if string isn'nt valid (closes #88808) + 2011-10-28 -- 0.57.1 * daemon: change $HOME after dropping privileges (closes #81297) diff --git a/test/unittest_textutils.py b/test/unittest_textutils.py index e8b46b1..db18d0d 100644 --- a/test/unittest_textutils.py +++ b/test/unittest_textutils.py @@ -184,6 +184,12 @@ class UnitsTC(TestCase): result = tu.apply_units('1 000 KB', self.units) self.assertEqual(result, 1000 * self.units['kb']) + def test_unit_wrong_input(self): + self.assertRaises(ValueError, tu.apply_units, '', self.units) + 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) + RGX = re.compile('abcd') class PrettyMatchTC(TestCase): @@ -246,9 +252,8 @@ class UnormalizeTC(TestCase): 'ab _ cd') def test_unormalize_backward_compat(self): - self.assertRaises(ValueError, tu.unormalize, u"\u8000", - ignorenonascii=False) - self.assertEqual(tu.unormalize(u"\u8000", ignorenonascii=True), u'') + self.assertRaises(ValueError, tu.unormalize, u"\u8000") + self.assertEqual(tu.unormalize(u"\u8000", substitute=''), u'') class ModuleDocTest(DocTest): diff --git a/textutils.py b/textutils.py index bdeed41..f55c004 100644 --- a/textutils.py +++ b/textutils.py @@ -313,6 +313,8 @@ _BLANK_RE = re.compile(_BLANK_URE) __VALUE_URE = r'-?(([0-9]+\.[0-9]*)|((0x?)?[0-9]+))' __UNITS_URE = r'[a-zA-Z]+' _VALUE_RE = re.compile(r'(?P<value>%s)(?P<unit>%s)?'%(__VALUE_URE, __UNITS_URE)) +_VALIDATION_RE = re.compile(r'^((%s)(%s))*(%s)?$' % (__VALUE_URE, __UNITS_URE, + __VALUE_URE)) BYTE_UNITS = { "b": 1, @@ -352,12 +354,12 @@ def apply_units(string, units, inter=None, final=float, blank_reg=_BLANK_RE, """ if inter is None: inter = final - string = _BLANK_RE.sub('', string) + fstring = _BLANK_RE.sub('', string) + if not (fstring and _VALIDATION_RE.match(fstring)): + raise ValueError("Invalid unit string: %r." % string) values = [] - for match in value_reg.finditer(string): + for match in value_reg.finditer(fstring): dic = match.groupdict() - #import sys - #print >> sys.stderr, dic lit, unit = dic["value"], dic.get("unit") value = inter(lit) if unit is not None: |