From: Amit Bawer <abawer(a)redhat.com>
This will serve python 3 porting in future patches to come.
Signed-off-by: Amit Bawer <abawer(a)redhat.com>
---
python/sanlock.c | 103 +++++++++++++++++++++++++++--------------------
1 file changed, 60 insertions(+), 43 deletions(-)
diff --git a/python/sanlock.c b/python/sanlock.c
index 446a4b5..420b4ba 100644
--- a/python/sanlock.c
+++ b/python/sanlock.c
@@ -28,10 +28,12 @@
__attribute__((cpychecker_negative_result_sets_exception))
#else
#define __neg_sets_exception
#endif
+#define MODULE_NAME "sanlock"
+
/* Functions prototypes */
static void __set_exception(int en, char *msg) __sets_exception;
static int __parse_resource(PyObject *obj, struct sanlk_resource **res_ret)
__neg_sets_exception;
static void set_value_error(const char* format, PyObject* obj);
@@ -1700,79 +1702,94 @@ exit_fail:
Py_XDECREF(dict);
return excp;
}
-PyMODINIT_FUNC
-initsanlock(void)
+static int
+module_init(PyObject* m)
{
- PyObject *py_module, *sk_constant;
-
- py_module = Py_InitModule4("sanlock",
- sanlock_methods, pydoc_sanlock, NULL, PYTHON_API_VERSION);
-
- if (py_module == NULL)
- return;
-
if (py_exception == NULL) {
py_exception = initexception();
if (py_exception == NULL)
- return;
+ return -1;
}
Py_INCREF(py_exception);
- if (PyModule_AddObject(py_module, "SanlockException", py_exception)) {
+ if (PyModule_AddObject(m, "SanlockException", py_exception)) {
Py_DECREF(py_exception);
- return;
- }
-
-#define PYSNLK_INIT_ADD_CONSTANT(x, y) \
- if ((sk_constant = PyInt_FromLong(x)) != NULL) { \
- if (PyModule_AddObject(py_module, y, sk_constant)) { \
- Py_DECREF(sk_constant); \
- } \
+ return -1;
}
/* lockspaces list flags */
- PYSNLK_INIT_ADD_CONSTANT(SANLK_LSF_ADD, "LSFLAG_ADD");
- PYSNLK_INIT_ADD_CONSTANT(SANLK_LSF_REM, "LSFLAG_REM");
+ if (PyModule_AddIntConstant(m, "LSFLAG_ADD", SANLK_LSF_ADD))
+ return -1;
+ if (PyModule_AddIntConstant(m, "LSFLAG_REM", SANLK_LSF_REM))
+ return -1;
/* resource request flags */
- PYSNLK_INIT_ADD_CONSTANT(SANLK_REQ_FORCE, "REQ_FORCE");
- PYSNLK_INIT_ADD_CONSTANT(SANLK_REQ_GRACEFUL, "REQ_GRACEFUL");
+ if (PyModule_AddIntConstant(m, "REQ_FORCE", SANLK_REQ_FORCE))
+ return -1;
+ if (PyModule_AddIntConstant(m, "REQ_GRACEFUL", SANLK_REQ_GRACEFUL))
+ return -1;
/* hosts list flags */
- PYSNLK_INIT_ADD_CONSTANT(SANLK_HOST_FREE, "HOST_FREE");
- PYSNLK_INIT_ADD_CONSTANT(SANLK_HOST_LIVE, "HOST_LIVE");
- PYSNLK_INIT_ADD_CONSTANT(SANLK_HOST_FAIL, "HOST_FAIL");
- PYSNLK_INIT_ADD_CONSTANT(SANLK_HOST_DEAD, "HOST_DEAD");
- PYSNLK_INIT_ADD_CONSTANT(SANLK_HOST_UNKNOWN, "HOST_UNKNOWN");
+ if (PyModule_AddIntConstant(m, "HOST_FREE", SANLK_HOST_FREE))
+ return -1;
+ if (PyModule_AddIntConstant(m, "HOST_LIVE", SANLK_HOST_LIVE))
+ return -1;
+ if (PyModule_AddIntConstant(m, "HOST_FAIL", SANLK_HOST_FAIL))
+ return -1;
+ if (PyModule_AddIntConstant(m, "HOST_DEAD", SANLK_HOST_DEAD))
+ return -1;
+ if (PyModule_AddIntConstant(m, "HOST_UNKNOWN", SANLK_HOST_UNKNOWN))
+ return -1;
/* set event flags */
- PYSNLK_INIT_ADD_CONSTANT(SANLK_SETEV_CUR_GENERATION,
"SETEV_CUR_GENERATION");
- PYSNLK_INIT_ADD_CONSTANT(SANLK_SETEV_CLEAR_HOSTID,
"SETEV_CLEAR_HOSTID");
- PYSNLK_INIT_ADD_CONSTANT(SANLK_SETEV_CLEAR_EVENT, "SETEV_CLEAR_EVENT");
- PYSNLK_INIT_ADD_CONSTANT(SANLK_SETEV_REPLACE_EVENT,
"SETEV_REPLACE_EVENT");
- PYSNLK_INIT_ADD_CONSTANT(SANLK_SETEV_ALL_HOSTS, "SETEV_ALL_HOSTS");
-
-#undef PYSNLK_INIT_ADD_CONSTANT
+ if (PyModule_AddIntConstant(m, "SETEV_CUR_GENERATION",
SANLK_SETEV_CUR_GENERATION))
+ return -1;
+ if (PyModule_AddIntConstant(m, "SETEV_CLEAR_HOSTID",
SANLK_SETEV_CLEAR_HOSTID))
+ return -1;
+ if (PyModule_AddIntConstant(m, "SETEV_CLEAR_EVENT",
SANLK_SETEV_CLEAR_EVENT))
+ return -1;
+ if (PyModule_AddIntConstant(m, "SETEV_REPLACE_EVENT",
SANLK_SETEV_REPLACE_EVENT))
+ return -1;
+ if (PyModule_AddIntConstant(m, "SETEV_ALL_HOSTS", SANLK_SETEV_ALL_HOSTS))
+ return -1;
/* Tuples with supported sector size and alignment values */
+
PyObject *sector = Py_BuildValue("ii", SECTOR_SIZE_512, SECTOR_SIZE_4K);
if (!sector)
- return;
- if (PyModule_AddObject(py_module, "SECTOR_SIZE", sector)) {
+ return -1;
+ if (PyModule_AddObject(m, "SECTOR_SIZE", sector)) {
Py_DECREF(sector);
- return;
+ return -1;
}
- PyObject *align = Py_BuildValue("llll", ALIGNMENT_1M, ALIGNMENT_2M,
ALIGNMENT_4M, ALIGNMENT_8M);
+ PyObject *align = Py_BuildValue(
+ "llll", ALIGNMENT_1M, ALIGNMENT_2M, ALIGNMENT_4M, ALIGNMENT_8M);
if (!align)
- return;
- if (PyModule_AddObject(py_module, "ALIGN_SIZE", align)) {
+ return -1;
+ if (PyModule_AddObject(m, "ALIGN_SIZE", align)) {
Py_DECREF(align);
- return;
+ return -1;
}
+ return 0;
+}
+
+PyMODINIT_FUNC
+initsanlock(void)
+{
+ PyObject *m = Py_InitModule3(
+ MODULE_NAME,
+ sanlock_methods,
+ pydoc_sanlock);
+
+ if (m == NULL)
+ return;
+
+ /* We don't have anything to do if module_init() fails. */
+ module_init(m);
}
/* vim: set expandtab shiftwidth=4 tabstop=4 : */
--
2.17.2