diff options
| author | Joe Gordon <jogo@cloudscaling.com> | 2012-04-13 15:12:04 -0400 |
|---|---|---|
| committer | Joe Gordon <jogo@cloudscaling.com> | 2012-05-07 16:11:24 -0700 |
| commit | c95162e52899618fc269fb536f6a2d3b26b7794d (patch) | |
| tree | 7834ddc39b7cd83ae2ca0a92db170feb318f4c23 /nova/api | |
| parent | 4e38b78f55850916904efbe2c79472659f5594a0 (diff) | |
Fix bug 983206 : _try_convert parsing string
* _try_convert in ec2utils.py didn't handle strings starting with "0x"
* Added tests to cover bug
* Add better float support
* remove unused complex number support
Change-Id: I382d36f4a8671bcceccfa1ebdbae89a9d2aca207
Diffstat (limited to 'nova/api')
| -rw-r--r-- | nova/api/ec2/ec2utils.py | 35 |
1 files changed, 11 insertions, 24 deletions
diff --git a/nova/api/ec2/ec2utils.py b/nova/api/ec2/ec2utils.py index 045ee12d3..9006da93e 100644 --- a/nova/api/ec2/ec2utils.py +++ b/nova/api/ec2/ec2utils.py @@ -220,6 +220,10 @@ def _try_convert(value): * try conversion to int, float, complex, fallback value """ + def _negative_zero(value): + epsilon = 1e-7 + return 0 if abs(value) < epsilon else value + if len(value) == 0: return '' if value == 'None': @@ -229,31 +233,14 @@ def _try_convert(value): return True if lowered_value == 'false': return False - valueneg = value[1:] if value[0] == '-' else value - if valueneg == '0': - return 0 - if valueneg == '': - return value - if valueneg[0] == '0': - if valueneg[1] in 'xX': - return int(value, 16) - elif valueneg[1] in 'bB': - return int(value, 2) - else: - try: - return int(value, 8) - except ValueError: - pass - try: - return int(value) - except ValueError: - pass - try: - return float(value) - except ValueError: - pass + for prefix, base in [('0x', 16), ('0b', 2), ('0', 8), ('', 10)]: + try: + if lowered_value.startswith((prefix, "-" + prefix)): + return int(lowered_value, base) + except ValueError: + pass try: - return complex(value) + return _negative_zero(float(value)) except ValueError: return value |
