win7 安装 webpy 0.3x 出现 UnicodeDecodeError,解决方法

原文地址:

http://bbs.csdn.net/topics/390654855?page=1#post-396875208



本人 windows 7 下安装 webpy 0.3x 版本的时候出现如下的错误提示:

>>python setup.py install

 File "setup.py", line 6, in <module>
    from web import __version__
  File "C:\Python27\web.py-0.37\web\__init__.py", line 14, in <module>
    import utils, db, net, wsgi, http, webapi, httpserver, debugerror
  File "C:\Python27\web.py-0.37\web\wsgi.py", line 12, in <module>
    import httpserver
  File "C:\Python27\web.py-0.37\web\httpserver.py", line 4, in <module>
    from SimpleHTTPServer import SimpleHTTPRequestHandler
  File "C:\Python27\lib\SimpleHTTPServer.py", line 27, in <module>
    class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
  File "C:\Python27\lib\SimpleHTTPServer.py", line 208, in SimpleHTTPRequestHand
ler
    mimetypes.init() # try to read system mime.types
  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 0xc4 in position 33: ordinal
 not in range(128)



根据最后一个提示:
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 0xc4 in position 33: ordinal
    not in range(128)

对文件 "c:\Python27\lib\mimetypes.py" 以默认编码 ascii 方式读取出错(位置0xc4)。
更换文件的编码方式为 'gbk',成功。



方法: 在 "c:\Python27\lib\mimetypes.py" 的 import ... 语句下面添加如下代码

Python code
?
1
2
3
4
5
# begin added by semon for 'UnicodeDecodeError'
if  sys.getdefaultencoding() ! =  'gbk' :
     reload (sys)
     sys.setdefaultencoding( 'gbk' )
# end added by semon


保存,退出。
再次执行 
>>python setup.py install 
成功。



尝试过将编码改为'utf-8',也失败了。不知道为什么。难道mimetypes.py中有中文不成,但我看了一下确实没有。

这是一个bug:http://bugs.python.org/issue10490
patch地址:http://bugs.python.org/file19746/mimetypes-patch2-2.7.patch

Python code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Index: release27 - maint / Lib / mimetypes.py
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
- - -  release27 - maint / Lib / mimetypes.py    (revision  86639 )
+ + +  release27 - maint / Lib / mimetypes.py    (working copy)
@@  - 245 , 12  + 245 , 7  @@
                      ctype  =  _winreg.EnumKey(mimedb, i)
                  except  EnvironmentError:
                      break
-                 try :
-                     ctype  =  ctype.encode(default_encoding)  # omit in 3.x!
-                 except  UnicodeEncodeError:
-                     pass
-                 else :
-                     yield  ctype
+                 yield  ctype
                  + =  1
  
          default_encoding  =  sys.getdefaultencoding()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值