UnicodeDecodeError:'ascii'编解码器无法解码位置0中的字节0xe0:序号不在范围内(128)

dem*_*mas 34 python django google-app-engine

在我的一台机器上,当我使用谷歌应用程序引擎或django时,我有错误.

例如:

  • 的app.yaml

    application: demas1252c
    version: 1
    runtime: python
    api_version: 1
    
    
    handlers:
       - url: /images
    static_dir: images
       - url: /css
    static_dir: css
       - url: /js
    static_dir: js
       - url: /.*
    script: demas1252c.py
    
    Run Code Online (Sandbox Code Playgroud)
  • demas1252c.py

    import cgi
    import wsgiref.handlers
    
    
    from google.appengine.ext.webapp import template
    from google.appengine.ext import webapp
    
    
    class MainPage(webapp.RequestHandler): 
    def get(self):
    values = {'id' : 10}
    
    
    self.response.out.write(template.render('foto.html', values))
    
    
    application = webapp.WSGIApplication([('/', MainPage)], debug = True)
    wsgiref.handlers.CGIHandler().run(application)
    
    Run Code Online (Sandbox Code Playgroud)
  • foto.html

    <!DOCTYPE html>
    <html lang="en">
        <head></head>
    <body>some</body>
    </html>
    
    Run Code Online (Sandbox Code Playgroud)

错误信息:

C:\artefacts\dev\project>"c:\Program Files\Google\google_appengine\dev_appserver.py" foto-hosting
Traceback (most recent call last):
  File "c:\Program Files\Google\google_appengine\dev_appserver.py", line 69, in <module>
    run_file(__file__, globals())
  File "c:\Program Files\Google\google_appengine\dev_appserver.py", line 65, in run_file
    execfile(script_path, globals_)
  File "c:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver_main.py", line 92, in <module>
    from google.appengine.tools import dev_appserver
  File "c:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 140, in <module>
    mimetypes.add_type(mime_type, '.' + ext)
  File "C:\Python27\lib\mimetypes.py", line 344, in add_type
    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)
Run Code Online (Sandbox Code Playgroud)

当我在django中使用静态文件时(没有gae)我有非常相似的错误(使用不同的堆栈).

我试图找到错误的原因并将代码添加到mimetypes.py:

print '====='
print ctype
ctype = ctype.encode(default_encoding) # omit in 3.x!
Run Code Online (Sandbox Code Playgroud)

然后我在控制台中收到下一条消息:

=====
video/x-ms-wvx
=====
video/x-msvideo
=====
?????/AMR
Traceback (most recent call last):
Run Code Online (Sandbox Code Playgroud)

在注册表中HKCR/Mime/Database/ContentType /我有五个带俄语(cyrilic)字母的键.但是我该如何解决这个错误呢?

bob*_*nce 75

这是mimetypes由注册表中的错误数据触发的错误.(?????/AMR根本不是有效的MIME媒体类型.)

ctype返回的是一个注册表项名称_winreg.EnumKey,它mimetypes希望是一个Unicode字符串,但事实并非如此.不同的是_winreg.QueryValueEx,EnumKey返回一个字节字符串(直接来自ANSI API的Windows API; _winreg在Python 2中,即使它返回Unicode字符串也不使用Unicode接口,因此它永远不会正确读取非ANSI字符).

因此,尝试.encode失败使用Unicode 解码错误试图编码回ASCII之前得到一个Unicode字符串!

try:
    ctype = ctype.encode(default_encoding) # omit in 3.x!
except UnicodeEncodeError:
    pass
Run Code Online (Sandbox Code Playgroud)

mimetypes应该简单地删除这些行.

ETA:添加到bug跟踪器.


new*_*ver 8

顺便说一下,问题的主要原因是QuickTime,它将非ascii mime类型添加到Windows注册表中.解决它的最简单方法是手动查找并从注册表中删除HKCR/Mime/Database/ContentType/?????/和开头的子部分?????/.


Wan*_*gsu 5

有补丁:

http://bugs.python.org/file18143/9291.patch

对我来说很棒.

只需将UnicodeEncodeError替换为UnicodeError即可