Google App Engine, Python2.7的UnicodeDecodeError bug

在跟Web Development,要在Google App Engine上写作业,出师不利,遇到以下bug:

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编码,因此遇上中文、俄文等等其他类型的编码统统会出错。

闯祸的软件会在注册表里填写恼人的不规范的文件类型。俄文的一个典型是QuickTime,而中文则是阿里旺旺

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个:

1. 修改mimetypes.py(有很多种修改办法,这里贴其中一种,添加如#<--标识的2行即可)

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

或者:

2. 在Python安装目录下的Lib\site-packages文件夹创建sitecustomize.py文件,内容为:

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

完。

你可能感兴趣的:(python2.7)