classification
Title: hashlib does not handle missing hash functions correctly
Type: behavior Stage:
Components: Versions: Python 2.7, Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: gregory.p.smith Nosy List: gregory.p.smith, iwienand, python-dev
Priority: normal Keywords: patch

Created on 2011-05-11 18:59 by iwienand, last changed 2011-05-14 22:37 by gregory.p.smith. This issue is now closed.

Files
File name Uploaded Description Edit
hashlib.py.diff iwienand, 2011-05-11 18:59
Messages (2)
msg135794 - (view) Author: Ian Wienand (iwienand) Date: 2011-05-11 18:59
If one of the hash functions isn't defined in _hashlib, the code suggests it should just be skipped

===
      # this one has no builtin implementation, don't define it
                pass
===

This doesn't happen however; due to ImportError not being caught the module decides the whole _hashlib module isn't available and tries to fall back to the older individual libraries.  You then get thrown an unrelated error about _md5 being unavailable

You can easily replicate this

---
$ python
Python 2.6.6 (r266:84292, Dec 26 2010, 22:31:48) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def foo():
...     raise ValueError
... 
>>> import _hashlib
>>> _hashlib.openssl_sha224 = foo
>>> import hashlib
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/hashlib.py", line 136, in <module>
    md5 = __get_builtin_constructor('md5')
  File "/usr/lib/python2.6/hashlib.py", line 63, in __get_builtin_constructor
    import _md5
ImportError: No module named _md5
>>> 
---

I think the solution is to catch the ImportError in __get_builtin_constructor and, if caught, consider the hash function unsupported
msg135999 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-05-14 22:36
New changeset 572fbd9ca28f by Gregory P. Smith in branch '2.7':
Fixes Issue #12059: Properly handle missing hash functions even when
http://hg.python.org/cpython/rev/572fbd9ca28f

New changeset ad9249f35914 by Gregory P. Smith in branch '3.2':
Fixes Issue #12059: Properly handle missing hash functions even when
http://hg.python.org/cpython/rev/ad9249f35914

New changeset 298888fd46b0 by Gregory P. Smith in branch 'default':
Fixes Issue #12059: Properly handle missing hash functions even when
http://hg.python.org/cpython/rev/298888fd46b0
History
Date User Action Args
2011-05-14 22:37:13gregory.p.smithsetstatus: open -> closed
assignee: gregory.p.smith
resolution: fixed
2011-05-14 22:36:21python-devsetnosy: + python-dev
messages: + msg135999
2011-05-12 12:32:39pitrousetnosy: + gregory.p.smith
2011-05-11 18:59:08iwienandcreate