« 吉里吉里Z Ver1.0 リリース | メイン | メモリ断片化低減対策 »

2014年01月01日

Misc.:: Python 2.7 で ez_setup.py で出るエラーを回避する

    

Python 2.7.6 で python ez_setup.py を実行すると以下のようなエラーが出る。

  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 0x83 in position 0: ordinal
not in range(128)
Something went wrong during the installation.
See the error message above.

該当個所見ると、どうもレジストリからの読込みで2バイト文字が混じっているとエラーになる様子。
検索するとレジストリの方を削除する対応方法などが見つかるが、それは明らかにまずい。
スクリプトを見てみると、# omit in 3.x!と書かれている。
Python 3.3 を入れて、Python 3.3 で ez_setup.py を実行すれば問題なく終わるが、Python 2.7 系でないとエラーが出るものがあるので、Python 2.7 を入れたい。
3.3 の Lib\mimetypes.py の read_windows_registry を見てみると、エンコード周りの処理がない。
と言うことで、2.7 の mimetypes.py も同じように修正してしまう。
228行目から対応する箇所をコメントアウトするとこんな感じ。

    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

#        default_encoding = sys.getdefaultencoding()
        with _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, '') as hkcr:
            for subkeyname in enum_types(hkcr):
                try:
                    with _winreg.OpenKey(hkcr, subkeyname) as subkey:
                        # Only check file extensions
                        if not subkeyname.startswith("."):
                            continue
                        # raises EnvironmentError if no 'Content Type' value
                        mimetype, datatype = _winreg.QueryValueEx(
                            subkey, 'Content Type')
                        if datatype != _winreg.REG_SZ:
                            continue
#                        try:
#                            mimetype = mimetype.encode(default_encoding)
#                            subkeyname = subkeyname.encode(default_encoding)
#                        except UnicodeEncodeError:
#                            continue
                        self.add_type(mimetype, subkeyname, strict)
                except EnvironmentError:
                    continue

これでエラーは出ずに ez_setup.py が実行できてインストールできた。



投稿者 Takenori : 2014年01月01日 19:21




comments powered by Disqus
Total : Today : Yesterday : なかのひと