From 1cb763b8808745b9a368c1158fda19d329f63f6f Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Tue, 31 Mar 2020 21:43:47 +0900 Subject: [PATCH] bpo-1635741: Port _uuid module to multiphase initialization (GH-19242) --- ...2020-03-31-21-12-27.bpo-1635741.S2nkF3.rst | 1 + Modules/_uuidmodule.c | 55 ++++++++++--------- 2 files changed, 30 insertions(+), 26 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2020-03-31-21-12-27.bpo-1635741.S2nkF3.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-03-31-21-12-27.bpo-1635741.S2nkF3.rst b/Misc/NEWS.d/next/Core and Builtins/2020-03-31-21-12-27.bpo-1635741.S2nkF3.rst new file mode 100644 index 00000000000..7d5a8ca21d2 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2020-03-31-21-12-27.bpo-1635741.S2nkF3.rst @@ -0,0 +1 @@ +Port _uuid module to multiphase initialization (:pep:`489`). diff --git a/Modules/_uuidmodule.c b/Modules/_uuidmodule.c index 0b7aa72e91a..3be6c848ad6 100644 --- a/Modules/_uuidmodule.c +++ b/Modules/_uuidmodule.c @@ -38,38 +38,41 @@ py_uuid_generate_time_safe(PyObject *Py_UNUSED(context), #endif } - -static PyMethodDef uuid_methods[] = { - {"generate_time_safe", py_uuid_generate_time_safe, METH_NOARGS, NULL}, - {NULL, NULL, 0, NULL} /* sentinel */ -}; - -static struct PyModuleDef uuidmodule = { - PyModuleDef_HEAD_INIT, - .m_name = "_uuid", - .m_size = -1, - .m_methods = uuid_methods, -}; - -PyMODINIT_FUNC -PyInit__uuid(void) -{ - PyObject *mod; +static int +uuid_exec(PyObject *module) { assert(sizeof(uuid_t) == 16); #ifdef HAVE_UUID_GENERATE_TIME_SAFE int has_uuid_generate_time_safe = 1; #else int has_uuid_generate_time_safe = 0; #endif - mod = PyModule_Create(&uuidmodule); - if (mod == NULL) { - return NULL; - } - if (PyModule_AddIntConstant(mod, "has_uuid_generate_time_safe", + if (PyModule_AddIntConstant(module, "has_uuid_generate_time_safe", has_uuid_generate_time_safe) < 0) { - Py_DECREF(mod); - return NULL; + return -1; } - - return mod; + return 0; +} + +static PyMethodDef uuid_methods[] = { + {"generate_time_safe", py_uuid_generate_time_safe, METH_NOARGS, NULL}, + {NULL, NULL, 0, NULL} /* sentinel */ +}; + +static PyModuleDef_Slot uuid_slots[] = { + {Py_mod_exec, uuid_exec}, + {0, NULL} +}; + +static struct PyModuleDef uuidmodule = { + PyModuleDef_HEAD_INIT, + .m_name = "_uuid", + .m_size = 0, + .m_methods = uuid_methods, + .m_slots = uuid_slots, +}; + +PyMODINIT_FUNC +PyInit__uuid(void) +{ + return PyModuleDef_Init(&uuidmodule); }