Google App Engine, Python2.7的UnicodeDecodeError bug

在跟Web Development,要在Google App Engine上寫做業,出師不利,遇到如下bug:python

2014-05-06 16:14:17 Running command: "['C:\\Python27\\python.exe', 'C:\\Program Files (x86)\\Google\\google_appengine\\dev_appserver.py', '--skip_sdk_update_check=yes', '--port=8080', '--admin_port=8000', 'E:\\GoogleAppEngine\\hello_udacity']"
Traceback (most recent call last):
  File "C:\Program Files (x86)\Google\google_appengine\dev_appserver.py", line 82, in <module>
    _run_file(__file__, globals())
  File "C:\Program Files (x86)\Google\google_appengine\dev_appserver.py", line 78, in _run_file
    execfile(_PATHS.script_file(script_name), globals_)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\devappserver2.py", line 33, in <module>
    from google.appengine.tools.devappserver2.admin import admin_server
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\admin\admin_server.py", line 29, in <module>
    from google.appengine.tools.devappserver2.admin import console
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\admin\console.py", line 22, in <module>
    from google.appengine.tools.devappserver2 import module
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\module.py", line 49, in <module>
    from google.appengine.tools.devappserver2 import gcs_server
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\gcs_server.py", line 29, in <module>
    from google.appengine.tools.devappserver2 import wsgi_server
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\wsgi_server.py", line 31, in <module>
    from cherrypy import wsgiserver
  File "C:\Program Files (x86)\Google\google_appengine\lib\cherrypy\cherrypy\__init__.py", line 70, in <module>
    from cherrypy import _cptools
  File "C:\Program Files (x86)\Google\google_appengine\lib\cherrypy\cherrypy\_cptools.py", line 245, in <module>
    from cherrypy.lib import cptools, encoding, auth, static, jsontools
  File "C:\Program Files (x86)\Google\google_appengine\lib\cherrypy\cherrypy\lib\static.py", line 7, in <module>
    mimetypes.init()
  File "C:\Python27\lib\mimetypes.py", line 358, in init
    db.read_windows_registry()
  File "C:\Python27\lib\mimetypes.py", line 258, in read_windows_registry
    for subkeyname in enum_types(hkcr):
  File "C:\Python27\lib\mimetypes.py", line 249, in enum_types
    ctype = ctype.encode(default_encoding) # omit in 3.x!
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in position 1: ordinal not in range(128)
2014-05-06 16:14:25 (Process exited with code 1)

 

經查,此爲Python2.7的一個bug。在讀取windows註冊表中的文件類型列表的時候,Python默認讀到的數據爲Latin-1編碼,所以趕上中文、俄文等等其餘類型的編碼通通會出錯。web

闖禍的軟件會在註冊表裏填寫惱人的不規範的文件類型。俄文的一個典型是QuickTime,而中文則是阿里旺旺json

On Windows, mimetypes initialization reads the list of MIME types from the Windows registry. It assumes that all characters are Latin-1 encoded, and fails when it's not the case, with the following exception:

Traceback (most recent call last):
  File "mttest.py", line 3, in <module>
    mimetypes.init()
  File "c:\Python27\lib\mimetypes.py", line 355, in init
    db.read_windows_registry()
  File "c:\Python27\lib\mimetypes.py", line 260, in read_windows_registry
    for ctype in enum_types(mimedb):
  File "c:\Python27\lib\mimetypes.py", line 250, in enum_types
    ctype = ctype.encode(default_encoding) # omit in 3.x!
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position 0: ordinal not in range(128)

This can be reproduced, for example, on a Russian Windows XP installation which has QuickTime installed (QuickTime creates the non-Latin entries in the registry). The following line causes the exception to happen:

import mimetypes; mimetypes.init()

解決辦法各類各樣,下面說2個:windows

1. 修改mimetypes.py(有不少種修改辦法,這裏貼其中一種,添加如#<--標識的2行便可)app

Alternative temporary solution def enum_types(mimedb): .... try: ctype = ctype.encode(default_encoding) # omit in 3.x!
except UnicodeEncodeError: pass
except Exception: #<--
  pass            #<--
else: yield ctype

或者:ui

2. 在Python安裝目錄下的Lib\site-packages文件夾建立sitecustomize.py文件,內容爲:google

import sys sys.setdefaultencoding('cp936')

此外還能夠清理註冊表中HKEY_CLASSES_ROOT下全部非ascii字符的鍵。

不用費那勁了,直接下載修正的mimetypes.py覆蓋原文件便可。編碼

http://yunpan.cn/QiZRib89b5h8X (提取碼:eacf)

連接:http://pan.baidu.com/share/link?shareid=4126899422&uk=1879334133 密碼:uhcw

完。spa

相關文章
相關標籤/搜索