Skip to content

Commit 9e50289

Browse files
committed
sha1: simplify clinic directives
1 parent af3ed8d commit 9e50289

File tree

2 files changed

+37
-30
lines changed

2 files changed

+37
-30
lines changed

Modules/clinic/sha1module.c.h

Lines changed: 5 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/sha1module.c

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
#endif
2121

2222
#include "Python.h"
23+
#include "pycore_moduleobject.h" // _PyModule_GetState()
24+
#include "pycore_strhex.h" // _Py_strhex()
25+
#include "pycore_typeobject.h" // _PyType_GetModuleState()
26+
2327
#include "hashlib.h"
24-
#include "pycore_strhex.h" // _Py_strhex()
25-
#include "pycore_typeobject.h" // _PyType_GetModuleState()
2628

2729
#include "_hacl/Hacl_Hash_SHA1.h"
2830

@@ -42,16 +44,27 @@ typedef struct {
4244

4345
// --- Module state -----------------------------------------------------------
4446

47+
static struct PyModuleDef sha1module_def;
48+
4549
typedef struct {
46-
PyTypeObject* sha1_type;
47-
} SHA1State;
50+
PyTypeObject *sha1_type;
51+
} sha1module_state;
4852

49-
static inline SHA1State*
50-
sha1_get_state(PyObject *module)
53+
static inline sha1module_state *
54+
get_sha1module_state(PyObject *module)
5155
{
52-
void *state = PyModule_GetState(module);
56+
void *state = _PyModule_GetState(module);
5357
assert(state != NULL);
54-
return (SHA1State *)state;
58+
return (sha1module_state *)state;
59+
}
60+
61+
static inline sha1module_state *
62+
get_sha1module_state_by_cls(PyTypeObject *cls)
63+
{
64+
_Py_hashlib_check_exported_type(cls, &sha1module_def);
65+
void *state = _PyType_GetModuleState(cls);
66+
assert(state != NULL);
67+
return (sha1module_state *)state;
5568
}
5669

5770
// --- Module clinic configuration --------------------------------------------
@@ -67,7 +80,7 @@ class SHA1Type "SHA1object *" "&PyType_Type"
6780
// --- SHA-1 object interface configuration -----------------------------------
6881

6982
static SHA1object *
70-
newSHA1object(SHA1State *st)
83+
newSHA1object(sha1module_state *st)
7184
{
7285
SHA1object *sha = PyObject_GC_New(SHA1object, st->sha1_type);
7386
if (sha == NULL) {
@@ -108,16 +121,14 @@ SHA1_dealloc(PyObject *op)
108121
/*[clinic input]
109122
SHA1Type.copy
110123
111-
cls: defining_class
112-
113124
Return a copy of the hash object.
114125
[clinic start generated code]*/
115126

116127
static PyObject *
117-
SHA1Type_copy_impl(SHA1object *self, PyTypeObject *cls)
118-
/*[clinic end generated code: output=b32d4461ce8bc7a7 input=6c22e66fcc34c58e]*/
128+
SHA1Type_copy_impl(SHA1object *self)
129+
/*[clinic end generated code: output=b4e001264620f02a input=b7eae10df6f89b36]*/
119130
{
120-
SHA1State *st = _PyType_GetModuleState(cls);
131+
sha1module_state *st = get_sha1module_state_by_cls(Py_TYPE(self));
121132

122133
SHA1object *newobj;
123134
if ((newobj = newSHA1object(st)) == NULL) {
@@ -288,7 +299,7 @@ _sha1_sha1_impl(PyObject *module, PyObject *data, int usedforsecurity,
288299
GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
289300
}
290301

291-
SHA1State *st = sha1_get_state(module);
302+
sha1module_state *st = get_sha1module_state(module);
292303
if ((new = newSHA1object(st)) == NULL) {
293304
if (string) {
294305
PyBuffer_Release(&buf);
@@ -329,15 +340,15 @@ static struct PyMethodDef SHA1_functions[] = {
329340
static int
330341
_sha1_traverse(PyObject *module, visitproc visit, void *arg)
331342
{
332-
SHA1State *state = sha1_get_state(module);
343+
sha1module_state *state = get_sha1module_state(module);
333344
Py_VISIT(state->sha1_type);
334345
return 0;
335346
}
336347

337348
static int
338349
_sha1_clear(PyObject *module)
339350
{
340-
SHA1State *state = sha1_get_state(module);
351+
sha1module_state *state = get_sha1module_state(module);
341352
Py_CLEAR(state->sha1_type);
342353
return 0;
343354
}
@@ -351,7 +362,7 @@ _sha1_free(void *module)
351362
static int
352363
_sha1_exec(PyObject *module)
353364
{
354-
SHA1State* st = sha1_get_state(module);
365+
sha1module_state *st = get_sha1module_state(module);
355366

356367
st->sha1_type = (PyTypeObject *)PyType_FromModuleAndSpec(
357368
module, &sha1_type_spec, NULL);
@@ -381,10 +392,10 @@ static PyModuleDef_Slot _sha1_slots[] = {
381392
{0, NULL}
382393
};
383394

384-
static struct PyModuleDef _sha1module = {
395+
static struct PyModuleDef sha1module_def = {
385396
PyModuleDef_HEAD_INIT,
386397
.m_name = "_sha1",
387-
.m_size = sizeof(SHA1State),
398+
.m_size = sizeof(sha1module_state),
388399
.m_methods = SHA1_functions,
389400
.m_slots = _sha1_slots,
390401
.m_traverse = _sha1_traverse,
@@ -395,5 +406,5 @@ static struct PyModuleDef _sha1module = {
395406
PyMODINIT_FUNC
396407
PyInit__sha1(void)
397408
{
398-
return PyModuleDef_Init(&_sha1module);
409+
return PyModuleDef_Init(&sha1module_def);
399410
}

0 commit comments

Comments
 (0)