_sha在python hashlib中导入 [英] _sha import in python hashlib

查看:108
本文介绍了_sha在python hashlib中导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,今天我正在检查python中的hashlib模块,但后来发现了我仍然无法弄清的东西.

Well, today I was checking the hashlib module in python, but then I found something that I still can't figure out.

在此python模块中,有一个我无法跟踪的导入.我是这样的:

Inside this python module, there is an import that I can't follow. I goes like this:

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

我试图从python shell导入_sha模块,但似乎无法通过这种方式实现.我的第一个猜测是它是C模块,但我不确定.

I tried to import the _sha module from a python shell, but is seems that it cannot be reached that way.My first guess is that it's a C module, but I'm not sure.

所以告诉我,你知道那个模块在哪里吗?他们如何导入它?

So tell me guys, do you know where is that module? How do they import it?

推荐答案

实际上,_sha模块由shamodule.c提供,_md5由md5module.c和md5.c提供,并且两者都仅在使用Python时才构建默认情况下未使用OpenSSL进行编译.

Actually, the _sha module is provided by shamodule.c and _md5 is provided by md5module.c and md5.c and both will be built only when your Python is not compiled with OpenSSL by default.

您可以在Python Source压缩文件的setup.py中找到详细信息.

You can find the details in setup.py in your Python Source 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库本身提供.

Most often, your Python is built with Openssl library and in that case, those functions are provided by the OpenSSL libraries itself.

现在,如果您要单独使用它们,则可以在没有OpenSSL或更好的情况下构建Python,可以使用pydebug选项进行构建并拥有它们.

Now, if you want them separately, then you can build your Python without OpenSSL or better yet, you can build with pydebug option and have them.

从您的Python Source压缩包中:

From your Python Source 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]

这篇关于_sha在python hashlib中导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