質問

さて、今日、私はPythonのHashlibモジュールをチェックしていましたが、それから私はまだ理解できないものを見つけました。

このPythonモジュール内には、従うことができないインポートがあります。私はこのようになります:

def __get_builtin_constructor(name):
    if name in ('SHA1', 'sha1'):
        import _sha
        return _sha.new

Pythonシェルから_shaモジュールをインポートしようとしましたが、そのように到達することはできないようです。最初の推測では、それはCモジュールだと思いますが、わかりません。

だから、みんな教えてください、あなたはそのモジュールがどこにあるか知っていますか?彼らはどのようにそれをインポートしますか?

役に立ちましたか?

解決

実際、_SHAモジュールはshamodule.cによって提供され、_md5はmd5module.cおよびmd5.cによって提供され、両方ともPythonがデフォルトでOpenSSLでコンパイルされていない場合にのみ構築されます。

詳細を見つけることができます setup.py PythonソースTarballで。

    if COMPILED_WITH_PYDEBUG or not have_usable_openssl:
        # The _sha module implements the SHA1 hash algorithm.
        exts.append( Extension('_sha', ['shamodule.c']) )
        # The _md5 module implements the RSA Data Security, Inc. MD5
        # Message-Digest Algorithm, described in RFC 1321.  The
        # necessary files md5.c and md5.h are included here.
        exts.append( Extension('_md5',
                        sources = ['md5module.c', 'md5.c'],
                        depends = ['md5.h']) )

ほとんどの場合、PythonはOpenSSLライブラリで構築されており、その場合、それらの機能はOpenSSLライブラリ自体によって提供されます。

これで、個別に必要な場合は、PythonをOpenSSL以上に作成することができます。PydeBugオプションで構築して、それらを作成できます。

PythonソースTarballから:

./configure --with-pydebug
make

そして、あなたはそこに行きます:

>>> import _sha
[38571 refs]
>>> _sha.__file__
'/home/senthil/python/release27-maint/build/lib.linux-i686-2.7-pydebug/_sha.so'
[38573 refs]

他のヒント

Pythonのインストールは、_sha(両方のCモジュール)の代わりに_haslib内にShaがコンパイルされているようです。 Python 2.6のHashlib.pyから:

import _haslib:
    .....
except ImportError:
    # We don't have the _hashlib OpenSSL module?
    # use the built in legacy interfaces via a wrapper function
    new = __py_new

    # lookup the C function to use directly for the named constructors
    md5 = __get_builtin_constructor('md5')
    sha1 = __get_builtin_constructor('sha1')
    sha224 = __get_builtin_constructor('sha224')
    sha256 = __get_builtin_constructor('sha256')
    sha384 = __get_builtin_constructor('sha384')
    sha512 = __get_builtin_constructor('sha512')
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top