More multi-phase init for extensions

This commit is contained in:
Kovid Goyal 2020-10-24 20:09:42 +05:30
parent 7516a8bda9
commit da1b470b54
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 54 additions and 91 deletions

View File

@ -197,22 +197,17 @@ static PyMethodDef cPalmdoc_methods[] = {
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef cPalmdoc_module = {
/* m_base */ PyModuleDef_HEAD_INIT,
/* m_name */ "cPalmdoc",
/* m_doc */ cPalmdoc_doc,
/* m_size */ -1,
/* m_methods */ cPalmdoc_methods,
/* m_slots */ 0,
/* m_traverse */ 0,
/* m_clear */ 0,
/* m_free */ 0,
};
CALIBRE_MODINIT_FUNC PyInit_cPalmdoc(void) {
PyObject *m = PyModule_Create(&cPalmdoc_module);
if (m == NULL) {
return NULL;
}
static int
exec_module(PyObject *module) { return 0; }
return m;
}
static PyModuleDef_Slot slots[] = { {Py_mod_exec, exec_module}, {0, NULL} };
static struct PyModuleDef module_def = {
.m_base = PyModuleDef_HEAD_INIT,
.m_name = "cPalmdoc",
.m_doc = cPalmdoc_doc,
.m_methods = cPalmdoc_methods,
.m_slots = slots,
};
CALIBRE_MODINIT_FUNC PyInit_cPalmdoc(void) { return PyModuleDef_Init(&module_def); }

View File

@ -694,22 +694,16 @@ static PyMethodDef bzzdec_methods[] = {
{NULL, NULL, 0, NULL}
};
#define INITMODULE
static struct PyModuleDef bzzdec_module = {
/* m_base */ PyModuleDef_HEAD_INIT,
/* m_name */ "bzzdec",
/* m_doc */ bzzdec_doc,
/* m_size */ -1,
/* m_methods */ bzzdec_methods,
/* m_slots */ 0,
/* m_traverse */ 0,
/* m_clear */ 0,
/* m_free */ 0,
static int
exec_module(PyObject *module) { return 0; }
static PyModuleDef_Slot slots[] = { {Py_mod_exec, exec_module}, {0, NULL} };
static struct PyModuleDef module_def = {
.m_base = PyModuleDef_HEAD_INIT,
.m_name = "bzzdec",
.m_doc = bzzdec_doc,
.m_methods = bzzdec_methods,
.m_slots = slots,
};
CALIBRE_MODINIT_FUNC PyInit_bzzdec(void) {
PyObject *m = PyModule_Create(&bzzdec_module);
if (m == NULL) {
return NULL;
}
return m;
}
CALIBRE_MODINIT_FUNC PyInit_bzzdec(void) { return PyModuleDef_Init(&module_def); }

View File

@ -430,23 +430,17 @@ static PyMethodDef methods[] = {
},
{NULL} /* Sentinel */
};
static int
exec_module(PyObject *mod) { return 0; }
static PyModuleDef_Slot slots[] = { {Py_mod_exec, (void*)exec_module}, {0, NULL} };
static struct PyModuleDef module_def = {PyModuleDef_HEAD_INIT};
static struct PyModuleDef hmod = {
/* m_base */ PyModuleDef_HEAD_INIT,
/* m_name */ "html_as_json",
/* m_doc */ doc,
/* m_size */ -1,
/* m_methods */ methods,
/* m_slots */ 0,
/* m_traverse */ 0,
/* m_clear */ 0,
/* m_free */ 0,
};
CALIBRE_MODINIT_FUNC PyInit_html_as_json(void) {
PyObject* m = PyModule_Create(&hmod);
if (m == NULL) {
return NULL;
module_def.m_name = "html_as_json";
module_def.m_slots = slots;
module_def.m_doc = doc;
module_def.m_methods = methods;
return PyModuleDef_Init(&module_def);
}
return m;
}
// }}}

View File

@ -36,50 +36,30 @@ PyLogMessage log_message;
static char podofo_doc[] = "Wrapper for the PoDoFo PDF library";
static PyMethodDef podofo_methods[] = {
{NULL} /* Sentinel */
};
static struct PyModuleDef podofo_module = {
/* m_base */ PyModuleDef_HEAD_INIT,
/* m_name */ "podofo",
/* m_doc */ podofo_doc,
/* m_size */ -1,
/* m_methods */ podofo_methods,
/* m_slots */ 0,
/* m_traverse */ 0,
/* m_clear */ 0,
/* m_free */ 0,
};
CALIBRE_MODINIT_FUNC PyInit_podofo(void) {
PyObject* m;
if (PyType_Ready(&pdf::PDFDocType) < 0) {
return NULL;
}
if (PyType_Ready(&pdf::PDFOutlineItemType) < 0) {
return NULL;
}
static int
exec_module(PyObject *m) {
if (PyType_Ready(&pdf::PDFDocType) < 0) return -1;
if (PyType_Ready(&pdf::PDFOutlineItemType) < 0) return -1;
pdf::Error = PyErr_NewException((char*)"podofo.Error", NULL, NULL);
if (pdf::Error == NULL) {
return NULL;
}
if (pdf::Error == NULL) return -1;
PyModule_AddObject(m, "Error", pdf::Error);
PdfError::SetLogMessageCallback((PdfError::LogMessageCallback*)&log_message);
PdfError::EnableDebug(false);
m = PyModule_Create(&podofo_module);
if (m == NULL) {
return NULL;
}
Py_INCREF(&pdf::PDFDocType);
PyModule_AddObject(m, "PDFDoc", (PyObject *)&pdf::PDFDocType);
PyModule_AddObject(m, "Error", pdf::Error);
return m;
return 0;
}
static PyModuleDef_Slot slots[] = { {Py_mod_exec, (void*)exec_module}, {0, NULL} };
static struct PyModuleDef module_def = {PyModuleDef_HEAD_INIT};
CALIBRE_MODINIT_FUNC PyInit_podofo(void) {
module_def.m_name = "podofo";
module_def.m_doc = podofo_doc;
module_def.m_slots = slots;
return PyModuleDef_Init(&module_def);
}