diff --git a/src/calibre/utils/speedup.c b/src/calibre/utils/speedup.c index 369a563f9f..c0f018ad17 100644 --- a/src/calibre/utils/speedup.c +++ b/src/calibre/utils/speedup.c @@ -577,25 +577,21 @@ static PyMethodDef speedup_methods[] = { {NULL, NULL, 0, NULL} }; +static int +exec_module(PyObject *module) { + PyDateTime_IMPORT; + PyModule_AddIntConstant(module, "O_CLOEXEC", O_CLOEXEC); + return 0; +} -static struct PyModuleDef speedup_module = { - /* m_base */ PyModuleDef_HEAD_INIT, - /* m_name */ "speedup", - /* m_doc */ "Implementation of methods in C for speed.", - /* m_size */ -1, - /* m_methods */ speedup_methods, - /* m_slots */ 0, - /* m_traverse */ 0, - /* m_clear */ 0, - /* m_free */ 0, +static PyModuleDef_Slot slots[] = { {Py_mod_exec, exec_module}, {0, NULL} }; + +static struct PyModuleDef module_def = { + .m_base = PyModuleDef_HEAD_INIT, + .m_name = "speedup", + .m_doc = "Implementation of methods in C for speed.", + .m_methods = speedup_methods, + .m_slots = slots, }; -CALIBRE_MODINIT_FUNC PyInit_speedup(void) { - PyObject *mod = PyModule_Create(&speedup_module); - if (mod == NULL) return NULL; - PyDateTime_IMPORT; -#ifdef O_CLOEXEC - PyModule_AddIntConstant(mod, "O_CLOEXEC", O_CLOEXEC); -#endif - return mod; -} +CALIBRE_MODINIT_FUNC PyInit_speedup(void) { return PyModuleDef_Init(&module_def); } diff --git a/src/calibre/utils/spell/hunspell_wrapper.cpp b/src/calibre/utils/spell/hunspell_wrapper.cpp index 7e518be716..d002ae14db 100644 --- a/src/calibre/utils/spell/hunspell_wrapper.cpp +++ b/src/calibre/utils/spell/hunspell_wrapper.cpp @@ -166,32 +166,28 @@ static PyTypeObject DictionaryType = { /* tp_new */ 0, }; -static struct PyModuleDef hunspell_module = { - /* m_base */ PyModuleDef_HEAD_INIT, - /* m_name */ "hunspell", - /* m_doc */ "A wrapper for the hunspell spell checking library", - /* m_size */ -1, - /* m_methods */ 0, - /* m_slots */ 0, - /* m_traverse */ 0, - /* m_clear */ 0, - /* m_free */ 0, -}; - -CALIBRE_MODINIT_FUNC PyInit_hunspell(void) { - PyObject *mod = PyModule_Create(&hunspell_module); - if (mod == NULL) return NULL; - +static int +exec_module(PyObject *mod) { HunspellError = PyErr_NewException((char*)"hunspell.HunspellError", NULL, NULL); - if (HunspellError == NULL) return NULL; + if (HunspellError == NULL) return -1; PyModule_AddObject(mod, "HunspellError", HunspellError); // Fill in some slots in the type, and make it ready DictionaryType.tp_new = PyType_GenericNew; - if (PyType_Ready(&DictionaryType) < 0) return NULL; + if (PyType_Ready(&DictionaryType) < 0) return -1; // Add the type to the module. Py_INCREF(&DictionaryType); - PyModule_AddObject(mod, "Dictionary", (PyObject *)&DictionaryType); + if (PyModule_AddObject(mod, "Dictionary", (PyObject *)&DictionaryType) != 0) return -1; - return mod; + return 0; } + +static PyModuleDef_Slot slots[] = { {Py_mod_exec, (void*)exec_module}, {0, NULL} }; + +static struct PyModuleDef module_def = { + .m_base = PyModuleDef_HEAD_INIT, + .m_name = "hunspell", + .m_slots = slots +}; + +CALIBRE_MODINIT_FUNC PyInit_hunspell(void) { return PyModuleDef_Init(&module_def); } diff --git a/src/calibre/utils/windows/winsapi.cpp b/src/calibre/utils/windows/winsapi.cpp index bad5cbd85d..9a2859abb2 100644 --- a/src/calibre/utils/windows/winsapi.cpp +++ b/src/calibre/utils/windows/winsapi.cpp @@ -336,23 +336,8 @@ static PyMethodDef winsapi_methods[] = { }; #undef M -static struct PyModuleDef winsapi_module = { - /* m_base */ PyModuleDef_HEAD_INIT, - /* m_name */ "winsapi", - /* m_doc */ "SAPI wrapper", - /* m_size */ -1, - /* m_methods */ winsapi_methods, - /* m_slots */ 0, - /* m_traverse */ 0, - /* m_clear */ 0, - /* m_free */ 0, -}; - - - -extern "C" { - -CALIBRE_MODINIT_FUNC PyInit_winsapi(void) { +static int +exec_module(PyObject *m) { VoiceType.tp_name = "winsapi.ISpVoice"; VoiceType.tp_doc = "Wrapper for ISpVoice"; VoiceType.tp_basicsize = sizeof(Voice); @@ -361,18 +346,14 @@ CALIBRE_MODINIT_FUNC PyInit_winsapi(void) { VoiceType.tp_new = Voice_new; VoiceType.tp_methods = Voice_methods; VoiceType.tp_dealloc = (destructor)Voice_dealloc; - if (PyType_Ready(&VoiceType) < 0) return NULL; - - PyObject *m = PyModule_Create(&winsapi_module); - if (m == NULL) return NULL; + if (PyType_Ready(&VoiceType) < 0) return -1; Py_INCREF(&VoiceType); if (PyModule_AddObject(m, "ISpVoice", (PyObject *) &VoiceType) < 0) { Py_DECREF(&VoiceType); - Py_DECREF(m); - return NULL; + return -1; } -#define AI(name) if (PyModule_AddIntMacro(m, name) != 0) { Py_DECREF(m); Py_DECREF(&VoiceType); return NULL; } +#define AI(name) if (PyModule_AddIntMacro(m, name) != 0) { Py_DECREF(&VoiceType); return -1; } AI(SPF_DEFAULT); AI(SPF_ASYNC); AI(SPF_PURGEBEFORESPEAK); @@ -521,7 +502,17 @@ CALIBRE_MODINIT_FUNC PyInit_winsapi(void) { AI(SPEI_RESERVED2); #undef AI - return m; } -} // }}} +static PyModuleDef_Slot slots[] = { {Py_mod_exec, (void*)exec_module}, {0, NULL} }; + +static struct PyModuleDef module_def = { + .m_base = PyModuleDef_HEAD_INIT, + .m_name = "winsapi", + .m_doc = "SAPI wrapper", + .m_methods = winsapi_methods, + .m_slots = slots, +}; + + +CALIBRE_MODINIT_FUNC PyInit_winsapi(void) {return PyModuleDef_Init(&module_def); } diff --git a/src/calibre/utils/windows/winutil.cpp b/src/calibre/utils/windows/winutil.cpp index 4c70a958e5..93af8b26a7 100644 --- a/src/calibre/utils/windows/winutil.cpp +++ b/src/calibre/utils/windows/winutil.cpp @@ -1197,32 +1197,15 @@ static PyMethodDef winutil_methods[] = { }; #undef M -static struct PyModuleDef winutil_module = { - /* m_base */ PyModuleDef_HEAD_INIT, - /* m_name */ "winutil", - /* m_doc */ winutil_doc, - /* m_size */ -1, - /* m_methods */ winutil_methods, - /* m_slots */ 0, - /* m_traverse */ 0, - /* m_clear */ 0, - /* m_free */ 0, -}; - - - -extern "C" { - -CALIBRE_MODINIT_FUNC PyInit_winutil(void) { +static int +exec_module(PyObject *m) { #define add_type(name, doc, obj) obj##Type.tp_name = "winutil." #name; obj##Type.tp_doc = doc; obj##Type.tp_basicsize = sizeof(obj); \ obj##Type.tp_itemsize = 0; obj##Type.tp_flags = Py_TPFLAGS_DEFAULT; obj##Type.tp_repr = (reprfunc)obj##_repr; \ obj##Type.tp_str = (reprfunc)obj##_repr; obj##Type.tp_new = obj##_new; obj##Type.tp_dealloc = (destructor)obj##_dealloc; \ obj##Type.tp_methods = obj##_methods; \ - if (PyType_Ready(&obj##Type) < 0) { Py_CLEAR(m); return NULL; } \ - Py_INCREF(&obj##Type); if (PyModule_AddObject(m, #name, (PyObject*) &obj##Type) < 0) { Py_CLEAR(m); Py_DECREF(&obj##Type); return NULL; } + if (PyType_Ready(&obj##Type) < 0) { return -1; } \ + Py_INCREF(&obj##Type); if (PyModule_AddObject(m, #name, (PyObject*) &obj##Type) < 0) { Py_DECREF(&obj##Type); return -1; } - PyObject *m = PyModule_Create(&winutil_module); - if (m == NULL) return NULL; HandleNumberMethods.nb_int = (unaryfunc)Handle_as_int; HandleNumberMethods.nb_bool = (inquiry)Handle_as_bool; @@ -1231,7 +1214,7 @@ CALIBRE_MODINIT_FUNC PyInit_winutil(void) { add_type(GUID, "Wrapper for Win32 GUID", PyGUID); #undef add_type -#define A(name) { PyObject *g = create_guid(FOLDERID_##name); if (!g) { Py_CLEAR(m); return NULL; } if (PyModule_AddObject(m, "FOLDERID_" #name, g) < 0) { Py_DECREF(g); Py_CLEAR(m); return NULL; } } +#define A(name) { PyObject *g = create_guid(FOLDERID_##name); if (!g) { return -1; } if (PyModule_AddObject(m, "FOLDERID_" #name, g) < 0) { Py_DECREF(g); return -1; } } A(AdminTools); A(Startup); A(RoamingAppData); @@ -1291,7 +1274,7 @@ CALIBRE_MODINIT_FUNC PyInit_winutil(void) { #undef A -#define A(name) if (PyModule_AddIntConstant(m, #name, name) != 0) { Py_CLEAR(m); return NULL; } +#define A(name) if (PyModule_AddIntConstant(m, #name, name) != 0) { return -1; } A(CSIDL_ADMINTOOLS); A(CSIDL_APPDATA); @@ -1407,6 +1390,18 @@ CALIBRE_MODINIT_FUNC PyInit_winutil(void) { A(KF_FLAG_SIMPLE_IDLIST); A(KF_FLAG_ALIAS_ONLY); #undef A - return m; + return 0; } -} // end extern "C" }}} + +static PyModuleDef_Slot slots[] = { {Py_mod_exec, (void*)exec_module}, {0, NULL} }; + +static struct PyModuleDef module_def = { + .m_base = PyModuleDef_HEAD_INIT, + .m_name = "winutil", + .m_doc = winutil_doc, + .m_methods = winutil_methods, + .m_slots = slots, +}; + + +CALIBRE_MODINIT_FUNC PyInit_winutil(void) {return PyModuleDef_Init(&module_def); }