mirror of
https://github.com/python/cpython.git
synced 2025-07-07 19:35:27 +00:00
gh-135532: use defining_class
for copying BLAKE-2 and SHA-3 objects (#135838)
This commit is contained in:
parent
34393cbdd4
commit
ef4fc86afa
5 changed files with 38 additions and 17 deletions
|
@ -371,6 +371,16 @@ class HashLibTestCase(unittest.TestCase):
|
||||||
self.assertIs(constructor, _md5.md5)
|
self.assertIs(constructor, _md5.md5)
|
||||||
self.assertEqual(sorted(builtin_constructor_cache), ['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):
|
def test_hexdigest(self):
|
||||||
for cons in self.hash_constructors:
|
for cons in self.hash_constructors:
|
||||||
h = cons(usedforsecurity=False)
|
h = cons(usedforsecurity=False)
|
||||||
|
|
|
@ -787,17 +787,19 @@ error:
|
||||||
/*[clinic input]
|
/*[clinic input]
|
||||||
_blake2.blake2b.copy
|
_blake2.blake2b.copy
|
||||||
|
|
||||||
|
cls: defining_class
|
||||||
|
|
||||||
Return a copy of the hash object.
|
Return a copy of the hash object.
|
||||||
[clinic start generated code]*/
|
[clinic start generated code]*/
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
_blake2_blake2b_copy_impl(Blake2Object *self)
|
_blake2_blake2b_copy_impl(Blake2Object *self, PyTypeObject *cls)
|
||||||
/*[clinic end generated code: output=622d1c56b91c50d8 input=e383c2d199fd8a2e]*/
|
/*[clinic end generated code: output=5f8ea31c56c52287 input=f38f3475e9aec98d]*/
|
||||||
{
|
{
|
||||||
int rc;
|
int rc;
|
||||||
Blake2Object *cpy;
|
Blake2Object *cpy;
|
||||||
|
|
||||||
if ((cpy = new_Blake2Object(Py_TYPE(self))) == NULL) {
|
if ((cpy = new_Blake2Object(cls)) == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
14
Modules/clinic/blake2module.c.h
generated
14
Modules/clinic/blake2module.c.h
generated
|
@ -434,15 +434,19 @@ PyDoc_STRVAR(_blake2_blake2b_copy__doc__,
|
||||||
"Return a copy of the hash object.");
|
"Return a copy of the hash object.");
|
||||||
|
|
||||||
#define _BLAKE2_BLAKE2B_COPY_METHODDEF \
|
#define _BLAKE2_BLAKE2B_COPY_METHODDEF \
|
||||||
{"copy", (PyCFunction)_blake2_blake2b_copy, METH_NOARGS, _blake2_blake2b_copy__doc__},
|
{"copy", _PyCFunction_CAST(_blake2_blake2b_copy), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _blake2_blake2b_copy__doc__},
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
_blake2_blake2b_copy_impl(Blake2Object *self);
|
_blake2_blake2b_copy_impl(Blake2Object *self, PyTypeObject *cls);
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
_blake2_blake2b_copy(PyObject *self, PyObject *Py_UNUSED(ignored))
|
_blake2_blake2b_copy(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
|
||||||
{
|
{
|
||||||
return _blake2_blake2b_copy_impl((Blake2Object *)self);
|
if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) {
|
||||||
|
PyErr_SetString(PyExc_TypeError, "copy() takes no arguments");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return _blake2_blake2b_copy_impl((Blake2Object *)self, cls);
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(_blake2_blake2b_update__doc__,
|
PyDoc_STRVAR(_blake2_blake2b_update__doc__,
|
||||||
|
@ -502,4 +506,4 @@ _blake2_blake2b_hexdigest(PyObject *self, PyObject *Py_UNUSED(ignored))
|
||||||
{
|
{
|
||||||
return _blake2_blake2b_hexdigest_impl((Blake2Object *)self);
|
return _blake2_blake2b_hexdigest_impl((Blake2Object *)self);
|
||||||
}
|
}
|
||||||
/*[clinic end generated code: output=eed18dcfaf6f7731 input=a9049054013a1b77]*/
|
/*[clinic end generated code: output=60a4abbcb8950fe5 input=a9049054013a1b77]*/
|
||||||
|
|
14
Modules/clinic/sha3module.c.h
generated
14
Modules/clinic/sha3module.c.h
generated
|
@ -100,15 +100,19 @@ PyDoc_STRVAR(_sha3_sha3_224_copy__doc__,
|
||||||
"Return a copy of the hash object.");
|
"Return a copy of the hash object.");
|
||||||
|
|
||||||
#define _SHA3_SHA3_224_COPY_METHODDEF \
|
#define _SHA3_SHA3_224_COPY_METHODDEF \
|
||||||
{"copy", (PyCFunction)_sha3_sha3_224_copy, METH_NOARGS, _sha3_sha3_224_copy__doc__},
|
{"copy", _PyCFunction_CAST(_sha3_sha3_224_copy), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _sha3_sha3_224_copy__doc__},
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
_sha3_sha3_224_copy_impl(SHA3object *self);
|
_sha3_sha3_224_copy_impl(SHA3object *self, PyTypeObject *cls);
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
_sha3_sha3_224_copy(PyObject *self, PyObject *Py_UNUSED(ignored))
|
_sha3_sha3_224_copy(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
|
||||||
{
|
{
|
||||||
return _sha3_sha3_224_copy_impl((SHA3object *)self);
|
if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) {
|
||||||
|
PyErr_SetString(PyExc_TypeError, "copy() takes no arguments");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return _sha3_sha3_224_copy_impl((SHA3object *)self, cls);
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(_sha3_sha3_224_digest__doc__,
|
PyDoc_STRVAR(_sha3_sha3_224_digest__doc__,
|
||||||
|
@ -306,4 +310,4 @@ _sha3_shake_128_hexdigest(PyObject *self, PyObject *const *args, Py_ssize_t narg
|
||||||
exit:
|
exit:
|
||||||
return return_value;
|
return return_value;
|
||||||
}
|
}
|
||||||
/*[clinic end generated code: output=c17e3ec670afe253 input=a9049054013a1b77]*/
|
/*[clinic end generated code: output=48be77f8a31e8a3e input=a9049054013a1b77]*/
|
||||||
|
|
|
@ -239,16 +239,17 @@ SHA3_traverse(PyObject *self, visitproc visit, void *arg)
|
||||||
/*[clinic input]
|
/*[clinic input]
|
||||||
_sha3.sha3_224.copy
|
_sha3.sha3_224.copy
|
||||||
|
|
||||||
|
cls: defining_class
|
||||||
|
|
||||||
Return a copy of the hash object.
|
Return a copy of the hash object.
|
||||||
[clinic start generated code]*/
|
[clinic start generated code]*/
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
_sha3_sha3_224_copy_impl(SHA3object *self)
|
_sha3_sha3_224_copy_impl(SHA3object *self, PyTypeObject *cls)
|
||||||
/*[clinic end generated code: output=6c537411ecdcda4c input=93a44aaebea51ba8]*/
|
/*[clinic end generated code: output=13958b44c244013e input=7134b4dc0a2fbcac]*/
|
||||||
{
|
{
|
||||||
SHA3object *newobj;
|
SHA3object *newobj;
|
||||||
|
if ((newobj = newSHA3object(cls)) == NULL) {
|
||||||
if ((newobj = newSHA3object(Py_TYPE(self))) == NULL) {
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
HASHLIB_ACQUIRE_LOCK(self);
|
HASHLIB_ACQUIRE_LOCK(self);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue