Skip to content

Commit 0384e6b

Browse files
committed
resolve comments
1 parent 37e2282 commit 0384e6b

File tree

4 files changed

+15
-30
lines changed

4 files changed

+15
-30
lines changed
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
Port :mod:`sha1` to multi-phase init
2-
Port :mod:`sha512` to multi-phase init
3-
Port :mod:`md5` to multi-phase init
1+
Port :mod:`sha1`, :mod:`sha512`, and :mod:`md5` to multi-phase init (PEP 489)

Modules/md5module.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ typedef struct {
322322
PyTypeObject* md5_type;
323323
} MD5State;
324324

325-
static inline MD5State* md5_get_state(PyObject *module) {
325+
static inline MD5State* md5_get_state(PyObject *module)
326+
{
326327
void *state = PyModule_GetState(module);
327328
assert(state != NULL);
328329
return (MD5State *)state;
@@ -339,8 +340,9 @@ newMD5object(MD5State * st)
339340
static void
340341
MD5_dealloc(PyObject *ptr)
341342
{
342-
Py_DECREF(Py_TYPE(ptr));
343+
PyObject *tp = Py_TYPE(ptr);
343344
PyObject_Del(ptr);
345+
Py_DECREF(tp);
344346
}
345347

346348

@@ -572,10 +574,6 @@ static int md5_exec(PyObject *m)
572574
return -1;
573575
}
574576

575-
if (PyType_Ready(st->md5_type) < 0) {
576-
return -1;
577-
}
578-
579577
Py_INCREF((PyObject *)st->md5_type);
580578
if (PyModule_AddObject(m, "MD5Type", (PyObject *)st->md5_type) < 0) {
581579
Py_DECREF(st->md5_type);

Modules/sha1module.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,8 @@ typedef struct {
299299
PyTypeObject* sha1_type;
300300
} SHA1State;
301301

302-
static inline SHA1State* sha1_get_state(PyObject *module) {
302+
static inline SHA1State* sha1_get_state(PyObject *module)
303+
{
303304
void *state = PyModule_GetState(module);
304305
assert(state != NULL);
305306
return (SHA1State *)state;
@@ -317,8 +318,9 @@ newSHA1object(SHA1State *st)
317318
static void
318319
SHA1_dealloc(PyObject *ptr)
319320
{
320-
Py_DECREF(Py_TYPE(ptr));
321+
PyObject *tp = Py_TYPE(ptr);
321322
PyObject_Del(ptr);
323+
Py_DECREF(tp);
322324
}
323325

324326

@@ -538,7 +540,8 @@ _sha1_free(void *module)
538540
_sha1_clear((PyObject *)module);
539541
}
540542

541-
static int sha1_exec(PyObject *module) {
543+
static int sha1_exec(PyObject *module)
544+
{
542545
SHA1State* st = sha1_get_state(module);
543546

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

551-
//cannot use PyModule_AddType becuase "SHA1Type"
552-
//isn't the same as _PyType_Name(st->sha1_type)
553-
if (PyType_Ready(st->sha1_type) < 0) {
554-
return -1;
555-
}
556-
557554
Py_INCREF(st->sha1_type);
558555
if (PyModule_AddObject(module,
559556
"SHA1Type",

Modules/sha512module.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,8 @@ typedef struct {
427427
PyTypeObject* sha512_type;
428428
} SHA512State;
429429

430-
static inline SHA512State* sha512_get_state(PyObject *module) {
430+
static inline SHA512State* sha512_get_state(PyObject *module)
431+
{
431432
void *state = PyModule_GetState(module);
432433
assert(state != NULL);
433434
return (SHA512State *)state;
@@ -446,8 +447,9 @@ static SHAobject *newSHA512object(SHA512State *st) {
446447
static void
447448
SHA512_dealloc(PyObject *ptr)
448449
{
449-
Py_DECREF(Py_TYPE(ptr));
450+
PyObject *tp = Py_TYPE(ptr);
450451
PyObject_Del(ptr);
452+
Py_DECREF(tp);
451453
}
452454

453455

@@ -754,16 +756,6 @@ static int sha512_exec(PyObject *m)
754756
return -1;
755757
}
756758

757-
//cannot use PyModule_AddType becuase "SHA1Type"
758-
//isn't the same as _PyType_Name(st->sha1_type)
759-
if (PyType_Ready(st->sha384_type) < 0) {
760-
return -1;
761-
}
762-
763-
if (PyType_Ready(st->sha512_type) < 0) {
764-
return -1;
765-
}
766-
767759
Py_INCREF(st->sha384_type);
768760
if (PyModule_AddObject(m, "SHA384Type", (PyObject *)st->sha384_type) < 0) {
769761
Py_DECREF(st->sha384_type);

0 commit comments

Comments
 (0)