# HG changeset patch
# User Damien Garaud <damien.garaud@logilab.fr>
# Date 1329731826 -3600
# Mon Feb 20 10:57:06 2012 +0100
# Branch stable
# Node ID 5b8c202bdc120a4f0a251bf4dbebc4c4c341baf4
# Parent c8c430118c6e8fb680feeb35ad762cb031853748
Fix bug in textutils.apply_units, raise an Exception if string does not match (closes #88808).
Update the dedicated unit test.
# User Damien Garaud <damien.garaud@logilab.fr>
# Date 1329731826 -3600
# Mon Feb 20 10:57:06 2012 +0100
# Branch stable
# Node ID 5b8c202bdc120a4f0a251bf4dbebc4c4c341baf4
# Parent c8c430118c6e8fb680feeb35ad762cb031853748
Fix bug in textutils.apply_units, raise an Exception if string does not match (closes #88808).
Update the dedicated unit test.
@@ -1,8 +1,11 @@
1 ChangeLog for logilab.common 2 ============================ 3 4 + -- 5 + * texutils: apply_units raise ValueError if string isn'nt valid (closes #88808) 6 + 7 2011-10-28 -- 0.57.1 8 * daemon: change $HOME after dropping privileges (closes #81297) 9 10 * compat: method_type for py3k use instance of the class to have a 11 real instance method (closes: #79268)
@@ -182,10 +182,16 @@
12 13 def test_unit_with_blank(self): 14 result = tu.apply_units('1 000 KB', self.units) 15 self.assertEqual(result, 1000 * self.units['kb']) 16 17 + def test_unit_wrong_input(self): 18 + self.assertRaises(ValueError, tu.apply_units, '', self.units) 19 + self.assertRaises(ValueError, tu.apply_units, 'wrong input', self.units) 20 + self.assertRaises(ValueError, tu.apply_units, 'wrong13 input', self.units) 21 + self.assertRaises(ValueError, tu.apply_units, 'wrong input42', self.units) 22 + 23 RGX = re.compile('abcd') 24 class PrettyMatchTC(TestCase): 25 26 def test_known(self): 27 string = 'hiuherabcdef'
@@ -244,13 +250,12 @@
28 def test_unormalize_substitute(self): 29 self.assertEqual(tu.unormalize(u'ab \u8000 cd', substitute='_'), 30 'ab _ cd') 31 32 def test_unormalize_backward_compat(self): 33 - self.assertRaises(ValueError, tu.unormalize, u"\u8000", 34 - ignorenonascii=False) 35 - self.assertEqual(tu.unormalize(u"\u8000", ignorenonascii=True), u'') 36 + self.assertRaises(ValueError, tu.unormalize, u"\u8000") 37 + self.assertEqual(tu.unormalize(u"\u8000", substitute=''), u'') 38 39 40 class ModuleDocTest(DocTest): 41 """test doc test in this module""" 42 module = tu
@@ -311,10 +311,12 @@
43 _BLANK_URE = r'(\s|,)+' 44 _BLANK_RE = re.compile(_BLANK_URE) 45 __VALUE_URE = r'-?(([0-9]+\.[0-9]*)|((0x?)?[0-9]+))' 46 __UNITS_URE = r'[a-zA-Z]+' 47 _VALUE_RE = re.compile(r'(?P<value>%s)(?P<unit>%s)?'%(__VALUE_URE, __UNITS_URE)) 48 +_VALIDATION_RE = re.compile(r'^((%s)(%s))*(%s)?$' % (__VALUE_URE, __UNITS_URE, 49 + __VALUE_URE)) 50 51 BYTE_UNITS = { 52 "b": 1, 53 "kb": 1024, 54 "mb": 1024 ** 2,
@@ -350,16 +352,16 @@
55 :type value_reg: regexp with "value" and optional "unit" group 56 :param value_reg: match a value and it's unit into the 57 """ 58 if inter is None: 59 inter = final 60 - string = _BLANK_RE.sub('', string) 61 + fstring = _BLANK_RE.sub('', string) 62 + if not (fstring and _VALIDATION_RE.match(fstring)): 63 + raise ValueError("Invalid unit string: %r." % string) 64 values = [] 65 - for match in value_reg.finditer(string): 66 + for match in value_reg.finditer(fstring): 67 dic = match.groupdict() 68 - #import sys 69 - #print >> sys.stderr, dic 70 lit, unit = dic["value"], dic.get("unit") 71 value = inter(lit) 72 if unit is not None: 73 try: 74 value *= units[unit.lower()]