From 501b13c6229f740dd92a0067fdb5486bf2ff1a57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20v=2E=20L=C3=B6wis?= Date: Sun, 27 Jul 2014 14:20:23 +0200 Subject: [PATCH] Issue #20173: Convert sha1, sha256, sha512 and md5 to ArgumentClinic. Patch by Vajrasky Kok. --- Misc/NEWS | 3 + Modules/md5module.c | 176 ++++++++++++++++++++++----- Modules/sha1module.c | 176 ++++++++++++++++++++++----- Modules/sha256module.c | 234 +++++++++++++++++++++++++++-------- Modules/sha512module.c | 268 +++++++++++++++++++++++++++++++++-------- 5 files changed, 699 insertions(+), 158 deletions(-) diff --git a/Misc/NEWS b/Misc/NEWS index 9a75f28b832..483829d1bdc 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -110,6 +110,9 @@ Core and Builtins Library ------- +- Issue #20173: Convert sha1, sha256, sha512 and md5 to ArgumentClinic. + Patch by Vajrasky Kok. + - Fix repr(_socket.socket) on Windows 64-bit: don't fail with OverflowError on closed socket. repr(socket.socket) already works fine. diff --git a/Modules/md5module.c b/Modules/md5module.c index 5cb3d36c9b0..65f5c35a620 100644 --- a/Modules/md5module.c +++ b/Modules/md5module.c @@ -19,6 +19,11 @@ #include "Python.h" #include "hashlib.h" +/*[clinic input] +module _md5 +class MD5Type "MD5object *" "&PyType_Type" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=6e5261719957a912]*/ /* Some useful types */ @@ -332,10 +337,33 @@ MD5_dealloc(PyObject *ptr) /* External methods for a hash object */ -PyDoc_STRVAR(MD5_copy__doc__, "Return a copy of the hash object."); +/*[clinic input] +MD5Type.copy + +Return a copy of the hash object. +[clinic start generated code]*/ + +PyDoc_STRVAR(MD5Type_copy__doc__, +"copy($self, /)\n" +"--\n" +"\n" +"Return a copy of the hash object."); + +#define MD5TYPE_COPY_METHODDEF \ + {"copy", (PyCFunction)MD5Type_copy, METH_NOARGS, MD5Type_copy__doc__}, static PyObject * -MD5_copy(MD5object *self, PyObject *unused) +MD5Type_copy_impl(MD5object *self); + +static PyObject * +MD5Type_copy(MD5object *self, PyObject *Py_UNUSED(ignored)) +{ + return MD5Type_copy_impl(self); +} + +static PyObject * +MD5Type_copy_impl(MD5object *self) +/*[clinic end generated code: output=3b3a88920b3dc7f4 input=2c09e6d2493f3079]*/ { MD5object *newobj; @@ -351,11 +379,33 @@ MD5_copy(MD5object *self, PyObject *unused) return (PyObject *)newobj; } -PyDoc_STRVAR(MD5_digest__doc__, +/*[clinic input] +MD5Type.digest + +Return the digest value as a string of binary data. +[clinic start generated code]*/ + +PyDoc_STRVAR(MD5Type_digest__doc__, +"digest($self, /)\n" +"--\n" +"\n" "Return the digest value as a string of binary data."); +#define MD5TYPE_DIGEST_METHODDEF \ + {"digest", (PyCFunction)MD5Type_digest, METH_NOARGS, MD5Type_digest__doc__}, + static PyObject * -MD5_digest(MD5object *self, PyObject *unused) +MD5Type_digest_impl(MD5object *self); + +static PyObject * +MD5Type_digest(MD5object *self, PyObject *Py_UNUSED(ignored)) +{ + return MD5Type_digest_impl(self); +} + +static PyObject * +MD5Type_digest_impl(MD5object *self) +/*[clinic end generated code: output=7a796b28fa89485f input=7b96e65389412a34]*/ { unsigned char digest[MD5_DIGESTSIZE]; struct md5_state temp; @@ -365,11 +415,33 @@ MD5_digest(MD5object *self, PyObject *unused) return PyBytes_FromStringAndSize((const char *)digest, MD5_DIGESTSIZE); } -PyDoc_STRVAR(MD5_hexdigest__doc__, +/*[clinic input] +MD5Type.hexdigest + +Return the digest value as a string of hexadecimal digits. +[clinic start generated code]*/ + +PyDoc_STRVAR(MD5Type_hexdigest__doc__, +"hexdigest($self, /)\n" +"--\n" +"\n" "Return the digest value as a string of hexadecimal digits."); +#define MD5TYPE_HEXDIGEST_METHODDEF \ + {"hexdigest", (PyCFunction)MD5Type_hexdigest, METH_NOARGS, MD5Type_hexdigest__doc__}, + static PyObject * -MD5_hexdigest(MD5object *self, PyObject *unused) +MD5Type_hexdigest_impl(MD5object *self); + +static PyObject * +MD5Type_hexdigest(MD5object *self, PyObject *Py_UNUSED(ignored)) +{ + return MD5Type_hexdigest_impl(self); +} + +static PyObject * +MD5Type_hexdigest_impl(MD5object *self) +/*[clinic end generated code: output=daa73609f94f92e1 input=b60b19de644798dd]*/ { unsigned char digest[MD5_DIGESTSIZE]; struct md5_state temp; @@ -401,18 +473,30 @@ MD5_hexdigest(MD5object *self, PyObject *unused) return retval; } -PyDoc_STRVAR(MD5_update__doc__, -"Update this hash object's state with the provided string."); +/*[clinic input] +MD5Type.update + + obj: object + / + +Update this hash object's state with the provided string. +[clinic start generated code]*/ + +PyDoc_STRVAR(MD5Type_update__doc__, +"update($self, obj, /)\n" +"--\n" +"\n" +"Update this hash object\'s state with the provided string."); + +#define MD5TYPE_UPDATE_METHODDEF \ + {"update", (PyCFunction)MD5Type_update, METH_O, MD5Type_update__doc__}, static PyObject * -MD5_update(MD5object *self, PyObject *args) +MD5Type_update(MD5object *self, PyObject *obj) +/*[clinic end generated code: output=9d09b6c6cdc6cac3 input=6e1efcd9ecf17032]*/ { - PyObject *obj; Py_buffer buf; - if (!PyArg_ParseTuple(args, "O:update", &obj)) - return NULL; - GET_BUFFER_VIEW_OR_ERROUT(obj, &buf); md5_process(&self->hash_state, buf.buf, buf.len); @@ -423,10 +507,10 @@ MD5_update(MD5object *self, PyObject *args) } static PyMethodDef MD5_methods[] = { - {"copy", (PyCFunction)MD5_copy, METH_NOARGS, MD5_copy__doc__}, - {"digest", (PyCFunction)MD5_digest, METH_NOARGS, MD5_digest__doc__}, - {"hexdigest", (PyCFunction)MD5_hexdigest, METH_NOARGS, MD5_hexdigest__doc__}, - {"update", (PyCFunction)MD5_update, METH_VARARGS, MD5_update__doc__}, + MD5TYPE_COPY_METHODDEF + MD5TYPE_DIGEST_METHODDEF + MD5TYPE_HEXDIGEST_METHODDEF + MD5TYPE_UPDATE_METHODDEF {NULL, NULL} /* sentinel */ }; @@ -502,27 +586,55 @@ static PyTypeObject MD5type = { /* The single module-level function: new() */ -PyDoc_STRVAR(MD5_new__doc__, +/*[clinic input] +_md5.md5 + + string: object(c_default="NULL") = b'' + +Return a new MD5 hash object; optionally initialized with a string. +[clinic start generated code]*/ + +PyDoc_STRVAR(_md5_md5__doc__, +"md5($module, /, string=b\'\')\n" +"--\n" +"\n" "Return a new MD5 hash object; optionally initialized with a string."); +#define _MD5_MD5_METHODDEF \ + {"md5", (PyCFunction)_md5_md5, METH_VARARGS|METH_KEYWORDS, _md5_md5__doc__}, + static PyObject * -MD5_new(PyObject *self, PyObject *args, PyObject *kwdict) +_md5_md5_impl(PyModuleDef *module, PyObject *string); + +static PyObject * +_md5_md5(PyModuleDef *module, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"string", NULL}; + PyObject *string = NULL; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "|O:md5", _keywords, + &string)) + goto exit; + return_value = _md5_md5_impl(module, string); + +exit: + return return_value; +} + +static PyObject * +_md5_md5_impl(PyModuleDef *module, PyObject *string) +/*[clinic end generated code: output=1039e912d919880e input=d12ef8f72d684f7b]*/ { - static char *kwlist[] = {"string", NULL}; MD5object *new; - PyObject *data_obj = NULL; Py_buffer buf; - if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist, - &data_obj)) { - return NULL; - } - - if (data_obj) - GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf); + if (string) + GET_BUFFER_VIEW_OR_ERROUT(string, &buf); if ((new = newMD5object()) == NULL) { - if (data_obj) + if (string) PyBuffer_Release(&buf); return NULL; } @@ -531,11 +643,11 @@ MD5_new(PyObject *self, PyObject *args, PyObject *kwdict) if (PyErr_Occurred()) { Py_DECREF(new); - if (data_obj) + if (string) PyBuffer_Release(&buf); return NULL; } - if (data_obj) { + if (string) { md5_process(&new->hash_state, buf.buf, buf.len); PyBuffer_Release(&buf); } @@ -547,7 +659,7 @@ MD5_new(PyObject *self, PyObject *args, PyObject *kwdict) /* List of functions exported by this module */ static struct PyMethodDef MD5_functions[] = { - {"md5", (PyCFunction)MD5_new, METH_VARARGS|METH_KEYWORDS, MD5_new__doc__}, + _MD5_MD5_METHODDEF {NULL, NULL} /* Sentinel */ }; diff --git a/Modules/sha1module.c b/Modules/sha1module.c index b44fe189d26..8961de48d8f 100644 --- a/Modules/sha1module.c +++ b/Modules/sha1module.c @@ -19,6 +19,11 @@ #include "Python.h" #include "hashlib.h" +/*[clinic input] +module _sha1 +class SHA1Type "SHA1object *" "&PyType_Type" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3dc9a20d1becb759]*/ /* Some useful types */ @@ -309,10 +314,33 @@ SHA1_dealloc(PyObject *ptr) /* External methods for a hash object */ -PyDoc_STRVAR(SHA1_copy__doc__, "Return a copy of the hash object."); +/*[clinic input] +SHA1Type.copy + +Return a copy of the hash object. +[clinic start generated code]*/ + +PyDoc_STRVAR(SHA1Type_copy__doc__, +"copy($self, /)\n" +"--\n" +"\n" +"Return a copy of the hash object."); + +#define SHA1TYPE_COPY_METHODDEF \ + {"copy", (PyCFunction)SHA1Type_copy, METH_NOARGS, SHA1Type_copy__doc__}, static PyObject * -SHA1_copy(SHA1object *self, PyObject *unused) +SHA1Type_copy_impl(SHA1object *self); + +static PyObject * +SHA1Type_copy(SHA1object *self, PyObject *Py_UNUSED(ignored)) +{ + return SHA1Type_copy_impl(self); +} + +static PyObject * +SHA1Type_copy_impl(SHA1object *self) +/*[clinic end generated code: output=1a320e75a7444098 input=b7eae10df6f89b36]*/ { SHA1object *newobj; @@ -323,11 +351,33 @@ SHA1_copy(SHA1object *self, PyObject *unused) return (PyObject *)newobj; } -PyDoc_STRVAR(SHA1_digest__doc__, +/*[clinic input] +SHA1Type.digest + +Return the digest value as a string of binary data. +[clinic start generated code]*/ + +PyDoc_STRVAR(SHA1Type_digest__doc__, +"digest($self, /)\n" +"--\n" +"\n" "Return the digest value as a string of binary data."); +#define SHA1TYPE_DIGEST_METHODDEF \ + {"digest", (PyCFunction)SHA1Type_digest, METH_NOARGS, SHA1Type_digest__doc__}, + static PyObject * -SHA1_digest(SHA1object *self, PyObject *unused) +SHA1Type_digest_impl(SHA1object *self); + +static PyObject * +SHA1Type_digest(SHA1object *self, PyObject *Py_UNUSED(ignored)) +{ + return SHA1Type_digest_impl(self); +} + +static PyObject * +SHA1Type_digest_impl(SHA1object *self) +/*[clinic end generated code: output=c4920f75228bfbfd input=205d47e1927fd009]*/ { unsigned char digest[SHA1_DIGESTSIZE]; struct sha1_state temp; @@ -337,11 +387,33 @@ SHA1_digest(SHA1object *self, PyObject *unused) return PyBytes_FromStringAndSize((const char *)digest, SHA1_DIGESTSIZE); } -PyDoc_STRVAR(SHA1_hexdigest__doc__, +/*[clinic input] +SHA1Type.hexdigest + +Return the digest value as a string of hexadecimal digits. +[clinic start generated code]*/ + +PyDoc_STRVAR(SHA1Type_hexdigest__doc__, +"hexdigest($self, /)\n" +"--\n" +"\n" "Return the digest value as a string of hexadecimal digits."); +#define SHA1TYPE_HEXDIGEST_METHODDEF \ + {"hexdigest", (PyCFunction)SHA1Type_hexdigest, METH_NOARGS, SHA1Type_hexdigest__doc__}, + static PyObject * -SHA1_hexdigest(SHA1object *self, PyObject *unused) +SHA1Type_hexdigest_impl(SHA1object *self); + +static PyObject * +SHA1Type_hexdigest(SHA1object *self, PyObject *Py_UNUSED(ignored)) +{ + return SHA1Type_hexdigest_impl(self); +} + +static PyObject * +SHA1Type_hexdigest_impl(SHA1object *self) +/*[clinic end generated code: output=6e345aac201887b2 input=97691055c0c74ab0]*/ { unsigned char digest[SHA1_DIGESTSIZE]; struct sha1_state temp; @@ -373,18 +445,30 @@ SHA1_hexdigest(SHA1object *self, PyObject *unused) return retval; } -PyDoc_STRVAR(SHA1_update__doc__, -"Update this hash object's state with the provided string."); +/*[clinic input] +SHA1Type.update + + obj: object + / + +Update this hash object's state with the provided string. +[clinic start generated code]*/ + +PyDoc_STRVAR(SHA1Type_update__doc__, +"update($self, obj, /)\n" +"--\n" +"\n" +"Update this hash object\'s state with the provided string."); + +#define SHA1TYPE_UPDATE_METHODDEF \ + {"update", (PyCFunction)SHA1Type_update, METH_O, SHA1Type_update__doc__}, static PyObject * -SHA1_update(SHA1object *self, PyObject *args) +SHA1Type_update(SHA1object *self, PyObject *obj) +/*[clinic end generated code: output=ab20a86a25e7d255 input=aad8e07812edbba3]*/ { - PyObject *obj; Py_buffer buf; - if (!PyArg_ParseTuple(args, "O:update", &obj)) - return NULL; - GET_BUFFER_VIEW_OR_ERROUT(obj, &buf); sha1_process(&self->hash_state, buf.buf, buf.len); @@ -395,10 +479,10 @@ SHA1_update(SHA1object *self, PyObject *args) } static PyMethodDef SHA1_methods[] = { - {"copy", (PyCFunction)SHA1_copy, METH_NOARGS, SHA1_copy__doc__}, - {"digest", (PyCFunction)SHA1_digest, METH_NOARGS, SHA1_digest__doc__}, - {"hexdigest", (PyCFunction)SHA1_hexdigest, METH_NOARGS, SHA1_hexdigest__doc__}, - {"update", (PyCFunction)SHA1_update, METH_VARARGS, SHA1_update__doc__}, + SHA1TYPE_COPY_METHODDEF + SHA1TYPE_DIGEST_METHODDEF + SHA1TYPE_HEXDIGEST_METHODDEF + SHA1TYPE_UPDATE_METHODDEF {NULL, NULL} /* sentinel */ }; @@ -474,27 +558,55 @@ static PyTypeObject SHA1type = { /* The single module-level function: new() */ -PyDoc_STRVAR(SHA1_new__doc__, +/*[clinic input] +_sha1.sha1 + + string: object(c_default="NULL") = b'' + +Return a new SHA1 hash object; optionally initialized with a string. +[clinic start generated code]*/ + +PyDoc_STRVAR(_sha1_sha1__doc__, +"sha1($module, /, string=b\'\')\n" +"--\n" +"\n" "Return a new SHA1 hash object; optionally initialized with a string."); +#define _SHA1_SHA1_METHODDEF \ + {"sha1", (PyCFunction)_sha1_sha1, METH_VARARGS|METH_KEYWORDS, _sha1_sha1__doc__}, + static PyObject * -SHA1_new(PyObject *self, PyObject *args, PyObject *kwdict) +_sha1_sha1_impl(PyModuleDef *module, PyObject *string); + +static PyObject * +_sha1_sha1(PyModuleDef *module, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"string", NULL}; + PyObject *string = NULL; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "|O:sha1", _keywords, + &string)) + goto exit; + return_value = _sha1_sha1_impl(module, string); + +exit: + return return_value; +} + +static PyObject * +_sha1_sha1_impl(PyModuleDef *module, PyObject *string) +/*[clinic end generated code: output=c9068552f07b8954 input=27ea54281d995ec2]*/ { - static char *kwlist[] = {"string", NULL}; SHA1object *new; - PyObject *data_obj = NULL; Py_buffer buf; - if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist, - &data_obj)) { - return NULL; - } - - if (data_obj) - GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf); + if (string) + GET_BUFFER_VIEW_OR_ERROUT(string, &buf); if ((new = newSHA1object()) == NULL) { - if (data_obj) + if (string) PyBuffer_Release(&buf); return NULL; } @@ -503,11 +615,11 @@ SHA1_new(PyObject *self, PyObject *args, PyObject *kwdict) if (PyErr_Occurred()) { Py_DECREF(new); - if (data_obj) + if (string) PyBuffer_Release(&buf); return NULL; } - if (data_obj) { + if (string) { sha1_process(&new->hash_state, buf.buf, buf.len); PyBuffer_Release(&buf); } @@ -519,7 +631,7 @@ SHA1_new(PyObject *self, PyObject *args, PyObject *kwdict) /* List of functions exported by this module */ static struct PyMethodDef SHA1_functions[] = { - {"sha1",(PyCFunction)SHA1_new, METH_VARARGS|METH_KEYWORDS,SHA1_new__doc__}, + _SHA1_SHA1_METHODDEF {NULL, NULL} /* Sentinel */ }; diff --git a/Modules/sha256module.c b/Modules/sha256module.c index b05bfc172f5..7b1488696f4 100644 --- a/Modules/sha256module.c +++ b/Modules/sha256module.c @@ -20,6 +20,11 @@ #include "structmember.h" #include "hashlib.h" +/*[clinic input] +module _sha256 +class SHA256Type "SHAobject *" "&PyType_Type" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=71a39174d4f0a744]*/ /* Some useful types */ @@ -393,10 +398,33 @@ SHA_dealloc(PyObject *ptr) /* External methods for a hash object */ -PyDoc_STRVAR(SHA256_copy__doc__, "Return a copy of the hash object."); +/*[clinic input] +SHA256Type.copy + +Return a copy of the hash object. +[clinic start generated code]*/ + +PyDoc_STRVAR(SHA256Type_copy__doc__, +"copy($self, /)\n" +"--\n" +"\n" +"Return a copy of the hash object."); + +#define SHA256TYPE_COPY_METHODDEF \ + {"copy", (PyCFunction)SHA256Type_copy, METH_NOARGS, SHA256Type_copy__doc__}, static PyObject * -SHA256_copy(SHAobject *self, PyObject *unused) +SHA256Type_copy_impl(SHAobject *self); + +static PyObject * +SHA256Type_copy(SHAobject *self, PyObject *Py_UNUSED(ignored)) +{ + return SHA256Type_copy_impl(self); +} + +static PyObject * +SHA256Type_copy_impl(SHAobject *self) +/*[clinic end generated code: output=f716c39d3f81c27c input=f58840a618d4f2a7]*/ { SHAobject *newobj; @@ -412,11 +440,33 @@ SHA256_copy(SHAobject *self, PyObject *unused) return (PyObject *)newobj; } -PyDoc_STRVAR(SHA256_digest__doc__, +/*[clinic input] +SHA256Type.digest + +Return the digest value as a string of binary data. +[clinic start generated code]*/ + +PyDoc_STRVAR(SHA256Type_digest__doc__, +"digest($self, /)\n" +"--\n" +"\n" "Return the digest value as a string of binary data."); +#define SHA256TYPE_DIGEST_METHODDEF \ + {"digest", (PyCFunction)SHA256Type_digest, METH_NOARGS, SHA256Type_digest__doc__}, + static PyObject * -SHA256_digest(SHAobject *self, PyObject *unused) +SHA256Type_digest_impl(SHAobject *self); + +static PyObject * +SHA256Type_digest(SHAobject *self, PyObject *Py_UNUSED(ignored)) +{ + return SHA256Type_digest_impl(self); +} + +static PyObject * +SHA256Type_digest_impl(SHAobject *self) +/*[clinic end generated code: output=72d34723d7bb694c input=1fb752e58954157d]*/ { unsigned char digest[SHA_DIGESTSIZE]; SHAobject temp; @@ -426,11 +476,33 @@ SHA256_digest(SHAobject *self, PyObject *unused) return PyBytes_FromStringAndSize((const char *)digest, self->digestsize); } -PyDoc_STRVAR(SHA256_hexdigest__doc__, +/*[clinic input] +SHA256Type.hexdigest + +Return the digest value as a string of hexadecimal digits. +[clinic start generated code]*/ + +PyDoc_STRVAR(SHA256Type_hexdigest__doc__, +"hexdigest($self, /)\n" +"--\n" +"\n" "Return the digest value as a string of hexadecimal digits."); +#define SHA256TYPE_HEXDIGEST_METHODDEF \ + {"hexdigest", (PyCFunction)SHA256Type_hexdigest, METH_NOARGS, SHA256Type_hexdigest__doc__}, + static PyObject * -SHA256_hexdigest(SHAobject *self, PyObject *unused) +SHA256Type_hexdigest_impl(SHAobject *self); + +static PyObject * +SHA256Type_hexdigest(SHAobject *self, PyObject *Py_UNUSED(ignored)) +{ + return SHA256Type_hexdigest_impl(self); +} + +static PyObject * +SHA256Type_hexdigest_impl(SHAobject *self) +/*[clinic end generated code: output=3687aa6183c7d27f input=0cc4c714693010d1]*/ { unsigned char digest[SHA_DIGESTSIZE]; SHAobject temp; @@ -462,18 +534,30 @@ SHA256_hexdigest(SHAobject *self, PyObject *unused) return retval; } -PyDoc_STRVAR(SHA256_update__doc__, -"Update this hash object's state with the provided string."); +/*[clinic input] +SHA256Type.update + + obj: object + / + +Update this hash object's state with the provided string. +[clinic start generated code]*/ + +PyDoc_STRVAR(SHA256Type_update__doc__, +"update($self, obj, /)\n" +"--\n" +"\n" +"Update this hash object\'s state with the provided string."); + +#define SHA256TYPE_UPDATE_METHODDEF \ + {"update", (PyCFunction)SHA256Type_update, METH_O, SHA256Type_update__doc__}, static PyObject * -SHA256_update(SHAobject *self, PyObject *args) +SHA256Type_update(SHAobject *self, PyObject *obj) +/*[clinic end generated code: output=b47f53d7cbeabee4 input=b2d449d5b30f0f5a]*/ { - PyObject *obj; Py_buffer buf; - if (!PyArg_ParseTuple(args, "O:update", &obj)) - return NULL; - GET_BUFFER_VIEW_OR_ERROUT(obj, &buf); sha_update(self, buf.buf, buf.len); @@ -484,10 +568,10 @@ SHA256_update(SHAobject *self, PyObject *args) } static PyMethodDef SHA_methods[] = { - {"copy", (PyCFunction)SHA256_copy, METH_NOARGS, SHA256_copy__doc__}, - {"digest", (PyCFunction)SHA256_digest, METH_NOARGS, SHA256_digest__doc__}, - {"hexdigest", (PyCFunction)SHA256_hexdigest, METH_NOARGS, SHA256_hexdigest__doc__}, - {"update", (PyCFunction)SHA256_update, METH_VARARGS, SHA256_update__doc__}, + SHA256TYPE_COPY_METHODDEF + SHA256TYPE_DIGEST_METHODDEF + SHA256TYPE_HEXDIGEST_METHODDEF + SHA256TYPE_UPDATE_METHODDEF {NULL, NULL} /* sentinel */ }; @@ -594,27 +678,55 @@ static PyTypeObject SHA256type = { /* The single module-level function: new() */ -PyDoc_STRVAR(SHA256_new__doc__, +/*[clinic input] +_sha256.sha256 + + string: object(c_default="NULL") = b'' + +Return a new SHA-256 hash object; optionally initialized with a string. +[clinic start generated code]*/ + +PyDoc_STRVAR(_sha256_sha256__doc__, +"sha256($module, /, string=b\'\')\n" +"--\n" +"\n" "Return a new SHA-256 hash object; optionally initialized with a string."); +#define _SHA256_SHA256_METHODDEF \ + {"sha256", (PyCFunction)_sha256_sha256, METH_VARARGS|METH_KEYWORDS, _sha256_sha256__doc__}, + static PyObject * -SHA256_new(PyObject *self, PyObject *args, PyObject *kwdict) +_sha256_sha256_impl(PyModuleDef *module, PyObject *string); + +static PyObject * +_sha256_sha256(PyModuleDef *module, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"string", NULL}; + PyObject *string = NULL; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "|O:sha256", _keywords, + &string)) + goto exit; + return_value = _sha256_sha256_impl(module, string); + +exit: + return return_value; +} + +static PyObject * +_sha256_sha256_impl(PyModuleDef *module, PyObject *string) +/*[clinic end generated code: output=4b1263d1e2fcdb98 input=09cce3fb855056b2]*/ { - static char *kwlist[] = {"string", NULL}; SHAobject *new; - PyObject *data_obj = NULL; Py_buffer buf; - if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist, - &data_obj)) { - return NULL; - } - - if (data_obj) - GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf); + if (string) + GET_BUFFER_VIEW_OR_ERROUT(string, &buf); if ((new = newSHA256object()) == NULL) { - if (data_obj) + if (string) PyBuffer_Release(&buf); return NULL; } @@ -623,11 +735,11 @@ SHA256_new(PyObject *self, PyObject *args, PyObject *kwdict) if (PyErr_Occurred()) { Py_DECREF(new); - if (data_obj) + if (string) PyBuffer_Release(&buf); return NULL; } - if (data_obj) { + if (string) { sha_update(new, buf.buf, buf.len); PyBuffer_Release(&buf); } @@ -635,27 +747,55 @@ SHA256_new(PyObject *self, PyObject *args, PyObject *kwdict) return (PyObject *)new; } -PyDoc_STRVAR(SHA224_new__doc__, +/*[clinic input] +_sha256.sha224 + + string: object(c_default="NULL") = b'' + +Return a new SHA-224 hash object; optionally initialized with a string. +[clinic start generated code]*/ + +PyDoc_STRVAR(_sha256_sha224__doc__, +"sha224($module, /, string=b\'\')\n" +"--\n" +"\n" "Return a new SHA-224 hash object; optionally initialized with a string."); +#define _SHA256_SHA224_METHODDEF \ + {"sha224", (PyCFunction)_sha256_sha224, METH_VARARGS|METH_KEYWORDS, _sha256_sha224__doc__}, + static PyObject * -SHA224_new(PyObject *self, PyObject *args, PyObject *kwdict) +_sha256_sha224_impl(PyModuleDef *module, PyObject *string); + +static PyObject * +_sha256_sha224(PyModuleDef *module, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"string", NULL}; + PyObject *string = NULL; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "|O:sha224", _keywords, + &string)) + goto exit; + return_value = _sha256_sha224_impl(module, string); + +exit: + return return_value; +} + +static PyObject * +_sha256_sha224_impl(PyModuleDef *module, PyObject *string) +/*[clinic end generated code: output=4dde0eb1cdaebc06 input=27a04ba24c353a73]*/ { - static char *kwlist[] = {"string", NULL}; SHAobject *new; - PyObject *data_obj = NULL; Py_buffer buf; - if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist, - &data_obj)) { - return NULL; - } - - if (data_obj) - GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf); + if (string) + GET_BUFFER_VIEW_OR_ERROUT(string, &buf); if ((new = newSHA224object()) == NULL) { - if (data_obj) + if (string) PyBuffer_Release(&buf); return NULL; } @@ -664,11 +804,11 @@ SHA224_new(PyObject *self, PyObject *args, PyObject *kwdict) if (PyErr_Occurred()) { Py_DECREF(new); - if (data_obj) + if (string) PyBuffer_Release(&buf); return NULL; } - if (data_obj) { + if (string) { sha_update(new, buf.buf, buf.len); PyBuffer_Release(&buf); } @@ -680,8 +820,8 @@ SHA224_new(PyObject *self, PyObject *args, PyObject *kwdict) /* List of functions exported by this module */ static struct PyMethodDef SHA_functions[] = { - {"sha256", (PyCFunction)SHA256_new, METH_VARARGS|METH_KEYWORDS, SHA256_new__doc__}, - {"sha224", (PyCFunction)SHA224_new, METH_VARARGS|METH_KEYWORDS, SHA224_new__doc__}, + _SHA256_SHA256_METHODDEF + _SHA256_SHA224_METHODDEF {NULL, NULL} /* Sentinel */ }; diff --git a/Modules/sha512module.c b/Modules/sha512module.c index 47c57e5c428..843f95d5c61 100644 --- a/Modules/sha512module.c +++ b/Modules/sha512module.c @@ -20,6 +20,12 @@ #include "structmember.h" #include "hashlib.h" +/*[clinic input] +module _sha512 +class SHA512Type "SHAobject *" "&PyType_Type" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=81a3ccde92bcfe8d]*/ + #ifdef PY_LONG_LONG /* If no PY_LONG_LONG, don't compile anything! */ /* Some useful types */ @@ -459,10 +465,33 @@ SHA512_dealloc(PyObject *ptr) /* External methods for a hash object */ -PyDoc_STRVAR(SHA512_copy__doc__, "Return a copy of the hash object."); +/*[clinic input] +SHA512Type.copy + +Return a copy of the hash object. +[clinic start generated code]*/ + +PyDoc_STRVAR(SHA512Type_copy__doc__, +"copy($self, /)\n" +"--\n" +"\n" +"Return a copy of the hash object."); + +#define SHA512TYPE_COPY_METHODDEF \ + {"copy", (PyCFunction)SHA512Type_copy, METH_NOARGS, SHA512Type_copy__doc__}, static PyObject * -SHA512_copy(SHAobject *self, PyObject *unused) +SHA512Type_copy_impl(SHAobject *self); + +static PyObject * +SHA512Type_copy(SHAobject *self, PyObject *Py_UNUSED(ignored)) +{ + return SHA512Type_copy_impl(self); +} + +static PyObject * +SHA512Type_copy_impl(SHAobject *self) +/*[clinic end generated code: output=14f8e6ce9c61ece0 input=9f5f31e6c457776a]*/ { SHAobject *newobj; @@ -478,11 +507,33 @@ SHA512_copy(SHAobject *self, PyObject *unused) return (PyObject *)newobj; } -PyDoc_STRVAR(SHA512_digest__doc__, +/*[clinic input] +SHA512Type.digest + +Return the digest value as a string of binary data. +[clinic start generated code]*/ + +PyDoc_STRVAR(SHA512Type_digest__doc__, +"digest($self, /)\n" +"--\n" +"\n" "Return the digest value as a string of binary data."); +#define SHA512TYPE_DIGEST_METHODDEF \ + {"digest", (PyCFunction)SHA512Type_digest, METH_NOARGS, SHA512Type_digest__doc__}, + static PyObject * -SHA512_digest(SHAobject *self, PyObject *unused) +SHA512Type_digest_impl(SHAobject *self); + +static PyObject * +SHA512Type_digest(SHAobject *self, PyObject *Py_UNUSED(ignored)) +{ + return SHA512Type_digest_impl(self); +} + +static PyObject * +SHA512Type_digest_impl(SHAobject *self) +/*[clinic end generated code: output=be8de024b232977e input=60c2cede9e023018]*/ { unsigned char digest[SHA_DIGESTSIZE]; SHAobject temp; @@ -492,11 +543,33 @@ SHA512_digest(SHAobject *self, PyObject *unused) return PyBytes_FromStringAndSize((const char *)digest, self->digestsize); } -PyDoc_STRVAR(SHA512_hexdigest__doc__, +/*[clinic input] +SHA512Type.hexdigest + +Return the digest value as a string of hexadecimal digits. +[clinic start generated code]*/ + +PyDoc_STRVAR(SHA512Type_hexdigest__doc__, +"hexdigest($self, /)\n" +"--\n" +"\n" "Return the digest value as a string of hexadecimal digits."); +#define SHA512TYPE_HEXDIGEST_METHODDEF \ + {"hexdigest", (PyCFunction)SHA512Type_hexdigest, METH_NOARGS, SHA512Type_hexdigest__doc__}, + static PyObject * -SHA512_hexdigest(SHAobject *self, PyObject *unused) +SHA512Type_hexdigest_impl(SHAobject *self); + +static PyObject * +SHA512Type_hexdigest(SHAobject *self, PyObject *Py_UNUSED(ignored)) +{ + return SHA512Type_hexdigest_impl(self); +} + +static PyObject * +SHA512Type_hexdigest_impl(SHAobject *self) +/*[clinic end generated code: output=28a4ab2f9a1781b8 input=498b877b25cbe0a2]*/ { unsigned char digest[SHA_DIGESTSIZE]; SHAobject temp; @@ -528,18 +601,30 @@ SHA512_hexdigest(SHAobject *self, PyObject *unused) return retval; } -PyDoc_STRVAR(SHA512_update__doc__, -"Update this hash object's state with the provided string."); +/*[clinic input] +SHA512Type.update + + obj: object + / + +Update this hash object's state with the provided string. +[clinic start generated code]*/ + +PyDoc_STRVAR(SHA512Type_update__doc__, +"update($self, obj, /)\n" +"--\n" +"\n" +"Update this hash object\'s state with the provided string."); + +#define SHA512TYPE_UPDATE_METHODDEF \ + {"update", (PyCFunction)SHA512Type_update, METH_O, SHA512Type_update__doc__}, static PyObject * -SHA512_update(SHAobject *self, PyObject *args) +SHA512Type_update(SHAobject *self, PyObject *obj) +/*[clinic end generated code: output=6be574cdc3a9c52d input=ded2b46656566283]*/ { - PyObject *obj; Py_buffer buf; - if (!PyArg_ParseTuple(args, "O:update", &obj)) - return NULL; - GET_BUFFER_VIEW_OR_ERROUT(obj, &buf); sha512_update(self, buf.buf, buf.len); @@ -548,12 +633,32 @@ SHA512_update(SHAobject *self, PyObject *args) Py_INCREF(Py_None); return Py_None; } +/*[clinic input] +dump buffer +[clinic start generated code]*/ + +#ifndef SHA512TYPE_COPY_METHODDEF + #define SHA512TYPE_COPY_METHODDEF +#endif /* !defined(SHA512TYPE_COPY_METHODDEF) */ + +#ifndef SHA512TYPE_DIGEST_METHODDEF + #define SHA512TYPE_DIGEST_METHODDEF +#endif /* !defined(SHA512TYPE_DIGEST_METHODDEF) */ + +#ifndef SHA512TYPE_HEXDIGEST_METHODDEF + #define SHA512TYPE_HEXDIGEST_METHODDEF +#endif /* !defined(SHA512TYPE_HEXDIGEST_METHODDEF) */ + +#ifndef SHA512TYPE_UPDATE_METHODDEF + #define SHA512TYPE_UPDATE_METHODDEF +#endif /* !defined(SHA512TYPE_UPDATE_METHODDEF) */ +/*[clinic end generated code: output=de713947d31130e9 input=524ce2e021e4eba6]*/ static PyMethodDef SHA_methods[] = { - {"copy", (PyCFunction)SHA512_copy, METH_NOARGS, SHA512_copy__doc__}, - {"digest", (PyCFunction)SHA512_digest, METH_NOARGS, SHA512_digest__doc__}, - {"hexdigest", (PyCFunction)SHA512_hexdigest, METH_NOARGS, SHA512_hexdigest__doc__}, - {"update", (PyCFunction)SHA512_update, METH_VARARGS, SHA512_update__doc__}, + SHA512TYPE_COPY_METHODDEF + SHA512TYPE_DIGEST_METHODDEF + SHA512TYPE_HEXDIGEST_METHODDEF + SHA512TYPE_UPDATE_METHODDEF {NULL, NULL} /* sentinel */ }; @@ -660,27 +765,55 @@ static PyTypeObject SHA512type = { /* The single module-level function: new() */ -PyDoc_STRVAR(SHA512_new__doc__, +/*[clinic input] +_sha512.sha512 + + string: object(c_default="NULL") = b'' + +Return a new SHA-512 hash object; optionally initialized with a string. +[clinic start generated code]*/ + +PyDoc_STRVAR(_sha512_sha512__doc__, +"sha512($module, /, string=b\'\')\n" +"--\n" +"\n" "Return a new SHA-512 hash object; optionally initialized with a string."); +#define _SHA512_SHA512_METHODDEF \ + {"sha512", (PyCFunction)_sha512_sha512, METH_VARARGS|METH_KEYWORDS, _sha512_sha512__doc__}, + static PyObject * -SHA512_new(PyObject *self, PyObject *args, PyObject *kwdict) +_sha512_sha512_impl(PyModuleDef *module, PyObject *string); + +static PyObject * +_sha512_sha512(PyModuleDef *module, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"string", NULL}; + PyObject *string = NULL; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "|O:sha512", _keywords, + &string)) + goto exit; + return_value = _sha512_sha512_impl(module, string); + +exit: + return return_value; +} + +static PyObject * +_sha512_sha512_impl(PyModuleDef *module, PyObject *string) +/*[clinic end generated code: output=9e39b11115f36878 input=e69bad9ae9b6a308]*/ { - static char *kwlist[] = {"string", NULL}; SHAobject *new; - PyObject *data_obj = NULL; Py_buffer buf; - if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist, - &data_obj)) { - return NULL; - } - - if (data_obj) - GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf); + if (string) + GET_BUFFER_VIEW_OR_ERROUT(string, &buf); if ((new = newSHA512object()) == NULL) { - if (data_obj) + if (string) PyBuffer_Release(&buf); return NULL; } @@ -689,11 +822,11 @@ SHA512_new(PyObject *self, PyObject *args, PyObject *kwdict) if (PyErr_Occurred()) { Py_DECREF(new); - if (data_obj) + if (string) PyBuffer_Release(&buf); return NULL; } - if (data_obj) { + if (string) { sha512_update(new, buf.buf, buf.len); PyBuffer_Release(&buf); } @@ -701,27 +834,55 @@ SHA512_new(PyObject *self, PyObject *args, PyObject *kwdict) return (PyObject *)new; } -PyDoc_STRVAR(SHA384_new__doc__, +/*[clinic input] +_sha512.sha384 + + string: object(c_default="NULL") = b'' + +Return a new SHA-384 hash object; optionally initialized with a string. +[clinic start generated code]*/ + +PyDoc_STRVAR(_sha512_sha384__doc__, +"sha384($module, /, string=b\'\')\n" +"--\n" +"\n" "Return a new SHA-384 hash object; optionally initialized with a string."); +#define _SHA512_SHA384_METHODDEF \ + {"sha384", (PyCFunction)_sha512_sha384, METH_VARARGS|METH_KEYWORDS, _sha512_sha384__doc__}, + static PyObject * -SHA384_new(PyObject *self, PyObject *args, PyObject *kwdict) +_sha512_sha384_impl(PyModuleDef *module, PyObject *string); + +static PyObject * +_sha512_sha384(PyModuleDef *module, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"string", NULL}; + PyObject *string = NULL; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "|O:sha384", _keywords, + &string)) + goto exit; + return_value = _sha512_sha384_impl(module, string); + +exit: + return return_value; +} + +static PyObject * +_sha512_sha384_impl(PyModuleDef *module, PyObject *string) +/*[clinic end generated code: output=397c6fba3525b93a input=c9327788d4ea4545]*/ { - static char *kwlist[] = {"string", NULL}; SHAobject *new; - PyObject *data_obj = NULL; Py_buffer buf; - if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist, - &data_obj)) { - return NULL; - } - - if (data_obj) - GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf); + if (string) + GET_BUFFER_VIEW_OR_ERROUT(string, &buf); if ((new = newSHA384object()) == NULL) { - if (data_obj) + if (string) PyBuffer_Release(&buf); return NULL; } @@ -730,11 +891,11 @@ SHA384_new(PyObject *self, PyObject *args, PyObject *kwdict) if (PyErr_Occurred()) { Py_DECREF(new); - if (data_obj) + if (string) PyBuffer_Release(&buf); return NULL; } - if (data_obj) { + if (string) { sha512_update(new, buf.buf, buf.len); PyBuffer_Release(&buf); } @@ -743,11 +904,24 @@ SHA384_new(PyObject *self, PyObject *args, PyObject *kwdict) } +/*[clinic input] +dump buffer +[clinic start generated code]*/ + +#ifndef _SHA512_SHA512_METHODDEF + #define _SHA512_SHA512_METHODDEF +#endif /* !defined(_SHA512_SHA512_METHODDEF) */ + +#ifndef _SHA512_SHA384_METHODDEF + #define _SHA512_SHA384_METHODDEF +#endif /* !defined(_SHA512_SHA384_METHODDEF) */ +/*[clinic end generated code: output=69d84aa9445b01d8 input=524ce2e021e4eba6]*/ + /* List of functions exported by this module */ static struct PyMethodDef SHA_functions[] = { - {"sha512", (PyCFunction)SHA512_new, METH_VARARGS|METH_KEYWORDS, SHA512_new__doc__}, - {"sha384", (PyCFunction)SHA384_new, METH_VARARGS|METH_KEYWORDS, SHA384_new__doc__}, + _SHA512_SHA512_METHODDEF + _SHA512_SHA384_METHODDEF {NULL, NULL} /* Sentinel */ };