diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py index 4551011f5ca9ef..190b42664c7f6a 100644 --- a/Lib/test/test_hashlib.py +++ b/Lib/test/test_hashlib.py @@ -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') diff --git a/Modules/_blake2/blake2b_impl.c b/Modules/_blake2/blake2b_impl.c index 8e1acce56b1d29..69ac8757081a46 100644 --- a/Modules/_blake2/blake2b_impl.c +++ b/Modules/_blake2/blake2b_impl.c @@ -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); + 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} }; diff --git a/Modules/_blake2/blake2s_impl.c b/Modules/_blake2/blake2s_impl.c index e1de5df37d0988..a1bae8dbe03c26 100644 --- a/Modules/_blake2/blake2s_impl.c +++ b/Modules/_blake2/blake2s_impl.c @@ -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, + "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} }; diff --git a/Modules/_blake2/clinic/blake2b_impl.c.h b/Modules/_blake2/clinic/blake2b_impl.c.h index 4e74e0885cf238..cbb69bb008253c 100644 --- a/Modules/_blake2/clinic/blake2b_impl.c.h +++ b/Modules/_blake2/clinic/blake2b_impl.c.h @@ -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]*/ diff --git a/Modules/_blake2/clinic/blake2s_impl.c.h b/Modules/_blake2/clinic/blake2s_impl.c.h index 0f0d9835fbfe24..c2a25ffd76510d 100644 --- a/Modules/_blake2/clinic/blake2s_impl.c.h +++ b/Modules/_blake2/clinic/blake2s_impl.c.h @@ -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]*/