← 返回首页
bpo-41052: Optout serialization/deserialization for blake2s/b by corona10 · Pull Request #22189 · python/cpython · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (2) .h  (2) .py  (1) All 3 file types selected Viewed files
Conversations
Failed to load comments. Retry
Loading
Jump to
Jump to file
Failed to load files. Retry
Loading
Diff view
Unified
Split
Hide whitespace
Apply and reload
Show whitespace
Diff view
Unified
Split
Hide whitespace
Apply and reload
9 changes: 9 additions & 0 deletions Lib/test/test_hashlib.py
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

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
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

Maybe a public marco :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

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
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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]*/
Toggle all file notes Toggle all file annotations

Footer

© 2026 GitHub, Inc.