Fix casting to int silently failing
This commit is contained in:
parent
a46cb731cf
commit
d96ac192d1
1 changed files with 10 additions and 1 deletions
|
@ -292,7 +292,16 @@ def cast(func, value):
|
||||||
return value
|
return value
|
||||||
else:
|
else:
|
||||||
return value.encode('utf-8')
|
return value.encode('utf-8')
|
||||||
elif func in (int, float):
|
elif func == int:
|
||||||
|
try:
|
||||||
|
return func(value)
|
||||||
|
except ValueError:
|
||||||
|
try:
|
||||||
|
# Converting e.g. '8.0' fails; need to convert to float first
|
||||||
|
return int(float(value))
|
||||||
|
except ValueError:
|
||||||
|
return float('nan')
|
||||||
|
elif func == float:
|
||||||
try:
|
try:
|
||||||
return func(value)
|
return func(value)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
|
Loading…
Reference in a new issue