Fix casting of int/float to str or unicode
This commit is contained in:
parent
2e8be9ec6b
commit
df6cb5718a
1 changed files with 8 additions and 2 deletions
|
@ -328,9 +328,15 @@ def cast(func, value):
|
||||||
if func == bool:
|
if func == bool:
|
||||||
return bool(int(value))
|
return bool(int(value))
|
||||||
elif func == unicode:
|
elif func == unicode:
|
||||||
return value.decode('utf-8')
|
if isinstance(value, (int, long, float)):
|
||||||
|
return unicode(value)
|
||||||
|
else:
|
||||||
|
return value.decode('utf-8')
|
||||||
elif func == str:
|
elif func == str:
|
||||||
return value.encode('utf-8')
|
if isinstance(value, (int, long, float)):
|
||||||
|
return str(value)
|
||||||
|
else:
|
||||||
|
return value.encode('utf-8')
|
||||||
elif func in (int, float):
|
elif func in (int, float):
|
||||||
try:
|
try:
|
||||||
return func(value)
|
return func(value)
|
||||||
|
|
Loading…
Reference in a new issue