diff options
author | Pierre-Yves David <pierre-yves.david@logilab.fr> | 2010-01-18 16:52:51 +0100 |
---|---|---|
committer | Pierre-Yves David <pierre-yves.david@logilab.fr> | 2010-01-18 16:52:51 +0100 |
commit | d23898fc964c40bb61cc699bd6cda0a3797b7c25 (patch) | |
tree | 32debd9123531b142afd8bc86e45ea225e5b88e3 | |
parent | b96951d6a140b92d033a8d9eb53614e22e30c2c5 (diff) | |
download | logilab-common-d23898fc964c40bb61cc699bd6cda0a3797b7c25.tar.gz |
[fix] Remove isinstance call for bytes units
The isinstance function was used to detect if a byte required unit application,
This was leading to error when the value was a float. isinstance call is
replaced by a more pythonix hasattribut __init__ check/
-rw-r--r-- | configuration.py | 4 | ||||
-rw-r--r-- | optik_ext.py | 2 | ||||
-rw-r--r-- | textutils.py | 6 |
3 files changed, 8 insertions, 4 deletions
diff --git a/configuration.py b/configuration.py index 6172c5c..6d03af1 100644 --- a/configuration.py +++ b/configuration.py @@ -311,8 +311,8 @@ def format_option_value(optdict, value): value = "'%s'" % value elif optdict.get('type') == 'time' and isinstance(value, (float, int, long)): value = "%ss" % value - elif optdict.get('type') == 'bytes' and isinstance(value, (int, long)): - value = "%sB" % value + elif optdict.get('type') == 'bytes' and hasattr(value, '__int__'): + value = "%iB" % value return value def ini_format_section(stream, section, options, encoding=None, doc=None): diff --git a/optik_ext.py b/optik_ext.py index 34dbfe9..86c2ffd 100644 --- a/optik_ext.py +++ b/optik_ext.py @@ -176,7 +176,7 @@ def check_time(option, opt, value): def check_bytes(option, opt, value): from logilab.common.textutils import BYTE_UNITS, apply_units - if instance(value, (int, long)): + if hasattr(value, '__int__'): return value return apply_units(value, BYTE_UNITS) diff --git a/textutils.py b/textutils.py index 38f59c7..37d4f48 100644 --- a/textutils.py +++ b/textutils.py @@ -289,7 +289,11 @@ def apply_units( string, units, inter=None, final=float, blank_reg=_BLANK_RE, lit, unit = dic["value"], dic.get("unit") value = inter(lit) if unit is not None: - value *= units[unit] + try: + value *= units[unit] + except KeyError: + raise KeyError('invalid unit %s. valid units are %s' % + unit, units.keys()) values.append(value) return final(sum(values)) |