Skip to content

gh-136929: ensure that hashlib.<name> does not raise AttributeError #136933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
ensure that hashlib.<name> does not raise AttributeError
  • Loading branch information
picnixz committed Jul 21, 2025
commit 49ac4afe6aa217ece0689ee1a37eb924c031a762
11 changes: 11 additions & 0 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,17 @@ difflib
(Contributed by Jiahao Li in :gh:`134580`.)


hashlib
-------

* Ensure that hash functions guaranteed to be always *available* exist as
attributes of :mod:`hashlib` even if they will not work at runtime due to
missing backend implementations. For instance, ``hashlib.md5`` will no
longer raise :exc:`AttributeError` if OpenSSL is not available and Python
has been built without MD5 support.
(Contributed by Bénédikt Tran in :gh:`136929`.)


http.client
-----------

Expand Down
32 changes: 29 additions & 3 deletions Lib/hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,16 +261,42 @@ def file_digest(fileobj, digest, /, *, _bufsize=2**18):
return digestobj


__logging = None
__logger = None
for __func_name in __always_supported:
# try them all, some may not work due to the OpenSSL
# version not supporting that algorithm.
try:
globals()[__func_name] = __get_hash(__func_name)
except ValueError:
import logging
logging.exception('code for hash %s was not found.', __func_name)

import logging as __logging
if __logger is None:
__logger = __logging.getLogger(__name__)
__logger.warning('hash algorithm %s will not be supported at runtime',
__func_name)
# The following code can be simplified in Python 3.19
# once "string" is removed from the signature.
__code = f'''\
def {__func_name}(data=__UNSET, *, usedforsecurity=True, string=__UNSET):
if data is __UNSET and string is not __UNSET:
import warnings
warnings.warn(
"the 'string' keyword parameter is deprecated since "
"Python 3.15 and slated for removal in Python 3.19; "
"use the 'data' keyword parameter or pass the data "
"to hash as a positional argument instead",
DeprecationWarning, stacklevel=2)
if data is not __UNSET and string is not __UNSET:
raise TypeError("'data' and 'string' are mutually exclusive "
"and support for 'string' keyword parameter "
"is slated for removal in a future version.")
raise ValueError("unsupported hash algorithm {__func_name}")
'''
exec(__code, {"__UNSET": object()}, __locals := {})
globals()[__func_name] = __locals[__func_name]
del __code, __locals

# Cleanup locals()
del __always_supported, __func_name, __get_hash
del __py_new, __hash_new, __get_openssl_constructor
del __logger, __logging
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Ensure that hash functions guaranteed to be always *available* exist as
attributes of :mod:`hashlib` even if they will not work at runtime due to
missing backend implementations. For instance, ``hashlib.md5`` will no
longer raise :exc:`AttributeError` if OpenSSL is not available and Python
has been built without MD5 support. Patch by Bénédikt Tran.
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy