GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Ensure that `hashlib.<name>` does not raise `AttributeError` · Issue #136929 · python/cpython

Ensure that hashlib.<name> does not raise AttributeError #136929

New issue
Copy link
New issue
Copy link
Closed
Closed
Ensure that hashlib.<name> does not raise AttributeError#136929
Copy link
Assignees
picnixz
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Description

@picnixz
picnixz
opened on Jul 21, 2025
Issue body actions

Feature or enhancement

Proposal:

Let's summarize the current behavior of hashlib. We have two interfaces for getting digests: hashlib.new(digest, ...) and hashlib.<digest>().

With hashlib.new() it depends on the presence of OpenSSL. If OpenSSL is present, and if it's not a BLAKE-2 (this is a special case that I'll talk about it later), we check if OpenSSL recognizes the digest and the security policy allows it. If this is not the case, we fall back to the built-in implementation, and we don't care about the security policy here. If the built-in doesn't exist, then we raise an exact ValueError.

With hashlib.md5() (and anything else except "blake2"), this is much more subtle. Named constructors are determined at import time and solely depend on the presence of OpenSSL. More precisely, if OpenSSL and the security policy allows it, then hashlib.md5 is set to _hashlib.openssl_md5. And this doesn't change for the interpreter's lifetime.

On the other hand, if the security policy doesn't allow it, then we still set hashlib.md5 to _hashlib.openssl_md5. This means that we will not be able to use it unless we explicitly pass usedforsecurity=False here. Now, without OpenSSL, we set the named constructors to the corresponding built-in HACL functions.

Now, as I said, the problem is about import hashlib when neither OpenSSL nor HACL* are present. Instead of raising an AttributeError when trying to access the function, we should either raise an ImportError, or create mock functions for hash functions that raise ValueError at runtime (which would be ideal IMO). That way, we can ensure that tests using cryptographic hashes are decorated with "@requires_hashdigest" and make build bots that are match by "FIPS" successful.


The case for blake2 is a bit different because we actually do not care about OpenSSL at all! IOW, hashlib.blake2 is solely HACL* implemented except that we can still access it via hashlib.new("blake2b512", ...).

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

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

Activity

  1. picnixz
    self-assigned this
    on Jul 21, 2025
  2. picnixz
    added
    type-featureA feature request or enhancementA feature request or enhancement
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Jul 21, 2025
  3. picnixz
    changed the title [-]Ensure that `hashlib` does not raise `ValueError` exceptions on imports[/-] [+]Ensure that `hashlib` does not log exceptions on imports[/+] on Jul 21, 2025
  4. picnixz
    changed the title [-]Ensure that `hashlib` does not log exceptions on imports[/-] [+]Ensure that `hashlib.md5` does not raise `AttributeError`[/+] on Jul 21, 2025
  5. picnixz
    changed the title [-]Ensure that `hashlib.md5` does not raise `AttributeError`[/-] [+]Ensure that `hashlib.<name>` does not raise `AttributeError`[/+] on Jul 22, 2025
  6. picnixz
    added a commit that references this issue on Jul 25, 2025

    gh-136929: ensure that `hashlib.<name>` does not raise `AttributeErro…

    7ce2f10
  7. picnixz
    closed this as completedon Jul 25, 2025
  8. picnixz commented on Jul 25, 2025

    @picnixz
    picnixz
    on Jul 25, 2025
    MemberAuthor
    More actions

    In the end I decided to the following:

    • No AttributeError.
    • ValueError when calling the function that would have failed.

    We can't add an algorithm that is not supported to __all__. Anything in __all__ should exist at runtime, at least to me, so I prefer raising a ValueError instead.

  9. taegyunkim
    added a commit that references this issue on Aug 4, 2025

    pythongh-136929: ensure that `hashlib.<name>` does not raise `Attribu…

    cfe1d16
  10. Agent-Hellboy
    added a commit that references this issue on Aug 19, 2025

    pythongh-136929: ensure that `hashlib.<name>` does not raise `Attribu…

    08f5eb1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @picnixz
    picnixz

Labels

stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.