From dce777f6ca1af2f6f5255d3323ce8f06bc7f4d4f Mon Sep 17 00:00:00 2001 From: Mohamed Koubaa Date: Mon, 10 Aug 2020 15:25:37 -0500 Subject: [PATCH 01/11] port blake2 --- Modules/_blake2/blake2b_impl.c | 54 ++++++---------------- Modules/_blake2/blake2module.c | 83 ++++++++++++++++++++-------------- Modules/_blake2/blake2s_impl.c | 55 ++++++---------------- 3 files changed, 76 insertions(+), 116 deletions(-) diff --git a/Modules/_blake2/blake2b_impl.c b/Modules/_blake2/blake2b_impl.c index 7fb1296f8b2b90..4041a698c0d20a 100644 --- a/Modules/_blake2/blake2b_impl.c +++ b/Modules/_blake2/blake2b_impl.c @@ -34,7 +34,7 @@ #endif -extern PyTypeObject PyBlake2_BLAKE2bType; +extern PyType_Spec blake2b_type_spec; typedef struct { PyObject_HEAD @@ -394,44 +394,18 @@ py_blake2b_dealloc(PyObject *self) PyObject_Del(self); } +static PyType_Slot blake2b_type_slots[] = { + {Py_tp_dealloc, py_blake2b_dealloc}, + {Py_tp_doc, (char *)py_blake2b_new__doc__}, + {Py_tp_methods, py_blake2b_methods}, + {Py_tp_getset, py_blake2b_getsetters}, + {Py_tp_new, py_blake2b_new}, + {0,0} +}; -PyTypeObject PyBlake2_BLAKE2bType = { - PyVarObject_HEAD_INIT(NULL, 0) - "_blake2.blake2b", /* tp_name */ - sizeof(BLAKE2bObject), /* tp_basicsize */ - 0, /* tp_itemsize */ - py_blake2b_dealloc, /* tp_dealloc */ - 0, /*tp_vectorcall_offset*/ - 0, /* tp_getattr */ - 0, /* tp_setattr */ - 0, /* tp_as_async */ - 0, /* tp_repr */ - 0, /* tp_as_number */ - 0, /* tp_as_sequence */ - 0, /* tp_as_mapping */ - 0, /* tp_hash */ - 0, /* tp_call */ - 0, /* tp_str */ - 0, /* tp_getattro */ - 0, /* tp_setattro */ - 0, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT, /* tp_flags */ - py_blake2b_new__doc__, /* tp_doc */ - 0, /* tp_traverse */ - 0, /* tp_clear */ - 0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - 0, /* tp_iter */ - 0, /* tp_iternext */ - py_blake2b_methods, /* tp_methods */ - 0, /* tp_members */ - py_blake2b_getsetters, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ - 0, /* tp_init */ - 0, /* tp_alloc */ - py_blake2b_new, /* tp_new */ +PyType_Spec blake2b_type_spec = { + .name = "_blake2.blake2b", + .basicsize = sizeof(BLAKE2bObject), + .flags = Py_TPFLAGS_DEFAULT, + .slots = blake2b_type_slots }; diff --git a/Modules/_blake2/blake2module.c b/Modules/_blake2/blake2module.c index ff142c9f3ed330..223c29a47e7e3c 100644 --- a/Modules/_blake2/blake2module.c +++ b/Modules/_blake2/blake2module.c @@ -12,62 +12,53 @@ #include "impl/blake2.h" -extern PyTypeObject PyBlake2_BLAKE2bType; -extern PyTypeObject PyBlake2_BLAKE2sType; - +extern PyType_Spec blake2b_type_spec; +extern PyType_Spec blake2s_type_spec; PyDoc_STRVAR(blake2mod__doc__, "_blake2b provides BLAKE2b for hashlib\n" ); +typedef struct { + PyTypeObject* blake2b_type; + PyTypeObject* blake2s_type; +} Blake2State; + +static inline Blake2State* blake2_get_state(PyObject *module) { + void *state = PyModule_GetState(module); + assert(state != NULL); + return (Blake2State *)state; +} static struct PyMethodDef blake2mod_functions[] = { {NULL, NULL} }; -static struct PyModuleDef blake2_module = { - PyModuleDef_HEAD_INIT, - "_blake2", - blake2mod__doc__, - -1, - blake2mod_functions, - NULL, - NULL, - NULL, - NULL -}; - #define ADD_INT(d, name, value) do { \ PyObject *x = PyLong_FromLong(value); \ if (!x) { \ - Py_DECREF(m); \ - return NULL; \ + return -1; \ } \ if (PyDict_SetItemString(d, name, x) < 0) { \ - Py_DECREF(m); \ - return NULL; \ + return -1; \ } \ Py_DECREF(x); \ } while(0) -PyMODINIT_FUNC -PyInit__blake2(void) +static int blake2_exec(PyObject *m) { - PyObject *m; - PyObject *d; + Blake2State* st = blake2_get_state(m); - m = PyModule_Create(&blake2_module); - if (m == NULL) - return NULL; + st->blake2b_type = (PyTypeObject *)PyType_FromModuleAndSpec( + m, &blake2b_type_spec, NULL); /* BLAKE2b */ - Py_SET_TYPE(&PyBlake2_BLAKE2bType, &PyType_Type); - if (PyModule_AddType(m, &PyBlake2_BLAKE2bType) < 0) { - return NULL; + if (PyModule_AddType(m, st->blake2b_type) < 0) { + return -1; } - d = PyBlake2_BLAKE2bType.tp_dict; + PyObject *d = st->blake2b_type->tp_dict; ADD_INT(d, "SALT_SIZE", BLAKE2B_SALTBYTES); ADD_INT(d, "PERSON_SIZE", BLAKE2B_PERSONALBYTES); ADD_INT(d, "MAX_KEY_SIZE", BLAKE2B_KEYBYTES); @@ -79,12 +70,14 @@ PyInit__blake2(void) PyModule_AddIntConstant(m, "BLAKE2B_MAX_DIGEST_SIZE", BLAKE2B_OUTBYTES); /* BLAKE2s */ - Py_SET_TYPE(&PyBlake2_BLAKE2sType, &PyType_Type); - if (PyModule_AddType(m, &PyBlake2_BLAKE2sType) < 0) { - return NULL; + st->blake2s_type = (PyTypeObject *)PyType_FromModuleAndSpec( + m, &blake2s_type_spec, NULL); + + if (PyModule_AddType(m, st->blake2s_type) < 0) { + return -1; } - d = PyBlake2_BLAKE2sType.tp_dict; + d = st->blake2s_type->tp_dict; ADD_INT(d, "SALT_SIZE", BLAKE2S_SALTBYTES); ADD_INT(d, "PERSON_SIZE", BLAKE2S_PERSONALBYTES); ADD_INT(d, "MAX_KEY_SIZE", BLAKE2S_KEYBYTES); @@ -95,5 +88,25 @@ PyInit__blake2(void) PyModule_AddIntConstant(m, "BLAKE2S_MAX_KEY_SIZE", BLAKE2S_KEYBYTES); PyModule_AddIntConstant(m, "BLAKE2S_MAX_DIGEST_SIZE", BLAKE2S_OUTBYTES); - return m; + return 0; } + +static PyModuleDef_Slot _blake2_slots[] = { + {Py_mod_exec, blake2_exec}, + {0, NULL} +}; + +static struct PyModuleDef blake2_module = { + PyModuleDef_HEAD_INIT, + "_blake2", + .m_doc = blake2mod__doc__, + .m_size = sizeof(Blake2State), + .m_methods = blake2mod_functions, + .m_slots = _blake2_slots +}; + +PyMODINIT_FUNC +PyInit__blake2(void) +{ + return PyModuleDef_Init(&blake2_module); +} \ No newline at end of file diff --git a/Modules/_blake2/blake2s_impl.c b/Modules/_blake2/blake2s_impl.c index e3e90d0587b805..9482cf7048f0d4 100644 --- a/Modules/_blake2/blake2s_impl.c +++ b/Modules/_blake2/blake2s_impl.c @@ -33,8 +33,7 @@ #include "impl/blake2s-ref.c" #endif - -extern PyTypeObject PyBlake2_BLAKE2sType; +extern PyType_Spec blake2s_type_spec; typedef struct { PyObject_HEAD @@ -394,44 +393,18 @@ py_blake2s_dealloc(PyObject *self) PyObject_Del(self); } +static PyType_Slot blake2s_type_slots[] = { + {Py_tp_dealloc, py_blake2s_dealloc}, + {Py_tp_doc, (char *)py_blake2s_new__doc__}, + {Py_tp_methods, py_blake2s_methods}, + {Py_tp_getset, py_blake2s_getsetters}, + {Py_tp_new, py_blake2s_new}, + {0,0} +}; -PyTypeObject PyBlake2_BLAKE2sType = { - PyVarObject_HEAD_INIT(NULL, 0) - "_blake2.blake2s", /* tp_name */ - sizeof(BLAKE2sObject), /* tp_basicsize */ - 0, /* tp_itemsize */ - py_blake2s_dealloc, /* tp_dealloc */ - 0, /*tp_vectorcall_offset*/ - 0, /* tp_getattr */ - 0, /* tp_setattr */ - 0, /* tp_as_async */ - 0, /* tp_repr */ - 0, /* tp_as_number */ - 0, /* tp_as_sequence */ - 0, /* tp_as_mapping */ - 0, /* tp_hash */ - 0, /* tp_call */ - 0, /* tp_str */ - 0, /* tp_getattro */ - 0, /* tp_setattro */ - 0, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT, /* tp_flags */ - py_blake2s_new__doc__, /* tp_doc */ - 0, /* tp_traverse */ - 0, /* tp_clear */ - 0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - 0, /* tp_iter */ - 0, /* tp_iternext */ - py_blake2s_methods, /* tp_methods */ - 0, /* tp_members */ - py_blake2s_getsetters, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ - 0, /* tp_init */ - 0, /* tp_alloc */ - py_blake2s_new, /* tp_new */ +PyType_Spec blake2s_type_spec = { + .name = "_blake2.blake2s", + .basicsize = sizeof(BLAKE2sObject), + .flags = Py_TPFLAGS_DEFAULT, + .slots = blake2s_type_slots }; From 88d58ff6d3141c620033656fc2621e2a22036c55 Mon Sep 17 00:00:00 2001 From: Mohamed Koubaa Date: Thu, 13 Aug 2020 07:18:12 -0500 Subject: [PATCH 02/11] blurb --- .../Core and Builtins/2020-08-13-07-18-05.bpo-1635741.FC13e7.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2020-08-13-07-18-05.bpo-1635741.FC13e7.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-08-13-07-18-05.bpo-1635741.FC13e7.rst b/Misc/NEWS.d/next/Core and Builtins/2020-08-13-07-18-05.bpo-1635741.FC13e7.rst new file mode 100644 index 00000000000000..83ba710b584148 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2020-08-13-07-18-05.bpo-1635741.FC13e7.rst @@ -0,0 +1 @@ +Port :mod:`_blake2` to multi-phase init From 00a468c7fe14b65230236995ec190c7c62e60a60 Mon Sep 17 00:00:00 2001 From: Mohamed Koubaa Date: Wed, 12 Aug 2020 19:23:58 -0500 Subject: [PATCH 03/11] decref the type --- Modules/_blake2/blake2b_impl.c | 3 +++ Modules/_blake2/blake2s_impl.c | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/Modules/_blake2/blake2b_impl.c b/Modules/_blake2/blake2b_impl.c index 4041a698c0d20a..4d2d873a3bbab5 100644 --- a/Modules/_blake2/blake2b_impl.c +++ b/Modules/_blake2/blake2b_impl.c @@ -391,6 +391,9 @@ py_blake2b_dealloc(PyObject *self) PyThread_free_lock(obj->lock); obj->lock = NULL; } + PyTypeObject *tp = Py_TYPE(self); + Py_DECREF(tp); + PyObject_Del(self); } diff --git a/Modules/_blake2/blake2s_impl.c b/Modules/_blake2/blake2s_impl.c index 9482cf7048f0d4..99db2d59b345ca 100644 --- a/Modules/_blake2/blake2s_impl.c +++ b/Modules/_blake2/blake2s_impl.c @@ -390,6 +390,10 @@ py_blake2s_dealloc(PyObject *self) PyThread_free_lock(obj->lock); obj->lock = NULL; } + + PyTypeObject *tp = Py_TYPE(self); + Py_DECREF(tp); + PyObject_Del(self); } From 0be5d6ca64badf3832c63702caf42946d1ba56da Mon Sep 17 00:00:00 2001 From: Mohamed Koubaa Date: Thu, 13 Aug 2020 07:27:30 -0500 Subject: [PATCH 04/11] respond to review --- Modules/_blake2/blake2b_impl.c | 5 +++-- Modules/_blake2/blake2module.c | 7 +++++++ Modules/_blake2/blake2s_impl.c | 4 ++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Modules/_blake2/blake2b_impl.c b/Modules/_blake2/blake2b_impl.c index 4d2d873a3bbab5..8c28249d561c08 100644 --- a/Modules/_blake2/blake2b_impl.c +++ b/Modules/_blake2/blake2b_impl.c @@ -391,10 +391,11 @@ py_blake2b_dealloc(PyObject *self) PyThread_free_lock(obj->lock); obj->lock = NULL; } - PyTypeObject *tp = Py_TYPE(self); - Py_DECREF(tp); PyObject_Del(self); + + PyTypeObject *tp = Py_TYPE(self); + Py_DECREF(tp); } static PyType_Slot blake2b_type_slots[] = { diff --git a/Modules/_blake2/blake2module.c b/Modules/_blake2/blake2module.c index 223c29a47e7e3c..9b525fffed2d47 100644 --- a/Modules/_blake2/blake2module.c +++ b/Modules/_blake2/blake2module.c @@ -37,9 +37,11 @@ static struct PyMethodDef blake2mod_functions[] = { #define ADD_INT(d, name, value) do { \ PyObject *x = PyLong_FromLong(value); \ if (!x) { \ + Py_DECREF(x); \ return -1; \ } \ if (PyDict_SetItemString(d, name, x) < 0) { \ + Py_DECREF(x); \ return -1; \ } \ Py_DECREF(x); \ @@ -53,6 +55,8 @@ static int blake2_exec(PyObject *m) st->blake2b_type = (PyTypeObject *)PyType_FromModuleAndSpec( m, &blake2b_type_spec, NULL); + if (NULL == st->blake2b_type) + return -1; /* BLAKE2b */ if (PyModule_AddType(m, st->blake2b_type) < 0) { return -1; @@ -73,6 +77,9 @@ static int blake2_exec(PyObject *m) st->blake2s_type = (PyTypeObject *)PyType_FromModuleAndSpec( m, &blake2s_type_spec, NULL); + if (NULL == st->blake2s_type) + return -1; + if (PyModule_AddType(m, st->blake2s_type) < 0) { return -1; } diff --git a/Modules/_blake2/blake2s_impl.c b/Modules/_blake2/blake2s_impl.c index 99db2d59b345ca..e4146997da33d4 100644 --- a/Modules/_blake2/blake2s_impl.c +++ b/Modules/_blake2/blake2s_impl.c @@ -391,10 +391,10 @@ py_blake2s_dealloc(PyObject *self) obj->lock = NULL; } + PyObject_Del(self); + PyTypeObject *tp = Py_TYPE(self); Py_DECREF(tp); - - PyObject_Del(self); } static PyType_Slot blake2s_type_slots[] = { From abb51fe0f57f9466c080777674f3d607963b2675 Mon Sep 17 00:00:00 2001 From: Mohamed Koubaa Date: Fri, 21 Aug 2020 18:48:44 -0500 Subject: [PATCH 05/11] add comment --- Modules/_blake2/blake2b_impl.c | 2 ++ Modules/_blake2/blake2s_impl.c | 2 ++ 2 files changed, 4 insertions(+) diff --git a/Modules/_blake2/blake2b_impl.c b/Modules/_blake2/blake2b_impl.c index 8c28249d561c08..7f8ea44bc5fd63 100644 --- a/Modules/_blake2/blake2b_impl.c +++ b/Modules/_blake2/blake2b_impl.c @@ -407,6 +407,8 @@ static PyType_Slot blake2b_type_slots[] = { {0,0} }; +// Using PyType_GetModuleState() on this type is safe since +// it cannot be subclassed: it does not have the Py_TPFLAGS_BASETYPE flag. PyType_Spec blake2b_type_spec = { .name = "_blake2.blake2b", .basicsize = sizeof(BLAKE2bObject), diff --git a/Modules/_blake2/blake2s_impl.c b/Modules/_blake2/blake2s_impl.c index e4146997da33d4..ffaf4315133061 100644 --- a/Modules/_blake2/blake2s_impl.c +++ b/Modules/_blake2/blake2s_impl.c @@ -406,6 +406,8 @@ static PyType_Slot blake2s_type_slots[] = { {0,0} }; +// Using PyType_GetModuleState() on this type is safe since +// it cannot be subclassed: it does not have the Py_TPFLAGS_BASETYPE flag. PyType_Spec blake2s_type_spec = { .name = "_blake2.blake2s", .basicsize = sizeof(BLAKE2sObject), From 6f6a25424ab21b27aa324646e8e9a60083ba2102 Mon Sep 17 00:00:00 2001 From: = <=> Date: Thu, 27 Aug 2020 19:11:02 -0500 Subject: [PATCH 06/11] fix use-after-free --- Modules/_blake2/blake2b_impl.c | 4 +--- Modules/_blake2/blake2s_impl.c | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/Modules/_blake2/blake2b_impl.c b/Modules/_blake2/blake2b_impl.c index 7f8ea44bc5fd63..449e68208cd7a6 100644 --- a/Modules/_blake2/blake2b_impl.c +++ b/Modules/_blake2/blake2b_impl.c @@ -392,10 +392,8 @@ py_blake2b_dealloc(PyObject *self) obj->lock = NULL; } + Py_DECREF(Py_TYPE(self)); PyObject_Del(self); - - PyTypeObject *tp = Py_TYPE(self); - Py_DECREF(tp); } static PyType_Slot blake2b_type_slots[] = { diff --git a/Modules/_blake2/blake2s_impl.c b/Modules/_blake2/blake2s_impl.c index ffaf4315133061..4554b587d4941d 100644 --- a/Modules/_blake2/blake2s_impl.c +++ b/Modules/_blake2/blake2s_impl.c @@ -391,10 +391,8 @@ py_blake2s_dealloc(PyObject *self) obj->lock = NULL; } + Py_DECREF(Py_TYPE(self)); PyObject_Del(self); - - PyTypeObject *tp = Py_TYPE(self); - Py_DECREF(tp); } static PyType_Slot blake2s_type_slots[] = { From 4b2e87f5d147f7652d5890404690fa69b7649be8 Mon Sep 17 00:00:00 2001 From: Mohamed Koubaa Date: Thu, 27 Aug 2020 19:26:37 -0500 Subject: [PATCH 07/11] add traverse, clear, and free slots --- Modules/_blake2/blake2module.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/Modules/_blake2/blake2module.c b/Modules/_blake2/blake2module.c index 9b525fffed2d47..c35a36a015bcb0 100644 --- a/Modules/_blake2/blake2module.c +++ b/Modules/_blake2/blake2module.c @@ -34,6 +34,30 @@ static struct PyMethodDef blake2mod_functions[] = { {NULL, NULL} }; +static int +_blake2_traverse(PyObject *module, visitproc visit, void *arg) +{ + Blake2State *state = blake2_get_state(module); + Py_VISIT(state->blake2b_type); + Py_VISIT(state->blake2s_type); + return 0; +} + +static int +_blake2_clear(PyObject *module) +{ + Blake2State *state = blake2_get_state(module); + Py_CLEAR(state->blake2b_type); + Py_CLEAR(state->blake2s_type); + return 0; +} + +static void +_blake2_free(void *module) +{ + _blake2_clear((PyObject *)module); +} + #define ADD_INT(d, name, value) do { \ PyObject *x = PyLong_FromLong(value); \ if (!x) { \ @@ -109,7 +133,10 @@ static struct PyModuleDef blake2_module = { .m_doc = blake2mod__doc__, .m_size = sizeof(Blake2State), .m_methods = blake2mod_functions, - .m_slots = _blake2_slots + .m_slots = _blake2_slots, + .traverse = _blake2_traverse, + .clear = __blake2_clear, + .free = _blake2_free, }; PyMODINIT_FUNC From c0533a526ebda5e4d775c09bfb2a79079b50b201 Mon Sep 17 00:00:00 2001 From: Mohamed Koubaa Date: Thu, 27 Aug 2020 19:42:55 -0500 Subject: [PATCH 08/11] fix some typos --- Modules/_blake2/blake2module.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Modules/_blake2/blake2module.c b/Modules/_blake2/blake2module.c index c35a36a015bcb0..c72d340037e2be 100644 --- a/Modules/_blake2/blake2module.c +++ b/Modules/_blake2/blake2module.c @@ -134,9 +134,9 @@ static struct PyModuleDef blake2_module = { .m_size = sizeof(Blake2State), .m_methods = blake2mod_functions, .m_slots = _blake2_slots, - .traverse = _blake2_traverse, - .clear = __blake2_clear, - .free = _blake2_free, + .m_traverse = _blake2_traverse, + .m_clear = _blake2_clear, + .m_free = _blake2_free, }; PyMODINIT_FUNC From 033e59da5f64dfc8a20598011f587f47e70d1177 Mon Sep 17 00:00:00 2001 From: Mohamed Koubaa Date: Fri, 28 Aug 2020 21:08:37 -0500 Subject: [PATCH 09/11] pep7 & improve dealloc --- Modules/_blake2/blake2b_impl.c | 3 ++- Modules/_blake2/blake2module.c | 3 ++- Modules/_blake2/blake2s_impl.c | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Modules/_blake2/blake2b_impl.c b/Modules/_blake2/blake2b_impl.c index 449e68208cd7a6..704c6b0c48ee44 100644 --- a/Modules/_blake2/blake2b_impl.c +++ b/Modules/_blake2/blake2b_impl.c @@ -392,8 +392,9 @@ py_blake2b_dealloc(PyObject *self) obj->lock = NULL; } - Py_DECREF(Py_TYPE(self)); + PyObject *type = Py_TYPE(self); PyObject_Del(self); + Py_DECREF(type); } static PyType_Slot blake2b_type_slots[] = { diff --git a/Modules/_blake2/blake2module.c b/Modules/_blake2/blake2module.c index c72d340037e2be..56f53be4512914 100644 --- a/Modules/_blake2/blake2module.c +++ b/Modules/_blake2/blake2module.c @@ -24,7 +24,8 @@ typedef struct { PyTypeObject* blake2s_type; } Blake2State; -static inline Blake2State* blake2_get_state(PyObject *module) { +static inline Blake2State* blake2_get_state(PyObject *module) +{ void *state = PyModule_GetState(module); assert(state != NULL); return (Blake2State *)state; diff --git a/Modules/_blake2/blake2s_impl.c b/Modules/_blake2/blake2s_impl.c index 4554b587d4941d..7629ec729c6a58 100644 --- a/Modules/_blake2/blake2s_impl.c +++ b/Modules/_blake2/blake2s_impl.c @@ -391,8 +391,9 @@ py_blake2s_dealloc(PyObject *self) obj->lock = NULL; } - Py_DECREF(Py_TYPE(self)); + PyObject *type = Py_TYPE(self); PyObject_Del(self); + Py_DECREF(type); } static PyType_Slot blake2s_type_slots[] = { From 1fe0fa2983149170f6c6514714f294eceeadde31 Mon Sep 17 00:00:00 2001 From: Mohamed Koubaa Date: Sat, 29 Aug 2020 11:12:49 -0500 Subject: [PATCH 10/11] fix issues --- Modules/_blake2/blake2b_impl.c | 4 +--- Modules/_blake2/blake2module.c | 11 +++++------ Modules/_blake2/blake2s_impl.c | 4 +--- 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/Modules/_blake2/blake2b_impl.c b/Modules/_blake2/blake2b_impl.c index 704c6b0c48ee44..8e1acce56b1d29 100644 --- a/Modules/_blake2/blake2b_impl.c +++ b/Modules/_blake2/blake2b_impl.c @@ -392,7 +392,7 @@ py_blake2b_dealloc(PyObject *self) obj->lock = NULL; } - PyObject *type = Py_TYPE(self); + PyTypeObject *type = Py_TYPE(self); PyObject_Del(self); Py_DECREF(type); } @@ -406,8 +406,6 @@ static PyType_Slot blake2b_type_slots[] = { {0,0} }; -// Using PyType_GetModuleState() on this type is safe since -// it cannot be subclassed: it does not have the Py_TPFLAGS_BASETYPE flag. PyType_Spec blake2b_type_spec = { .name = "_blake2.blake2b", .basicsize = sizeof(BLAKE2bObject), diff --git a/Modules/_blake2/blake2module.c b/Modules/_blake2/blake2module.c index 56f53be4512914..631de2cc0abc74 100644 --- a/Modules/_blake2/blake2module.c +++ b/Modules/_blake2/blake2module.c @@ -24,7 +24,8 @@ typedef struct { PyTypeObject* blake2s_type; } Blake2State; -static inline Blake2State* blake2_get_state(PyObject *module) +static inline Blake2State* +blake2_get_state(PyObject *module) { void *state = PyModule_GetState(module); assert(state != NULL); @@ -61,10 +62,8 @@ _blake2_free(void *module) #define ADD_INT(d, name, value) do { \ PyObject *x = PyLong_FromLong(value); \ - if (!x) { \ - Py_DECREF(x); \ + if (!x) \ return -1; \ - } \ if (PyDict_SetItemString(d, name, x) < 0) { \ Py_DECREF(x); \ return -1; \ @@ -72,8 +71,8 @@ _blake2_free(void *module) Py_DECREF(x); \ } while(0) - -static int blake2_exec(PyObject *m) +static int +blake2_exec(PyObject *m) { Blake2State* st = blake2_get_state(m); diff --git a/Modules/_blake2/blake2s_impl.c b/Modules/_blake2/blake2s_impl.c index 7629ec729c6a58..e1de5df37d0988 100644 --- a/Modules/_blake2/blake2s_impl.c +++ b/Modules/_blake2/blake2s_impl.c @@ -391,7 +391,7 @@ py_blake2s_dealloc(PyObject *self) obj->lock = NULL; } - PyObject *type = Py_TYPE(self); + PyTypeObject *type = Py_TYPE(self); PyObject_Del(self); Py_DECREF(type); } @@ -405,8 +405,6 @@ static PyType_Slot blake2s_type_slots[] = { {0,0} }; -// Using PyType_GetModuleState() on this type is safe since -// it cannot be subclassed: it does not have the Py_TPFLAGS_BASETYPE flag. PyType_Spec blake2s_type_spec = { .name = "_blake2.blake2s", .basicsize = sizeof(BLAKE2sObject), From d63f02c612a91ac371951b69879ad9d111f554c8 Mon Sep 17 00:00:00 2001 From: Mohamed Koubaa Date: Tue, 1 Sep 2020 16:58:24 -0500 Subject: [PATCH 11/11] update news --- .../2020-08-13-07-18-05.bpo-1635741.FC13e7.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-08-13-07-18-05.bpo-1635741.FC13e7.rst b/Misc/NEWS.d/next/Core and Builtins/2020-08-13-07-18-05.bpo-1635741.FC13e7.rst index 83ba710b584148..cdfee874095fe5 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2020-08-13-07-18-05.bpo-1635741.FC13e7.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2020-08-13-07-18-05.bpo-1635741.FC13e7.rst @@ -1 +1 @@ -Port :mod:`_blake2` to multi-phase init +Port the :mod:`_blake2` extension module to the multi-phase initialization API (:pep:`489`).