Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ef4fc86

Browse files
authored
gh-135532: use defining_class for copying BLAKE-2 and SHA-3 objects (#135838)
1 parent 34393cb commit ef4fc86

File tree

5 files changed

+38
-17
lines changed

5 files changed

+38
-17
lines changed

Lib/test/test_hashlib.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,16 @@ def test_get_builtin_constructor(self):
371371
self.assertIs(constructor, _md5.md5)
372372
self.assertEqual(sorted(builtin_constructor_cache), ['MD5', 'md5'])
373373

374+
def test_copy(self):
375+
for cons in self.hash_constructors:
376+
h1 = cons(os.urandom(16), usedforsecurity=False)
377+
h2 = h1.copy()
378+
self.assertIs(type(h1), type(h2))
379+
self.assertEqual(h1.name, h2.name)
380+
size = (16,) if h1.name in self.shakes else ()
381+
self.assertEqual(h1.digest(*size), h2.digest(*size))
382+
self.assertEqual(h1.hexdigest(*size), h2.hexdigest(*size))
383+
374384
def test_hexdigest(self):
375385
for cons in self.hash_constructors:
376386
h = cons(usedforsecurity=False)

Modules/blake2module.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -787,17 +787,19 @@ blake2_blake2b_copy_unlocked(Blake2Object *self, Blake2Object *cpy)
787787
/*[clinic input]
788788
_blake2.blake2b.copy
789789
790+
cls: defining_class
791+
790792
Return a copy of the hash object.
791793
[clinic start generated code]*/
792794

793795
static PyObject *
794-
_blake2_blake2b_copy_impl(Blake2Object *self)
795-
/*[clinic end generated code: output=622d1c56b91c50d8 input=e383c2d199fd8a2e]*/
796+
_blake2_blake2b_copy_impl(Blake2Object *self, PyTypeObject *cls)
797+
/*[clinic end generated code: output=5f8ea31c56c52287 input=f38f3475e9aec98d]*/
796798
{
797799
int rc;
798800
Blake2Object *cpy;
799801

800-
if ((cpy = new_Blake2Object(Py_TYPE(self))) == NULL) {
802+
if ((cpy = new_Blake2Object(cls)) == NULL) {
801803
return NULL;
802804
}
803805

Modules/clinic/blake2module.c.h

Lines changed: 9 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/clinic/sha3module.c.h

Lines changed: 9 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/sha3module.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,17 @@ SHA3_traverse(PyObject *self, visitproc visit, void *arg)
239239
/*[clinic input]
240240
_sha3.sha3_224.copy
241241
242+
cls: defining_class
243+
242244
Return a copy of the hash object.
243245
[clinic start generated code]*/
244246

245247
static PyObject *
246-
_sha3_sha3_224_copy_impl(SHA3object *self)
247-
/*[clinic end generated code: output=6c537411ecdcda4c input=93a44aaebea51ba8]*/
248+
_sha3_sha3_224_copy_impl(SHA3object *self, PyTypeObject *cls)
249+
/*[clinic end generated code: output=13958b44c244013e input=7134b4dc0a2fbcac]*/
248250
{
249251
SHA3object *newobj;
250-
251-
if ((newobj = newSHA3object(Py_TYPE(self))) == NULL) {
252+
if ((newobj = newSHA3object(cls)) == NULL) {
252253
return NULL;
253254
}
254255
HASHLIB_ACQUIRE_LOCK(self);

0 commit comments

Comments
 (0)