Commit 29371c26 authored by 's avatar

Merged better number conversion error messages from 2.2 branch

parent e34591e0
...@@ -82,7 +82,7 @@ ...@@ -82,7 +82,7 @@
# attributions are listed in the accompanying credits file. # attributions are listed in the accompanying credits file.
# #
############################################################################## ##############################################################################
__version__='$Revision: 1.8 $'[11:-2] __version__='$Revision: 1.9 $'[11:-2]
import regex import regex
from string import atoi, atol, atof, join, split, strip from string import atoi, atol, atof, join, split, strip
...@@ -119,8 +119,12 @@ def field2int(v): ...@@ -119,8 +119,12 @@ def field2int(v):
return map(field2int, v) return map(field2int, v)
if hasattr(v,'read'): v=v.read() if hasattr(v,'read'): v=v.read()
else: v=str(v) else: v=str(v)
# we can remove the check for an empty string when we go to python 1.4 if v:
if v: return atoi(v) try: return atoi(v)
except ValueError:
raise ValueError, (
"An integer was expected in the value '%s'" % v
)
raise ValueError, 'Empty entry when <strong>integer</strong> expected' raise ValueError, 'Empty entry when <strong>integer</strong> expected'
def field2float(v): def field2float(v):
...@@ -128,8 +132,12 @@ def field2float(v): ...@@ -128,8 +132,12 @@ def field2float(v):
return map(field2float, v) return map(field2float, v)
if hasattr(v,'read'): v=v.read() if hasattr(v,'read'): v=v.read()
else: v=str(v) else: v=str(v)
# we can remove the check for an empty string when we go to python 1.4 if v:
if v: return atof(v) try: return atof(v)
except ValueError:
raise ValueError, (
"A floating-point number was expected in the value '%s'" % v
)
raise ValueError, ( raise ValueError, (
'Empty entry when <strong>floating-point number</strong> expected') 'Empty entry when <strong>floating-point number</strong> expected')
...@@ -142,7 +150,12 @@ def field2long(v): ...@@ -142,7 +150,12 @@ def field2long(v):
# handle trailing 'L' if present. # handle trailing 'L' if present.
if v[-1:] in ('L', 'l'): if v[-1:] in ('L', 'l'):
v = v[:-1] v = v[:-1]
if v: return atol(v) if v:
try: return atol(v)
except ValueError:
raise ValueError, (
"A long integer was expected in the value '%s'" % v
)
raise ValueError, 'Empty entry when <strong>integer</strong> expected' raise ValueError, 'Empty entry when <strong>integer</strong> expected'
def field2tokens(v): def field2tokens(v):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment