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 /textutils.py | |
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.
Diffstat (limited to 'textutils.py')
-rw-r--r-- | textutils.py | 10 |
1 files changed, 6 insertions, 4 deletions
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: |