https://github.com/python/cpython/commit/77ad3d59c2c920dd3c0408139cb9be9cab430ce3
commit: 77ad3d59c2c920dd3c0408139cb9be9cab430ce3
branch: 3.14
author: Bénédikt Tran <[email protected]>
committer: picnixz <[email protected]>
date: 2025-12-14T09:27:03Z
summary:
[3.14] gh-142451: correctly copy HMAC attributes in `HMAC.copy()` (GH-142510)
(#142698)
Partially cherry-picked from d3ef5ba34d3068b8178d6ff0f39462db6bbc4ad5
which ensures that the `block_size` attribute exists on the copy.
files:
A Misc/NEWS.d/next/Library/2025-12-14-10-00-23.gh-issue-142451._rkf2S.rst
M Lib/hmac.py
M Lib/test/test_hmac.py
diff --git a/Lib/hmac.py b/Lib/hmac.py
index 16022c9ceb5439..2d6016cda11c0e 100644
--- a/Lib/hmac.py
+++ b/Lib/hmac.py
@@ -159,6 +159,7 @@ def copy(self):
# 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 344c6ddf28afcf..1d270b2a43c184 100644
--- a/Lib/test/test_hmac.py
+++ b/Lib/test/test_hmac.py
@@ -1076,6 +1076,15 @@ def test_properties(self):
self.assertEqual(h.digest_size, self.digest_size)
self.assertEqual(h.block_size, self.block_size)
+ def test_copy(self):
+ # Test a generic copy() and the attributes it exposes.
+ # See https://github.com/python/cpython/issues/142451.
+ h1 = self.hmac_new(b"my secret key", digestmod=self.digestname)
+ 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)
+
def test_repr(self):
# HMAC object representation may differ across implementations
raise NotImplementedError
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 00000000000000..cceb572f503eda
--- /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 <hmac.HMAC.block_size>`
+attribute is correctly copied by :meth:`HMAC.copy <hmac.HMAC.copy>`. Patch
+by Bénédikt Tran.
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]