diff --git a/Modules/structmodule.c b/Modules/structmodule.c index 8cc49450634..86ac366c9f4 100644 --- a/Modules/structmodule.c +++ b/Modules/structmodule.c @@ -1502,16 +1502,18 @@ static PyMethodDef struct_methods[] = { DL_EXPORT(void) initstruct(void) { - PyObject *m, *d; + PyObject *m; /* Create the module and add the functions */ m = Py_InitModule4("struct", struct_methods, struct__doc__, (PyObject*)NULL, PYTHON_API_VERSION); /* Add some symbolic constants to the module */ - d = PyModule_GetDict(m); - StructError = PyErr_NewException("struct.error", NULL, NULL); - if (StructError == NULL) - return; - PyDict_SetItemString(d, "error", StructError); + if (StructError == NULL) { + StructError = PyErr_NewException("struct.error", NULL, NULL); + if (StructError == NULL) + return; + } + Py_INCREF(StructError); + PyModule_AddObject(d, "error", StructError); } diff --git a/Modules/termios.c b/Modules/termios.c index 3362e8158c2..aaabe6034f9 100644 --- a/Modules/termios.c +++ b/Modules/termios.c @@ -891,15 +891,17 @@ static struct constant { DL_EXPORT(void) PyInit_termios(void) { - PyObject *m, *d; + PyObject *m; struct constant *constant = termios_constants; m = Py_InitModule4("termios", termios_methods, termios__doc__, (PyObject *)NULL, PYTHON_API_VERSION); - d = PyModule_GetDict(m); - TermiosError = PyErr_NewException("termios.error", NULL, NULL); - PyDict_SetItemString(d, "error", TermiosError); + if (TermiosError == NULL) { + TermiosError = PyErr_NewException("termios.error", NULL, NULL); + } + Py_INCREF(TermiosError); + PyModule_AddObject(m, "error", TermiosError); while (constant->name != NULL) { PyModule_AddIntConstant(m, constant->name, constant->value);