You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
hmac.new() and hmac.digest() construct and discard an extra hash object on the Python fallback path #162718
On CPython 3.15 and 3.16, passing a BLAKE2b factory to hmac.new() or hmac.digest() constructs three hash objects for a short key. CPython 3.14 constructs two: one for the inner hash and one for the outer hash.
The extra call comes from #136802. _is_shake_constructor() calls the factory to inspect the resulting object's .name, then discards that object. Both fallback paths immediately call the factory again to create the inner and outer hashes. The one-byte key in the example rules out the additional hash that HMAC legitimately needs for a long key.
I tested a local change on main that checks .name on the first hash object the fallback already needs. Both counts returned to two. With hashlib.blake2b passed directly, median time for hmac.new(b"k", b"data", hashlib.blake2b) went from 2.43 to 2.19 µs per call. For hmac.digest() it went from 2.90 to 2.66 µs. These were ten alternating runs of 150,000 calls per implementation on the same interpreter, pinned to one CPU.
The existing test_hmac suite passed with three skips. I also checked that both fallback paths still raised ValueError for a factory returning hashlib.shake_128()
Activity
picnixz commentedon Nov 27, 2026
It is not a concern. The operations are cheap and re the fallbacks