Skip to content

bpo-41052: Optout serialization/deserialization for blake2s/b #22189

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

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 9 additions & 0 deletions Lib/test/test_hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,15 @@ def test_no_unicode_blake2(self):
self.check_no_unicode('blake2b')
self.check_no_unicode('blake2s')

@requires_blake2
@support.cpython_only
def test_bug_41052(self):
import pickle
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
s, b = _blake2.blake2s(), _blake2.blake2b()
self.assertRaises(TypeError, s, proto)
self.assertRaises(TypeError, b, proto)

@requires_sha3
def test_no_unicode_sha3(self):
self.check_no_unicode('sha3_224')
Expand Down
17 changes: 17 additions & 0 deletions Modules/_blake2/blake2b_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,29 @@ _blake2_blake2b_hexdigest_impl(BLAKE2bObject *self)
return _Py_strhex((const char *)digest, self->param.digest_length);
}

/*[clinic input]
_blake2.blake2b.__reduce__

Return the digest value as a string of hexadecimal digits.
[clinic start generated code]*/

static PyObject *
_blake2_blake2b___reduce___impl(BLAKE2bObject *self)
/*[clinic end generated code: output=3aeb49ad77e385ca input=6155b63cd93bf62c]*/
{
PyErr_Format(PyExc_TypeError,
"cannot pickle %s object",
Py_TYPE(self)->tp_name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than having to copy/paste such code in each type, would it be possible to have a protocol like:

class NotHashable:
    __hash__ = None

obj = NotHashable()
hash(obj)

This code raises TypeError: unhashable type: 'NotHashable'.

cc @pitrou @serhiy-storchaka

return NULL;
}


static PyMethodDef py_blake2b_methods[] = {
_BLAKE2_BLAKE2B_COPY_METHODDEF
_BLAKE2_BLAKE2B_DIGEST_METHODDEF
_BLAKE2_BLAKE2B_HEXDIGEST_METHODDEF
_BLAKE2_BLAKE2B_UPDATE_METHODDEF
_BLAKE2_BLAKE2B___REDUCE___METHODDEF
{NULL, NULL}
};

Expand Down
17 changes: 17 additions & 0 deletions Modules/_blake2/blake2s_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,29 @@ _blake2_blake2s_hexdigest_impl(BLAKE2sObject *self)
return _Py_strhex((const char *)digest, self->param.digest_length);
}

/*[clinic input]
_blake2.blake2s.__reduce__

Return the digest value as a string of hexadecimal digits.
[clinic start generated code]*/

static PyObject *
_blake2_blake2s___reduce___impl(BLAKE2sObject *self)
/*[clinic end generated code: output=fb085f3da88f0c39 input=49898c2f1ef1ff74]*/
{
PyErr_Format(PyExc_TypeError,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vstinner This function is very redundant with this issue.
Do you have any ideas with better solution?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a public marco :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that exposing it to the public is the proper way.
It's should be a kind of internal utility routine.

"cannot pickle %s object",
Py_TYPE(self)->tp_name);
return NULL;
}


static PyMethodDef py_blake2s_methods[] = {
_BLAKE2_BLAKE2S_COPY_METHODDEF
_BLAKE2_BLAKE2S_DIGEST_METHODDEF
_BLAKE2_BLAKE2S_HEXDIGEST_METHODDEF
_BLAKE2_BLAKE2S_UPDATE_METHODDEF
_BLAKE2_BLAKE2S___REDUCE___METHODDEF
{NULL, NULL}
};

Expand Down
20 changes: 19 additions & 1 deletion Modules/_blake2/clinic/blake2b_impl.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,22 @@ _blake2_blake2b_hexdigest(BLAKE2bObject *self, PyObject *Py_UNUSED(ignored))
{
return _blake2_blake2b_hexdigest_impl(self);
}
/*[clinic end generated code: output=10eb47aba77f192d input=a9049054013a1b77]*/

PyDoc_STRVAR(_blake2_blake2b___reduce____doc__,
"__reduce__($self, /)\n"
"--\n"
"\n"
"Return the digest value as a string of hexadecimal digits.");

#define _BLAKE2_BLAKE2B___REDUCE___METHODDEF \
{"__reduce__", (PyCFunction)_blake2_blake2b___reduce__, METH_NOARGS, _blake2_blake2b___reduce____doc__},

static PyObject *
_blake2_blake2b___reduce___impl(BLAKE2bObject *self);

static PyObject *
_blake2_blake2b___reduce__(BLAKE2bObject *self, PyObject *Py_UNUSED(ignored))
{
return _blake2_blake2b___reduce___impl(self);
}
/*[clinic end generated code: output=fb5bd684d682c259 input=a9049054013a1b77]*/
20 changes: 19 additions & 1 deletion Modules/_blake2/clinic/blake2s_impl.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,22 @@ _blake2_blake2s_hexdigest(BLAKE2sObject *self, PyObject *Py_UNUSED(ignored))
{
return _blake2_blake2s_hexdigest_impl(self);
}
/*[clinic end generated code: output=f7ee8092ed67e9c7 input=a9049054013a1b77]*/

PyDoc_STRVAR(_blake2_blake2s___reduce____doc__,
"__reduce__($self, /)\n"
"--\n"
"\n"
"Return the digest value as a string of hexadecimal digits.");

#define _BLAKE2_BLAKE2S___REDUCE___METHODDEF \
{"__reduce__", (PyCFunction)_blake2_blake2s___reduce__, METH_NOARGS, _blake2_blake2s___reduce____doc__},

static PyObject *
_blake2_blake2s___reduce___impl(BLAKE2sObject *self);

static PyObject *
_blake2_blake2s___reduce__(BLAKE2sObject *self, PyObject *Py_UNUSED(ignored))
{
return _blake2_blake2s___reduce___impl(self);
}
/*[clinic end generated code: output=c1e1e6d51762d97a input=a9049054013a1b77]*/