utils: safeunicode error
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
web.py |
Confirmed
|
Undecided
|
Zed A. Shaw |
Bug Description
>>> from web.utils import safeunicode
>>> safeunicode('\xff')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/
return obj.decode(
File "/usr/local/
return codecs.
UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: unexpected code byte
This happens for example when form data is uploaded with binary data and there is some exception raised in POST function handler. No full error stacktrace is displayed on web page, instead python is using 100% cpu. How about this simple patch:
--- utils.py.org 2009-09-23 18:18:01.000000000 +0200
+++ utils.py 2009-09-23 18:15:58.000000000 +0200
@@ -232,7 +232,7 @@
if isinstance(obj, unicode):
return obj
elif isinstance(obj, str):
- return obj.decode(
+ return obj.decode(
else:
if hasattr(obj, '__unicode__'):
return unicode(obj)
Then:
>>> from web.utils import safeunicode
>>> safeunicode('\xff')
u''
>>> safeunicode(
u'helloworld'
That is dangerous. It results in loss of data.
web.input doesn't convert input data to unicode if optional argument _unicode=False is passed. This should be used when there is binary data.