Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 501b13c

Browse files
committed
Issue #20173: Convert sha1, sha256, sha512 and md5 to ArgumentClinic.
Patch by Vajrasky Kok.
1 parent 6aef4dc commit 501b13c

5 files changed

Lines changed: 699 additions & 158 deletions

File tree

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ Core and Builtins
110110
Library
111111
-------
112112

113+
- Issue #20173: Convert sha1, sha256, sha512 and md5 to ArgumentClinic.
114+
Patch by Vajrasky Kok.
115+
113116
- Fix repr(_socket.socket) on Windows 64-bit: don't fail with OverflowError
114117
on closed socket. repr(socket.socket) already works fine.
115118

Modules/md5module.c

Lines changed: 144 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
#include "Python.h"
2020
#include "hashlib.h"
2121

22+
/*[clinic input]
23+
module _md5
24+
class MD5Type "MD5object *" "&PyType_Type"
25+
[clinic start generated code]*/
26+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=6e5261719957a912]*/
2227

2328
/* Some useful types */
2429

@@ -332,10 +337,33 @@ MD5_dealloc(PyObject *ptr)
332337

333338
/* External methods for a hash object */
334339

335-
PyDoc_STRVAR(MD5_copy__doc__, "Return a copy of the hash object.");
340+
/*[clinic input]
341+
MD5Type.copy
342+
343+
Return a copy of the hash object.
344+
[clinic start generated code]*/
345+
346+
PyDoc_STRVAR(MD5Type_copy__doc__,
347+
"copy($self, /)\n"
348+
"--\n"
349+
"\n"
350+
"Return a copy of the hash object.");
351+
352+
#define MD5TYPE_COPY_METHODDEF \
353+
{"copy", (PyCFunction)MD5Type_copy, METH_NOARGS, MD5Type_copy__doc__},
354+
355+
static PyObject *
356+
MD5Type_copy_impl(MD5object *self);
357+
358+
static PyObject *
359+
MD5Type_copy(MD5object *self, PyObject *Py_UNUSED(ignored))
360+
{
361+
return MD5Type_copy_impl(self);
362+
}
336363

337364
static PyObject *
338-
MD5_copy(MD5object *self, PyObject *unused)
365+
MD5Type_copy_impl(MD5object *self)
366+
/*[clinic end generated code: output=3b3a88920b3dc7f4 input=2c09e6d2493f3079]*/
339367
{
340368
MD5object *newobj;
341369

@@ -351,11 +379,33 @@ MD5_copy(MD5object *self, PyObject *unused)
351379
return (PyObject *)newobj;
352380
}
353381

