2011-04-18 36 views
0

Possible Duplicate:
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) 
+4

-1不讀取錯誤信息:'UnicodeDecodeError:'ascii'編碼解碼器無法解碼位置3中的字節0xc3:序號不在範圍內(128)'您的輸入數據有問題,或者至少看起來像所以。 – Bobby 2011-04-18 10:14:34

+1

http://stackoverflow.com/questions/3570434/unicodedecodeerror-when-passing-get-data-in-python-appengine – Andrey 2011-04-18 10:16:28

回答

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/usr/lib/python2.6

這裏的混合非ASCII strunicode的例子:

>>> 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) 
>>> 

然後你可以運行你的應用程序,看看什麼是buflist:哪些部分是你寫數據,並且通過設置GAE。您需要查看GAE文檔以查看它是否預計str內容(使用什麼編碼?)或unicode內容,並相應地調整您的代碼。

+0

感謝您的有用答案。現在起作用了。 – 2011-04-21 22:53:07

相關問題