From d209fd911eab6588932558b1e40c339f49c85f59 Mon Sep 17 00:00:00 2001 From: =?utf8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sun, 14 Dec 2025 10:36:26 +0100 Subject: [PATCH] [3.13] gh-142451: correctly copy HMAC attributes in `HMAC.copy()` (GH-142510) (#142701) Partially cherry-picked from d3ef5ba34d3068b8178d6ff0f39462db6bbc4ad5 which ensures that the `block_size` attribute exists on the copy. --- Lib/hmac.py | 1 + Lib/test/test_hmac.py | 10 ++++++++++ .../2025-12-14-10-00-23.gh-issue-142451._rkf2S.rst | 3 +++ 3 files changed, 14 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-12-14-10-00-23.gh-issue-142451._rkf2S.rst diff --git a/Lib/hmac.py b/Lib/hmac.py index 8b4eb2fe741e..a49f2aeb1d61 100644 --- a/Lib/hmac.py +++ b/Lib/hmac.py @@ -127,6 +127,7 @@ class HMAC: # Call __new__ directly to avoid the expensive __init__. other = self.__class__.__new__(self.__class__) other.digest_size = self.digest_size + other.block_size = self.block_size if self._hmac: other._hmac = self._hmac.copy() other._inner = other._outer = None diff --git a/Lib/test/test_hmac.py b/Lib/test/test_hmac.py index 1502fba9f3e8..9161ac716075 100644 --- a/Lib/test/test_hmac.py +++ b/Lib/test/test_hmac.py @@ -489,6 +489,16 @@ class UpdateTestCase(unittest.TestCase): class CopyTestCase(unittest.TestCase): + @hashlib_helper.requires_hashdigest('sha256') + def test_copy(self): + # Test a generic copy() and the attributes it exposes. + # See https://github.com/python/cpython/issues/142451. + h1 = hmac.new(b"my secret key", digestmod="sha256") + h2 = h1.copy() + self.assertEqual(h1.name, h2.name) + self.assertEqual(h1.digest_size, h2.digest_size) + self.assertEqual(h1.block_size, h2.block_size) + @hashlib_helper.requires_hashdigest('sha256') def test_attributes_old(self): # Testing if attributes are of same type. diff --git a/Misc/NEWS.d/next/Library/2025-12-14-10-00-23.gh-issue-142451._rkf2S.rst b/Misc/NEWS.d/next/Library/2025-12-14-10-00-23.gh-issue-142451._rkf2S.rst new file mode 100644 index 000000000000..cceb572f503e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-12-14-10-00-23.gh-issue-142451._rkf2S.rst @@ -0,0 +1,3 @@ +:mod:`hmac`: Ensure that the :attr:`HMAC.block_size ` +attribute is correctly copied by :meth:`HMAC.copy `. Patch +by Bénédikt Tran. -- 2.47.3