Skip to content

gh-135532: use defining_class for copying BLAKE-2 and SHA-3 objects #135838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Lib/test/test_hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,16 @@ def test_get_builtin_constructor(self):
self.assertIs(constructor, _md5.md5)
self.assertEqual(sorted(builtin_constructor_cache), ['MD5', 'md5'])

def test_copy(self):
for cons in self.hash_constructors:
h1 = cons(os.urandom(16), usedforsecurity=False)
h2 = h1.copy()
self.assertIs(type(h1), type(h2))
self.assertEqual(h1.name, h2.name)
size = (16,) if h1.name in self.shakes else ()
self.assertEqual(h1.digest(*size), h2.digest(*size))
self.assertEqual(h1.hexdigest(*size), h2.hexdigest(*size))

def test_hexdigest(self):
for cons in self.hash_constructors:
h = cons(usedforsecurity=False)
Expand Down
8 changes: 5 additions & 3 deletions Modules/blake2module.c
Original file line number Diff line number Diff line change
Expand Up @@ -787,17 +787,19 @@ blake2_blake2b_copy_unlocked(Blake2Object *self, Blake2Object *cpy)
/*[clinic input]
_blake2.blake2b.copy

cls: defining_class

Return a copy of the hash object.
[clinic start generated code]*/

static PyObject *
_blake2_blake2b_copy_impl(Blake2Object *self)
/*[clinic end generated code: output=622d1c56b91c50d8 input=e383c2d199fd8a2e]*/
_blake2_blake2b_copy_impl(Blake2Object *self, PyTypeObject *cls)
/*[clinic end generated code: output=5f8ea31c56c52287 input=f38f3475e9aec98d]*/
{
int rc;
Blake2Object *cpy;

if ((cpy = new_Blake2Object(Py_TYPE(self))) == NULL) {
if ((cpy = new_Blake2Object(cls)) == NULL) {
return NULL;
}

Expand Down
14 changes: 9 additions & 5 deletions Modules/clinic/blake2module.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions Modules/clinic/sha3module.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions Modules/sha3module.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,17 @@ SHA3_traverse(PyObject *self, visitproc visit, void *arg)
/*[clinic input]
_sha3.sha3_224.copy

cls: defining_class

Return a copy of the hash object.
[clinic start generated code]*/

static PyObject *
_sha3_sha3_224_copy_impl(SHA3object *self)
/*[clinic end generated code: output=6c537411ecdcda4c input=93a44aaebea51ba8]*/
_sha3_sha3_224_copy_impl(SHA3object *self, PyTypeObject *cls)
/*[clinic end generated code: output=13958b44c244013e input=7134b4dc0a2fbcac]*/
{
SHA3object *newobj;

if ((newobj = newSHA3object(Py_TYPE(self))) == NULL) {
if ((newobj = newSHA3object(cls)) == NULL) {
return NULL;
}
HASHLIB_ACQUIRE_LOCK(self);
Expand Down
Loading