Skip to content

Commit d209fd9

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

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

Lib/hmac.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ def copy(self):
127127
# Call __new__ directly to avoid the expensive __init__.
128128
other = self.__class__.__new__(self.__class__)
129129
other.digest_size = self.digest_size
130+
other.block_size = self.block_size
130131
if self._hmac:
131132
other._hmac = self._hmac.copy()
132133
other._inner = other._outer = None

Lib/test/test_hmac.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,16 @@ def test_with_str_update(self):
489489

490490
class CopyTestCase(unittest.TestCase):
491491

492+
@hashlib_helper.requires_hashdigest('sha256')
493+
def test_copy(self):
494+
# Test a generic copy() and the attributes it exposes.
495+
# See https://github.com/python/cpython/issues/142451.
496+
h1 = hmac.new(b"my secret key", digestmod="sha256")
497+
h2 = h1.copy()
498+
self.assertEqual(h1.name, h2.name)
499+
self.assertEqual(h1.digest_size, h2.digest_size)
500+
self.assertEqual(h1.block_size, h2.block_size)
501+
492502
@hashlib_helper.requires_hashdigest('sha256')
493503
def test_attributes_old(self):
494504
# Testing if attributes are of same type.
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)