GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-135532: use `defining_class` for copying BLAKE-2 and SHA-3 objects by picnixz · Pull Request #135838 · python/cpython

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.

Sign up for GitHub

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
picnixz merged 2 commits into python:main from picnixz:refactor/hashlib/defining-class-135532
Jun 24, 2025

Uh oh!

There was an error while loading. Please reload this page.

Merged

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

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit Hold shift + click to select a range
f631476
use 'defining_class' when copying HACL*-based hashes
picnixz Jun 23, 2025
26a029c
simplify test
picnixz Jun 23, 2025
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Next Next commit
use 'defining_class' when copying HACL*-based hashes
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@picnixz
picnixz committed Jun 23, 2025
commit f6314764fd253d520809c73bf82ec1567992b145

There are no files selected for viewing

26 changes: 26 additions & 0 deletions 26 Lib/test/test_hashlib.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,24 @@ def shake_constructors(self):
def is_fips_mode(self):
return get_fips_mode()

def match_digest(self, h1, h2, *, shake_size=16):
self.assertIs(type(h1), type(h2))
self.assertEqual(h1.name, h2.name)

if h1.name in self.shakes:
d1, h1 = h1.digest(shake_size), h1.hexdigest(shake_size)
d2, h2 = h2.digest(shake_size), h2.hexdigest(shake_size)
else:
d1, h1 = h1.digest(), h1.hexdigest()
d2, h2 = h2.digest(), h2.hexdigest()

self.assertIsInstance(d1, bytes)
self.assertIsInstance(h1, str)
self.assertEqual(d1.hex(), h1)

self.assertEqual(d1, d2)
self.assertEqual(h1, h2)

def test_hash_array(self):
a = array.array("b", range(10))
for cons in self.hash_constructors:
Expand Down Expand Up @@ -371,6 +389,14 @@ 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(usedforsecurity=False)
h1.update(os.urandom(16))
h2 = h1.copy()
self.assertIs(type(h1), type(h2))
self.match_digest(h1, h2)

def test_hexdigest(self):
for cons in self.hash_constructors:
h = cons(usedforsecurity=False)
Expand Down
8 changes: 5 additions & 3 deletions 8 Modules/blake2module.c
Show comments
View file
Open in desktop
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 14 Modules/clinic/blake2module.c.h
Show comments
View file
Open in desktop

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

Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

14 changes: 9 additions & 5 deletions 14 Modules/clinic/sha3module.c.h
Show comments
View file
Open in desktop

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

Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

9 changes: 5 additions & 4 deletions 9 Modules/sha3module.c
Show comments
View file
Open in desktop
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
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.