是什么引起了这次追溯?

时间:2011-04-18 10:12:09

标签: python django google-app-engine django-forms

  

可能重复:
  UnicodeDecodeError when passing GET data in Python/AppEngine

在尝试提交表单时,我在本地和生产中获得以下回溯。你能解释一下我应该在哪里或者我应该开始编写调试语句来查看代码中发生异常的位置吗?

 -->  --> -->

Traceback (most recent call last):
  File "/media/Lexar/montao/google/appengine/tools/dev_appserver.py", line 3858, in _HandleRequest
    self._Dispatch(dispatcher, self.rfile, outfile, env_dict)
  File "/media/Lexar/montao/google/appengine/tools/dev_appserver.py", line 3792, in _Dispatch
    base_env_dict=env_dict)
  File "/media/Lexar/montao/google/appengine/tools/dev_appserver.py", line 580, in Dispatch
    base_env_dict=base_env_dict)
  File "/media/Lexar/montao/google/appengine/tools/dev_appserver.py", line 2918, in Dispatch
    self._module_dict)
  File "/media/Lexar/montao/google/appengine/tools/dev_appserver.py", line 2822, in ExecuteCGI
    reset_modules = exec_script(handler_path, cgi_path, hook)
  File "/media/Lexar/montao/google/appengine/tools/dev_appserver.py", line 2704, in ExecuteOrImportScript
    script_module.main()
  File "/media/Lexar/montao/classifiedsmarket/main.py", line 2497, in main
    util.run_wsgi_app(application)
  File "/media/Lexar/montao/google/appengine/ext/webapp/util.py", line 98, in run_wsgi_app
    run_bare_wsgi_app(add_wsgi_middleware(application))
  File "/media/Lexar/montao/google/appengine/ext/webapp/util.py", line 116, in run_bare_wsgi_app
    result = application(env, _start_response)
  File "/media/Lexar/montao/google/appengine/ext/webapp/__init__.py", line 655, in __call__
    response.wsgi_write(start_response)
  File "/media/Lexar/montao/google/appengine/ext/webapp/__init__.py", line 274, in wsgi_write
    body = self.out.getvalue()
  File "/usr/lib/python2.6/StringIO.py", line 270, in getvalue
    self.buf += ''.join(self.buflist)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 3: ordinal not in range(128)

1 个答案:

答案 0 :(得分:5)

错误表现在/usr/lib/python2.6/StringIO.py中,即Python StringIO模块。我们不需要过多地阅读该源文件(第49行)以找到此警告:

The StringIO object can accept either Unicode or 8-bit strings, but
mixing the two may take some care. If both are used, 8-bit strings that
cannot be interpreted as 7-bit ASCII (that use the 8th bit) will
cause a UnicodeError to be raised when getvalue() is called.

宾果!并且在getvalue()方法中再次重复警告。请注意,警告是古老的;它提到了UnicodeError而不是UnicodeDecodeError,但是你得到了漂移。

我建议修补模块,以便在发生错误时显示包中的内容。在第270行总结有问题的陈述,如下所示:

if self.buflist:
    try:
        self.buf += ''.join(self.buflist)
    except UnicodeDecodeError:
        import sys
        print >> sys.stderr, "*** error context: buf=%r buflist=%r" % (self.buf, self.buflist)
        raise
    self.buflist = []
return self.buf

如果修补原位一个Python提供的模块的想法你触目惊心,把补丁版本的目录中早期在sys.path {超过{1}

以下是混合非ASCII /usr/lib/python2.6str

的示例
unicode

然后,您可以运行您的应用程序并查看>>> from StringIO import StringIO >>> f = StringIO() >>> f.write('ascii') >>> f.write(u'\u1234'.encode('utf8')) >>> f.write(u'\u5678') >>> f.getvalue() *** error context: buf='' buflist=['ascii', '\xe1\x88\xb4', u'\u5678'] Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\python26\lib\StringIO.py", line 271, in getvalue self.buf += ''.join(self.buflist) UnicodeDecodeError: 'ascii' codec can't decode byte 0xe1 in position 0: ordinal not in range(128) >>> 中的内容:哪些部分是您编写的数据,哪些是GAE提供的。你需要看GAE文档,看看它是否期待buflist内容(用什么编码?)或str内容,并相应地调整你的代码。