diff -r 1f3242fb0c9c Modules/sha512module.c --- a/Modules/sha512module.c Thu Jan 09 19:03:32 2014 -0500 +++ b/Modules/sha512module.c Fri Jan 10 16:46:09 2014 +0800 @@ -38,6 +38,17 @@ #define SHA_BLOCKSIZE 128 #define SHA_DIGESTSIZE 64 +/*[clinic input] +module _sha512 +class _sha512.SHA512 +[clinic start generated code]*/ +/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ +/*[python input] +class SHAobject_converter(self_converter): + type = "SHAobject *" +[python start generated code]*/ +/*[python end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ + /* The structure for storing SHA info */ typedef struct { @@ -528,18 +539,28 @@ return retval; } -PyDoc_STRVAR(SHA512_update__doc__, -"Update this hash object's state with the provided string."); +/*[clinic input] +_sha512.SHA512.update + self: SHAobject + obj: object + / + +Update this hash object's state with the provided string. +[clinic start generated code]*/ + +PyDoc_STRVAR(_sha512_SHA512_update__doc__, +"update(obj)\n" +"Update this hash object\'s state with the provided string."); + +#define _SHA512_SHA512_UPDATE_METHODDEF \ + {"update", (PyCFunction)_sha512_SHA512_update, METH_O, _sha512_SHA512_update__doc__}, static PyObject * -SHA512_update(SHAobject *self, PyObject *args) +_sha512_SHA512_update(SHAobject *self, PyObject *obj) +/*[clinic end generated code: checksum=5ffc03fea5d43dede86340fc71bf1d319a36955a]*/ { - 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); @@ -553,7 +574,7 @@ {"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__}, + _SHA512_SHA512_UPDATE_METHODDEF {NULL, NULL} /* sentinel */ }; @@ -660,27 +681,53 @@ /* The single module-level function: new() */ -PyDoc_STRVAR(SHA512_new__doc__, +/*[clinic input] +_sha512.SHA512.sha512 + + string: object = None + +Return a new SHA-512 hash object; optionally initialized with a string. +[clinic start generated code]*/ + +PyDoc_STRVAR(_sha512_SHA512_sha512__doc__, +"sha512(string=None)\n" "Return a new SHA-512 hash object; optionally initialized with a string."); +#define _SHA512_SHA512_SHA512_METHODDEF \ + {"sha512", (PyCFunction)_sha512_SHA512_sha512, METH_VARARGS|METH_KEYWORDS, _sha512_SHA512_sha512__doc__}, + static PyObject * -SHA512_new(PyObject *self, PyObject *args, PyObject *kwdict) +_sha512_SHA512_sha512_impl(PyObject *self, PyObject *string); + +static PyObject * +_sha512_SHA512_sha512(PyObject *self, PyObject *args, PyObject *kwargs) { - static char *kwlist[] = {"string", NULL}; + PyObject *return_value = NULL; + static char *_keywords[] = {"string", NULL}; + PyObject *string = Py_None; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "|O:sha512", _keywords, + &string)) + goto exit; + return_value = _sha512_SHA512_sha512_impl(self, string); + +exit: + return return_value; +} + +static PyObject * +_sha512_SHA512_sha512_impl(PyObject *self, PyObject *string) +/*[clinic end generated code: checksum=c0bcaf64c8ae75651e32a225270b09f5a10bba2e]*/ +{ 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 != Py_None) + GET_BUFFER_VIEW_OR_ERROUT(string, &buf); if ((new = newSHA512object()) == NULL) { - if (data_obj) + if (string != Py_None) PyBuffer_Release(&buf); return NULL; } @@ -689,11 +736,11 @@ if (PyErr_Occurred()) { Py_DECREF(new); - if (data_obj) + if (string != Py_None) PyBuffer_Release(&buf); return NULL; } - if (data_obj) { + if (string != Py_None) { sha512_update(new, buf.buf, buf.len); PyBuffer_Release(&buf); } @@ -701,27 +748,53 @@ return (PyObject *)new; } -PyDoc_STRVAR(SHA384_new__doc__, +/*[clinic input] +_sha512.SHA512.sha384 + + string: object = None + +Return a new SHA-384 hash object; optionally initialized with a string. +[clinic start generated code]*/ + +PyDoc_STRVAR(_sha512_SHA512_sha384__doc__, +"sha384(string=None)\n" "Return a new SHA-384 hash object; optionally initialized with a string."); +#define _SHA512_SHA512_SHA384_METHODDEF \ + {"sha384", (PyCFunction)_sha512_SHA512_sha384, METH_VARARGS|METH_KEYWORDS, _sha512_SHA512_sha384__doc__}, + static PyObject * -SHA384_new(PyObject *self, PyObject *args, PyObject *kwdict) +_sha512_SHA512_sha384_impl(PyObject *self, PyObject *string); + +static PyObject * +_sha512_SHA512_sha384(PyObject *self, PyObject *args, PyObject *kwargs) { - static char *kwlist[] = {"string", NULL}; + PyObject *return_value = NULL; + static char *_keywords[] = {"string", NULL}; + PyObject *string = Py_None; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "|O:sha384", _keywords, + &string)) + goto exit; + return_value = _sha512_SHA512_sha384_impl(self, string); + +exit: + return return_value; +} + +static PyObject * +_sha512_SHA512_sha384_impl(PyObject *self, PyObject *string) +/*[clinic end generated code: checksum=c4fe686dd0c50fde61c8f48ff9aae8011348a78f]*/ +{ 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 != Py_None) + GET_BUFFER_VIEW_OR_ERROUT(string, &buf); if ((new = newSHA384object()) == NULL) { - if (data_obj) + if (string != Py_None) PyBuffer_Release(&buf); return NULL; } @@ -730,11 +803,11 @@ if (PyErr_Occurred()) { Py_DECREF(new); - if (data_obj) + if (string != Py_None) PyBuffer_Release(&buf); return NULL; } - if (data_obj) { + if (string != Py_None) { sha512_update(new, buf.buf, buf.len); PyBuffer_Release(&buf); } @@ -746,8 +819,8 @@ /* 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_SHA512_METHODDEF + _SHA512_SHA512_SHA384_METHODDEF {NULL, NULL} /* Sentinel */ };