diff -r 8d572bf526e6 Modules/md5module.c --- a/Modules/md5module.c Sun Jan 26 21:35:22 2014 -0500 +++ b/Modules/md5module.c Mon Jan 27 16:45:50 2014 +0800 @@ -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: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ /* Some useful types */ @@ -332,10 +337,31 @@ /* 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" +"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: checksum=8a5407c545305c1705c4e04a41e15bfc64f062ce]*/ { MD5object *newobj; @@ -351,11 +377,31 @@ 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" "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: checksum=dde3c41c92886f1e370675a05fceda5e2b253d77]*/ { unsigned char digest[MD5_DIGESTSIZE]; struct md5_state temp; @@ -365,11 +411,31 @@ 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" "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: checksum=69f2e660005d7a5b6d2a4ed844c57171251f2e00]*/ { unsigned char digest[MD5_DIGESTSIZE]; struct md5_state temp; @@ -401,18 +467,28 @@ 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" +"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: checksum=0e7ea5fb51ed09cfc11b8fee78c396a091971da8]*/ { - 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 +499,10 @@ } 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 +578,53 @@ /* 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" "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) { - static char *kwlist[] = {"string", NULL}; + 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: checksum=4cf7838155317776d44ba6dd50d7f6b02ac87bd7]*/ +{ 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 +633,11 @@ 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 +649,7 @@ /* 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 */ };