1. cherrypy
  2. CherryPy
Issue #5 resolved

Unicode error in CherryTemplate

Anonymous created an issue

==problem: if there is chinese code returned from template,it will raise exception like:

Traceback (most recent call last): File "D:\Python23\lib\site-packages\CherryPy_cpHTTPTools.py", line 138, in doRequest handleRequest(wfile) File "D:\Python23\lib\site-packages\CherryPy_cpHTTPTools.py", line 419, in handleRequest cpg.response.body = func(**(cpg.request.paramMap)) File "D:\Python23\testcherry.py", line 32, in index return renderTemplate(locals(), globals(),open("./formview.html","rb").read()) File "D:\Python23\lib\site-packages\CherryTemplate\CherryTemplate.py", line 442, in renderTemplate exec(template, locals, globals) File "<string>", line 25, in ? UnicodeDecodeError: 'ascii' codec can't decode byte 0xc8 in position 0: ordinal not in range(128)

==solution: remove unicode function seems ok, I dont know if it works for other languages. change f.write(tab+'_page.write(unicode(%s))\n' % evalStr) to f.write(tab+'_page.write(%s)\n' % evalStr)


change return _page.getvalue().encode(encoding, encodingErrors) to return _page.getvalue()

==reason it seems that StringIO cause this problem, we can reproduce the exception in: f = StringIO.StringIO() f.write('\xd6\xd0\xce\xc4') f.write('abcd') f.getvalue() #OK f.write(unicode('abcd')) f.getvalue() #exception raised.

Reported by hyg@21cnhr.gov.cn

Comments (2)

  1. Log in to comment