Skip to content

Commit 77ad3d5

Browse files
authored
[3.14] gh-142451: correctly copy HMAC attributes in HMAC.copy() (GH-142510) (#142698)
Partially cherry-picked from d3ef5ba which ensures that the `block_size` attribute exists on the copy.
1 parent aef4552 commit 77ad3d5

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

Lib/hmac.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ def copy(self):
159159
# Call __new__ directly to avoid the expensive __init__.
160160
other = self.__class__.__new__(self.__class__)
161161
other.digest_size = self.digest_size
162+
other.block_size = self.block_size
162163
if self._hmac:
163164
other._hmac = self._hmac.copy()
164165
other._inner = other._outer = None

Lib/test/test_hmac.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,15 @@ def test_properties(self):
10761076
self.assertEqual(h.digest_size, self.digest_size)
10771077
self.assertEqual(h.block_size, self.block_size)
10781078

1079+
def test_copy(self):
1080+
# Test a generic copy() and the attributes it exposes.
1081+
# See https://github.com/python/cpython/issues/142451.
1082+
h1 = self.hmac_new(b"my secret key", digestmod=self.digestname)
1083+
h2 = h1.copy()
1084+
self.assertEqual(h1.name, h2.name)
1085+
self.assertEqual(h1.digest_size, h2.digest_size)
1086+
self.assertEqual(h1.block_size, h2.block_size)
1087+
10791088
def test_repr(self):
10801089
# HMAC object representation may differ across implementations
10811090
raise NotImplementedError
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:mod:`hmac`: Ensure that the :attr:`HMAC.block_size <hmac.HMAC.block_size>`
2+
attribute is correctly copied by :meth:`HMAC.copy <hmac.HMAC.copy>`. Patch
3+
by Bénédikt Tran.

0 commit comments

Comments
 (0)