diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-06-26-22-22-44.bpo-1635741.mE0WHS.rst b/Misc/NEWS.d/next/Core and Builtins/2020-06-26-22-22-44.bpo-1635741.mE0WHS.rst new file mode 100644 index 00000000000000..1947d5adb3608b --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2020-06-26-22-22-44.bpo-1635741.mE0WHS.rst @@ -0,0 +1 @@ +Port :mod:`termios` to multiphase initialization. diff --git a/Modules/termios.c b/Modules/termios.c index 75e5e523206f41..62bdb1f44cc433 100644 --- a/Modules/termios.c +++ b/Modules/termios.c @@ -997,44 +997,43 @@ static void termiosmodule_free(void *m) { termiosmodule_clear((PyObject *)m); } -static struct PyModuleDef termiosmodule = { - PyModuleDef_HEAD_INIT, - "termios", - termios__doc__, - sizeof(termiosmodulestate), - termios_methods, - NULL, - termiosmodule_traverse, - termiosmodule_clear, - termiosmodule_free, -}; - -PyMODINIT_FUNC -PyInit_termios(void) -{ - PyObject *m; +static int +termios_exec(PyObject *module) { struct constant *constant = termios_constants; - - if ((m = PyState_FindModule(&termiosmodule)) != NULL) { - Py_INCREF(m); - return m; - } - - if ((m = PyModule_Create(&termiosmodule)) == NULL) { - return NULL; - } - - termiosmodulestate *state = get_termios_state(m); + termiosmodulestate *state = get_termios_state(module); state->TermiosError = PyErr_NewException("termios.error", NULL, NULL); - if (state->TermiosError == NULL) { - return NULL; + if (PyModule_AddType(module, state->TermiosError) < 0) { + return -1; } - Py_INCREF(state->TermiosError); - PyModule_AddObject(m, "error", state->TermiosError); while (constant->name != NULL) { - PyModule_AddIntConstant(m, constant->name, constant->value); + if (PyModule_AddIntConstant(module, constant->name, constant->value) < 0) { + return -1; + } ++constant; } - return m; + return 0; +} + +static PyModuleDef_Slot termios_slots[] = { + {Py_mod_exec, termios_exec}, + {0, NULL} +}; + +static struct PyModuleDef termiosmodule = { + PyModuleDef_HEAD_INIT, + .m_name = "termios", + .m_doc = termios__doc__, + .m_size = sizeof(termiosmodulestate), + .m_methods = termios_methods, + .m_slots = termios_slots, + .m_traverse = termiosmodule_traverse, + .m_clear = termiosmodule_clear, + .m_free = termiosmodule_free, +}; + +PyMODINIT_FUNC +PyInit_termios(void) +{ + return PyModuleDef_Init(&termiosmodule); }