8000 bpo-1635741: Port hashlib modules to multiphase init (PEP 489) by koubaa · Pull Request #21818 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-1635741: Port hashlib modules to multiphase init (PEP 489) #21818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Sep 6, 2020
Prev Previous commit
Next Next commit
resolve comments
  • Loading branch information
koubaa committed Aug 31, 2020
commit 0384e6b967f32879cc38d0b377ff758e6165dfd6
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
Port :mod:`sha1` to multi-phase init
Port :mod:`sha512` to multi-phase init
Port :mod:`md5` to multi-phase init
Port :mod:`sha1`, :mod:`sha512`, and :mod:`md5` to multi-phase init (PEP 489)
10 changes: 4 additions & 6 deletions Modules/md5module.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ typedef struct {
PyTypeObject* md5_type;
} MD5State;

static inline MD5State* md5_get_state(PyObject *module) {
static inline MD5State* md5_get_state(PyObject *module)
{
void *state = PyModule_GetState(module);
assert(state != NULL);
return (MD5State *)state;
Expand All @@ -339,8 +340,9 @@ newMD5object(MD5State * st)
static void
MD5_dealloc(PyObject *ptr)
{
Py_DECREF(Py_TYPE(ptr));
PyObject *tp = Py_TYPE(ptr);
PyObject_Del(ptr);
Py_DECREF(tp);
}


Expand Down Expand Up @@ -572,10 +574,6 @@ static int md5_exec(PyObject *m)
return -1;
}

if (PyType_Ready(st->md5_type) < 0) {
return -1;
}

Py_INCREF((PyObject *)st->md5_type);
if (PyModule_AddObject(m, "MD5Type", (PyObject *)st->md5_type) < 0) {
Py_DECREF(st->md5_type);
Expand Down
15 changes: 6 additions & 9 deletions Modules/sha1module.c
8000
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ typedef struct {
PyTypeObject* sha1_type;
} SHA1State;

static inline SHA1State* sha1_get_state(PyObject *module) {
static inline SHA1State* sha1_get_state(PyObject *module)
{
void *state = PyModule_GetState(module);
assert(state != NULL);
return (SHA1State *)state;
Expand All @@ -317,8 +318,9 @@ newSHA1object(SHA1State *st)
static void
SHA1_dealloc(PyObject *ptr)
{
Py_DECREF(Py_TYPE(ptr));
PyObject *tp = Py_TYPE(ptr);
PyObject_Del(ptr);
Py_DECREF(tp);
}


Expand Down Expand Up @@ -538,7 +540,8 @@ _sha1_free(void *module)
_sha1_clear((PyObject *)module);
}

static int sha1_exec(PyObject *module) {
static int sha1_exec(PyObject *module)
{
SHA1State* st = sha1_get_state(module);

st->sha1_type = (PyTypeObject *)PyType_FromModuleAndSpec(
Expand All @@ -548,12 +551,6 @@ static int sha1_exec(PyObject *module) {
return -1;
}

//cannot use PyModule_AddType becuase "SHA1Type"
//isn't the same as _PyType_Name(st->sha1_type)
if (PyType_Ready(st->sha1_type) < 0) {
return -1;
}

Py_INCREF(st->sha1_type);
if (PyModule_AddObject(module,
"SHA1Type",
Expand Down
16 changes: 4 additions & 12 deletions Modules/sha512module.c
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,8 @@ typedef struct {
PyTypeObject* sha512_type;
} SHA512State;

static inline SHA512State* sha512_get_state(PyObject *module) {
static inline SHA512State* sha512_get_state(PyObject *module)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static inline SHA512State* sha512_get_state(PyObject *module)
static inline SHA512State*
sha512_get_state(PyObject *module)

{
void *state = PyModule_GetState(module);
assert(state != NULL);
return (SHA512State *)state;
Expand All @@ -446,8 +447,9 @@ static SHAobject *newSHA512object(SHA512State *st) {
static void
SHA512_dealloc(PyObject *ptr)
{
Py_DECREF(Py_TYPE(ptr));
PyObject *tp = Py_TYPE(ptr);
PyObject_Del(ptr);
Py_DECREF(tp);
}


Expand Down Expand Up @@ -754,16 +756,6 @@ static int sha512_exec(PyObject *m)
return -1;
}

//cannot use PyModule_AddType becuase "SHA1Type"
//isn't the same as _PyType_Name(st->sha1_type)
if (PyType_Ready(st->sha384_type) < 0) {
return -1;
}

if (PyType_Ready(st->sha512_type) < 0) {
return -1;
}

Py_INCREF(st->sha384_type);
if (PyModule_AddObject(m, "SHA384Type", (PyObject *)st->sha384_type) < 0) {
Py_DECREF(st->sha384_type);
Expand Down
0