调试django发现的新鲜出炉的python mimetype.py的bug.

调试django发现的新鲜出炉的python bug,python  标准库mimetype.py存在bug. 这个bug当注册表中存在中文注册项时会触发UnicodeDecodeError,会导致diango对静态文件的服务异常。如代码中所示,这个bug应该只在windows上存在。本文中的python是当前最新版python2.7.6。


    def read_windows_registry(self, strict=True):
        """
        Load the MIME types database from Windows registry.

        If strict is true, information will be added to
        list of standard types, else to the list of non-standard
        types.
        """

        # Windows only
        if not _winreg:
            return

        def enum_types(mimedb):
            i = 0
            while True:
                try:
                    ctype = _winreg.EnumKey(mimedb, i)
                except EnvironmentError:
                    break
                try:
                    ctype = ctype.encode(default_encoding) # omit in 3.x!
                except UnicodeEncodeError:
                    pass
                else:
                    yield ctype
                i += 1

其中错误处理应改为:

                try:
                    ctype = ctype.encode(default_encoding) # omit in 3.x!
                except UnicodeEncodeError:
                    pass
                except UnicodeDecodeError:
                    pass


你可能感兴趣的:(调试django发现的新鲜出炉的python mimetype.py的bug.)