summaryrefslogtreecommitdiff
path: root/psutil/_psutil_linux.c
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2019-10-21 14:43:06 +0800
committerGitHub <noreply@github.com>2019-10-21 14:43:06 +0800
commit2ac1cd40168677bfc550bf34a6f04972ed745374 (patch)
tree5f9bf86104c8df465043bff81d725f128ceefac2 /psutil/_psutil_linux.c
parentc20d734ed387476cd79b711684b5a295baf8b8b0 (diff)
downloadpsutil-2ac1cd40168677bfc550bf34a6f04972ed745374.tar.gz
Refactor C modules init (#1603)
Diffstat (limited to 'psutil/_psutil_linux.c')
-rw-r--r--psutil/_psutil_linux.c128
1 files changed, 52 insertions, 76 deletions
diff --git a/psutil/_psutil_linux.c b/psutil/_psutil_linux.c
index 8151b75d..717723d0 100644
--- a/psutil/_psutil_linux.c
+++ b/psutil/_psutil_linux.c
@@ -537,10 +537,10 @@ error:
/*
- * Define the psutil C module methods and initialize the module.
+ * Module init.
*/
-static PyMethodDef
-PsutilMethods[] = {
+
+static PyMethodDef mod_methods[] = {
// --- per-process functions
#ifdef PSUTIL_HAVE_IOPRIO
@@ -574,7 +574,6 @@ PsutilMethods[] = {
{"linux_prlimit", psutil_linux_prlimit, METH_VARARGS,
"Get or set process resource limits."},
#endif
-
// --- others
{"set_testing", psutil_set_testing, METH_NOARGS,
"Set psutil in testing mode"},
@@ -582,75 +581,51 @@ PsutilMethods[] = {
{NULL, NULL, 0, NULL}
};
-struct module_state {
- PyObject *error;
-};
-
-#if PY_MAJOR_VERSION >= 3
-#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
-#else
-#define GETSTATE(m) (&_state)
-#endif
#if PY_MAJOR_VERSION >= 3
-
-static int
-psutil_linux_traverse(PyObject *m, visitproc visit, void *arg) {
- Py_VISIT(GETSTATE(m)->error);
- return 0;
-}
-
-static int
-psutil_linux_clear(PyObject *m) {
- Py_CLEAR(GETSTATE(m)->error);
- return 0;
-}
-
-static struct PyModuleDef
- moduledef = {
- PyModuleDef_HEAD_INIT,
- "psutil_linux",
- NULL,
- sizeof(struct module_state),
- PsutilMethods,
- NULL,
- psutil_linux_traverse,
- psutil_linux_clear,
- NULL
-};
-
-#define INITERROR return NULL
-
-PyMODINIT_FUNC PyInit__psutil_linux(void)
-
-#else
-#define INITERROR return
-
-void init_psutil_linux(void)
-#endif
+ #define INITERR return NULL
+
+ static struct PyModuleDef moduledef = {
+ PyModuleDef_HEAD_INIT,
+ "_psutil_linux",
+ NULL,
+ -1,
+ mod_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
+ };
+
+ PyObject *PyInit__psutil_linux(void)
+#else /* PY_MAJOR_VERSION */
+ #define INITERR return
+
+ void init_psutil_linux(void)
+#endif /* PY_MAJOR_VERSION */
{
PyObject *v;
#if PY_MAJOR_VERSION >= 3
- PyObject *module = PyModule_Create(&moduledef);
+ PyObject *mod = PyModule_Create(&moduledef);
#else
- PyObject *module = Py_InitModule("_psutil_linux", PsutilMethods);
+ PyObject *mod = Py_InitModule("_psutil_linux", mod_methods);
#endif
- if (module == NULL)
- INITERROR;
+ if (mod == NULL)
+ INITERR;
- PyModule_AddIntConstant(module, "version", PSUTIL_VERSION);
+ if (PyModule_AddIntConstant(mod, "version", PSUTIL_VERSION)) INITERR;
#if PSUTIL_HAVE_PRLIMIT
- PyModule_AddIntConstant(module, "RLIMIT_AS", RLIMIT_AS);
- PyModule_AddIntConstant(module, "RLIMIT_CORE", RLIMIT_CORE);
- PyModule_AddIntConstant(module, "RLIMIT_CPU", RLIMIT_CPU);
- PyModule_AddIntConstant(module, "RLIMIT_DATA", RLIMIT_DATA);
- PyModule_AddIntConstant(module, "RLIMIT_FSIZE", RLIMIT_FSIZE);
- PyModule_AddIntConstant(module, "RLIMIT_LOCKS", RLIMIT_LOCKS);
- PyModule_AddIntConstant(module, "RLIMIT_MEMLOCK", RLIMIT_MEMLOCK);
- PyModule_AddIntConstant(module, "RLIMIT_NOFILE", RLIMIT_NOFILE);
- PyModule_AddIntConstant(module, "RLIMIT_NPROC", RLIMIT_NPROC);
- PyModule_AddIntConstant(module, "RLIMIT_RSS", RLIMIT_RSS);
- PyModule_AddIntConstant(module, "RLIMIT_STACK", RLIMIT_STACK);
+ if (PyModule_AddIntConstant(mod, "RLIMIT_AS", RLIMIT_AS)) INITERR;
+ if (PyModule_AddIntConstant(mod, "RLIMIT_CORE", RLIMIT_CORE)) INITERR;
+ if (PyModule_AddIntConstant(mod, "RLIMIT_CPU", RLIMIT_CPU)) INITERR;
+ if (PyModule_AddIntConstant(mod, "RLIMIT_DATA", RLIMIT_DATA)) INITERR;
+ if (PyModule_AddIntConstant(mod, "RLIMIT_FSIZE", RLIMIT_FSIZE)) INITERR;
+ if (PyModule_AddIntConstant(mod, "RLIMIT_LOCKS", RLIMIT_LOCKS)) INITERR;
+ if (PyModule_AddIntConstant(mod, "RLIMIT_MEMLOCK", RLIMIT_MEMLOCK)) INITERR;
+ if (PyModule_AddIntConstant(mod, "RLIMIT_NOFILE", RLIMIT_NOFILE)) INITERR;
+ if (PyModule_AddIntConstant(mod, "RLIMIT_NPROC", RLIMIT_NPROC)) INITERR;
+ if (PyModule_AddIntConstant(mod, "RLIMIT_RSS", RLIMIT_RSS)) INITERR;
+ if (PyModule_AddIntConstant(mod, "RLIMIT_STACK", RLIMIT_STACK)) INITERR;
#if defined(HAVE_LONG_LONG)
if (sizeof(RLIM_INFINITY) > sizeof(long)) {
@@ -661,32 +636,33 @@ void init_psutil_linux(void)
v = PyLong_FromLong((long) RLIM_INFINITY);
}
if (v) {
- PyModule_AddObject(module, "RLIM_INFINITY", v);
+ PyModule_AddObject(mod, "RLIM_INFINITY", v);
}
#ifdef RLIMIT_MSGQUEUE
- PyModule_AddIntConstant(module, "RLIMIT_MSGQUEUE", RLIMIT_MSGQUEUE);
+ if (PyModule_AddIntConstant(mod, "RLIMIT_MSGQUEUE", RLIMIT_MSGQUEUE)) INITERR;
#endif
#ifdef RLIMIT_NICE
- PyModule_AddIntConstant(module, "RLIMIT_NICE", RLIMIT_NICE);
+ if (PyModule_AddIntConstant(mod, "RLIMIT_NICE", RLIMIT_NICE)) INITERR;
#endif
#ifdef RLIMIT_RTPRIO
- PyModule_AddIntConstant(module, "RLIMIT_RTPRIO", RLIMIT_RTPRIO);
+ if (PyModule_AddIntConstant(mod, "RLIMIT_RTPRIO", RLIMIT_RTPRIO)) INITERR;
#endif
#ifdef RLIMIT_RTTIME
- PyModule_AddIntConstant(module, "RLIMIT_RTTIME", RLIMIT_RTTIME);
+ if (PyModule_AddIntConstant(mod, "RLIMIT_RTTIME", RLIMIT_RTTIME)) INITERR;
#endif
#ifdef RLIMIT_SIGPENDING
- PyModule_AddIntConstant(module, "RLIMIT_SIGPENDING", RLIMIT_SIGPENDING);
+ if (PyModule_AddIntConstant(mod, "RLIMIT_SIGPENDING", RLIMIT_SIGPENDING))
+ INITERR;
#endif
#endif
- PyModule_AddIntConstant(module, "DUPLEX_HALF", DUPLEX_HALF);
- PyModule_AddIntConstant(module, "DUPLEX_FULL", DUPLEX_FULL);
- PyModule_AddIntConstant(module, "DUPLEX_UNKNOWN", DUPLEX_UNKNOWN);
+ if (PyModule_AddIntConstant(mod, "DUPLEX_HALF", DUPLEX_HALF)) INITERR;
+ if (PyModule_AddIntConstant(mod, "DUPLEX_FULL", DUPLEX_FULL)) INITERR;
+ if (PyModule_AddIntConstant(mod, "DUPLEX_UNKNOWN", DUPLEX_UNKNOWN)) INITERR;
- if (module == NULL)
- INITERROR;
+ if (mod == NULL)
+ INITERR;
#if PY_MAJOR_VERSION >= 3
- return module;
+ return mod;
#endif
}