354-
PyDoc_STRVAR(MD5_digest__doc__,
382+
/*[clinic input]
383+
MD5Type.digest
384+
385+
Return the digest value as a string of binary data.
386+
[clinic start generated code]*/
387+
388+
PyDoc_STRVAR(MD5Type_digest__doc__,
389+
"digest($self, /)\n"
390+
"--\n"
391+
"\n"
355392
"Return the digest value as a string of binary data.");
356393

394+
#define MD5TYPE_DIGEST_METHODDEF \
395+
{"digest", (PyCFunction)MD5Type_digest, METH_NOARGS, MD5Type_digest__doc__},
396+
357397
static PyObject *
358-
MD5_digest(MD5object *self, PyObject *unused)
398+
MD5Type_digest_impl(MD5object *self);
399+
400+
static PyObject *
401+
MD5Type_digest(MD5object *self, PyObject *Py_UNUSED(ignored))
402+
{
403+
return MD5Type_digest_impl(self);
404+
}
405+
406+
static PyObject *
407+
MD5Type_digest_impl(MD5object *self)
408+
/*[clinic end generated code: output=7a796b28fa89485f input=7b96e65389412a34]*/
359409
{
360410
unsigned char digest[MD5_DIGESTSIZE];
361411
struct md5_state temp;
@@ -365,11 +415,33 @@ MD5_digest(MD5object *self, PyObject *unused)
365415
return PyBytes_FromStringAndSize((const char *)digest, MD5_DIGESTSIZE);
366416
}
367417

368-
PyDoc_STRVAR(MD5_hexdigest__doc__,
418+
/*[clinic input]
419+
MD5Type.hexdigest
420+
421+
Return the digest value as a string of hexadecimal digits.
422+
[clinic start generated code]*/
423+
424+
PyDoc_STRVAR(MD5Type_hexdigest__doc__,
425+
"hexdigest($self, /)\n"
426+
"--\n"
427+
"\n"
369428
"Return the digest value as a string of hexadecimal digits.");
370429

430+
#define MD5TYPE_HEXDIGEST_METHODDEF \
431+
{"hexdigest", (PyCFunction)MD5Type_hexdigest, METH_NOARGS, MD5Type_hexdigest__doc__},
432+
433+
static PyObject *
434+
MD5Type_hexdigest_impl(MD5object *self);
435+
371436
static PyObject *
372-
MD5_hexdigest(MD5object *self, PyObject *unused)
437+
MD5Type_hexdigest(MD5object *self, PyObject *Py_UNUSED(ignored))
438+
{
439+
return MD5Type_hexdigest_impl(self);
440+
}
441+
442+
static PyObject *
443+
MD5Type_hexdigest_impl(MD5object *self)
444+
/*[clinic end generated code: output=daa73609f94f92e1 input=b60b19de644798dd]*/
373445
{
374446
unsigned char digest[MD5_DIGESTSIZE];
375447
struct md5_state temp;
@@ -401,18 +473,30 @@ MD5_hexdigest(MD5object *self, PyObject *unused)
401473
return retval;
402474
}
403475

404-
PyDoc_STRVAR(MD5_update__doc__,
405-
"Update this hash object's state with the provided string.");
476+
/*[clinic input]
477+
MD5Type.update
478+
479+
obj: object
480+
/
481+
482+
Update this hash object's state with the provided string.
483+
[clinic start generated code]*/
484+
485+
PyDoc_STRVAR(MD5Type_update__doc__,
486+
"update($self, obj, /)\n"
487+
"--\n"
488+
"\n"
489+
"Update this hash object\'s state with the provided string.");
490+
491+
#define MD5TYPE_UPDATE_METHODDEF \
492+
{"update", (PyCFunction)MD5Type_update, METH_O, MD5Type_update__doc__},
406493

407494
static PyObject *
408-
MD5_update(MD5object *self, PyObject *args)
495+
MD5Type_update(MD5object *self, PyObject *obj)
496+
/*[clinic end generated code: output=9d09b6c6cdc6cac3 input=6e1efcd9ecf17032]*/
409497
{
410-
PyObject *obj;
411498
Py_buffer buf;
412499

413-
if (!PyArg_ParseTuple(args, "O:update", &obj))
414-
return NULL;
415-
416500
GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
417501

418502
md5_process(&self->hash_state, buf.buf, buf.len);
@@ -423,10 +507,10 @@ MD5_update(MD5object *self, PyObject *args)
423507
}
424508

425509
static PyMethodDef MD5_methods[] = {
426-
{"copy", (PyCFunction)MD5_copy, METH_NOARGS, MD5_copy__doc__},
427-
{"digest", (PyCFunction)MD5_digest, METH_NOARGS, MD5_digest__doc__},
428-
{"hexdigest", (PyCFunction)MD5_hexdigest, METH_NOARGS, MD5_hexdigest__doc__},
429-
{"update", (PyCFunction)MD5_update, METH_VARARGS, MD5_update__doc__},
510+
MD5TYPE_COPY_METHODDEF
511+
MD5TYPE_DIGEST_METHODDEF
512+
MD5TYPE_HEXDIGEST_METHODDEF
513+
MD5TYPE_UPDATE_METHODDEF
430514
{NULL, NULL} /* sentinel */
431515
};
432516

@@ -502,27 +586,55 @@ static PyTypeObject MD5type = {
502586

503587
/* The single module-level function: new() */
504588

505-
PyDoc_STRVAR(MD5_new__doc__,
589+
/*[clinic input]
590+
_md5.md5
591+
592+
string: object(c_default="NULL") = b''
593+
594+
Return a new MD5 hash object; optionally initialized with a string.
595+
[clinic start generated code]*/
596+
597+
PyDoc_STRVAR(_md5_md5__doc__,
598+
"md5($module, /, string=b\'\')\n"
599+
"--\n"
600+
"\n"
506601
"Return a new MD5 hash object; optionally initialized with a string.");
507602

603+
#define _MD5_MD5_METHODDEF \
604+
{"md5", (PyCFunction)_md5_md5, METH_VARARGS|METH_KEYWORDS, _md5_md5__doc__},
605+
606+
static PyObject *
607+
_md5_md5_impl(PyModuleDef *module, PyObject *string);
608+
508609
static PyObject *
509-
MD5_new(PyObject *self, PyObject *args, PyObject *kwdict)
610+
_md5_md5(PyModuleDef *module, PyObject *args, PyObject *kwargs)
611+
{
612+
PyObject *return_value = NULL;
613+
static char *_keywords[] = {"string", NULL};
614+
PyObject *string = NULL;
615+
616+
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
617+
"|O:md5", _keywords,
618+
&string))
619+
goto exit;
620+
return_value = _md5_md5_impl(module, string);
621+
622+
exit:
623+
return return_value;
624+
}
625+
626+
static PyObject *
627+
_md5_md5_impl(PyModuleDef *module, PyObject *string)
628+
/*[clinic end generated code: output=1039e912d919880e input=d12ef8f72d684f7b]*/
510629
{
511-
static char *kwlist[] = {"string", NULL};
512630
MD5object *new;
513-
PyObject *data_obj = NULL;
514631
Py_buffer buf;
515632

516-
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
517-
&data_obj)) {
518-
return NULL;
519-
}
520-
521-
if (data_obj)
522-
GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf);
633+
if (string)
634+
GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
523635

524636
if ((new = newMD5object()) == NULL) {
525-
if (data_obj)
637+
if (string)
526638
PyBuffer_Release(&buf);
527639
return NULL;
528640
}
@@ -531,11 +643,11 @@ MD5_new(PyObject *self, PyObject *args, PyObject *kwdict)
531643

532644
if (PyErr_Occurred()) {
533645
Py_DECREF(new);
534-
if (data_obj)
646+
if (string)
535647
PyBuffer_Release(&buf);
536648
return NULL;
537649
}
538-
if (data_obj) {
650+
if (string) {
539651
md5_process(&new->hash_state, buf.buf, buf.len);
540652
PyBuffer_Release(&buf);
541653
}
@@ -547,7 +659,7 @@ MD5_new(PyObject *self, PyObject *args, PyObject *kwdict)
547659
/* List of functions exported by this module */
548660

549661
static struct PyMethodDef MD5_functions[] = {
550-
{"md5", (PyCFunction)MD5_new, METH_VARARGS|METH_KEYWORDS, MD5_new__doc__},
662+
_MD5_MD5_METHODDEF
551663
{NULL, NULL} /* Sentinel */
552664
};
553665

0 commit comments

Comments
 (0)