Skip to content

gh-110357: hashlib no longer logs at import when a guaranteed hash is unavailable#152538

Merged
gpshead merged 1 commit into
python:mainfrom
gpshead:gh-110357-hashlib-no-import-noise
Jun 29, 2026
Merged

gh-110357: hashlib no longer logs at import when a guaranteed hash is unavailable#152538
gpshead merged 1 commit into
python:mainfrom
gpshead:gh-110357-hashlib-no-import-noise

Conversation

@gpshead

@gpshead gpshead commented Jun 29, 2026

Copy link
Copy Markdown
Member

hashlib logs an error at import time when a normally-guaranteed algorithm
cannot be constructed, which happens when an OpenSSL FIPS configuration excludes
it from the default provider or the build used --without-builtin-hashlib-hashes.
For example:

ERROR:root:hash algorithm blake2b will not be supported at runtime [reason: unsupported hash algorithm blake2b]

Logging at import time is best avoided because of its side effects. Here the
message is unwanted output for programs that never use the missing algorithm,
and the module-level logging.error() call runs logging.basicConfig(), which
attaches a handler to the root logger. That changes global logging state for any
application that imports hashlib before configuring logging, and regrtest
reports it as an altered execution environment.

This removes the logging. Code that uses a missing algorithm still gets a
ValueError from the stub constructor installed in its place, so nothing useful
is lost.

Verification

Built with --without-builtin-hashlib-hashes, so blake2b/blake2s are
unavailable: import hashlib is now silent and test_hashlib no longer reports
env_changed. A normal build's test_hashlib is unaffected.

…ash is unavailable

When a normally-guaranteed hash algorithm cannot be constructed at import time
(e.g. an OpenSSL FIPS configuration excludes it from the default provider, or
the build used --without-builtin-hashlib-hashes), importing hashlib emitted an
"ERROR:root:hash algorithm ... will not be supported at runtime" message to
stderr.  For the many programs that never use the missing algorithm this is
pure noise.  Worse, logging.error() lazily calls logging.basicConfig(), which
mutates the root logger's handlers -- a global side effect that the test suite
flags as an altered execution environment.

Stop logging in that path.  Code that actually uses a missing algorithm still
gets a clear ValueError from the stub constructor installed in its place.

The stray output has shown up incidentally in FIPS / "No Builtin Hashes"
buildbot reports for years (e.g. pythongh-110357, pythongh-76902) without being the
reported subject.
@gpshead gpshead force-pushed the gh-110357-hashlib-no-import-noise branch from c0e838d to 681a4e9 Compare June 29, 2026 01:59
@gpshead gpshead added the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jun 29, 2026
@gpshead gpshead self-assigned this Jun 29, 2026
@gpshead gpshead marked this pull request as ready for review June 29, 2026 02:01
@gpshead gpshead requested a review from picnixz as a code owner June 29, 2026 02:01
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #33347603 | 📁 Comparing 681a4e9 against main (f57d3d6)

  🔍 Preview build  

2 files changed
± library/hashlib.html
± whatsnew/changelog.html

@picnixz

picnixz commented Jun 29, 2026

Copy link
Copy Markdown
Member

FTR, the logging part was already there for a long time and so i kept the logic in addition to a runtie check.

@gpshead gpshead merged commit 8ae1a23 into python:main Jun 29, 2026
55 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @gpshead for the PR 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@bedevere-app

bedevere-app Bot commented Jun 29, 2026

Copy link
Copy Markdown

GH-152554 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jun 29, 2026
gpshead added a commit that referenced this pull request Jun 29, 2026
…hash is unavailable (GH-152538) (#152554)

gh-110357: hashlib no longer logs at import when a guaranteed hash is unavailable (GH-152538)

When a normally-guaranteed hash algorithm cannot be constructed at import time
(e.g. an OpenSSL FIPS configuration excludes it from the default provider, or
the build used --without-builtin-hashlib-hashes), importing hashlib emitted an
"ERROR:root:hash algorithm ... will not be supported at runtime" message to
stderr.  For the many programs that never use the missing algorithm this is
pure noise.  Worse, logging.error() lazily calls logging.basicConfig(), which
mutates the root logger's handlers -- a global side effect that the test suite
flags as an altered execution environment.

Stop logging in that path.  Code that actually uses a missing algorithm still
gets a clear ValueError from the stub constructor installed in its place.

The stray output has shown up incidentally in FIPS / "No Builtin Hashes"
buildbot reports for years (e.g. gh-110357, gh-76902) without being the
reported subject.
(cherry picked from commit 8ae1a23)

Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants