diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2019-10-21 14:43:06 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-21 14:43:06 +0800 |
commit | 2ac1cd40168677bfc550bf34a6f04972ed745374 (patch) | |
tree | 5f9bf86104c8df465043bff81d725f128ceefac2 /psutil/_psutil_posix.c | |
parent | c20d734ed387476cd79b711684b5a295baf8b8b0 (diff) | |
download | psutil-2ac1cd40168677bfc550bf34a6f04972ed745374.tar.gz |
Refactor C modules init (#1603)
Diffstat (limited to 'psutil/_psutil_posix.c')
-rw-r--r-- | psutil/_psutil_posix.c | 90 |
1 files changed, 34 insertions, 56 deletions
diff --git a/psutil/_psutil_posix.c b/psutil/_psutil_posix.c index 5d20b21e..209e787d 100644 --- a/psutil/_psutil_posix.c +++ b/psutil/_psutil_posix.c @@ -618,11 +618,15 @@ psutil_net_if_duplex_speed(PyObject *self, PyObject *args) { #endif // net_if_stats() macOS/BSD implementation +#ifdef __cplusplus +extern "C" { +#endif + + /* * define the psutil C module methods and initialize the module. */ -static PyMethodDef -PsutilMethods[] = { +static PyMethodDef mod_methods[] = { {"getpriority", psutil_posix_getpriority, METH_VARARGS, "Return process priority"}, {"setpriority", psutil_posix_setpriority, METH_VARARGS, @@ -640,71 +644,45 @@ 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 - -#ifdef __cplusplus -extern "C" { -#endif - -#if PY_MAJOR_VERSION >= 3 - -static int -psutil_posix_traverse(PyObject *m, visitproc visit, void *arg) { - Py_VISIT(GETSTATE(m)->error); - return 0; -} - - -static int -psutil_posix_clear(PyObject *m) { - Py_CLEAR(GETSTATE(m)->error); - return 0; -} - -static struct PyModuleDef moduledef = { - PyModuleDef_HEAD_INIT, - "psutil_posix", - NULL, - sizeof(struct module_state), - PsutilMethods, - NULL, - psutil_posix_traverse, - psutil_posix_clear, - NULL -}; - -#define INITERROR return NULL - -PyMODINIT_FUNC PyInit__psutil_posix(void) - -#else -#define INITERROR return - -void init_psutil_posix(void) -#endif + #define INITERR return NULL + + static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, + "_psutil_posix", + NULL, + -1, + mod_methods, + NULL, + NULL, + NULL, + NULL + }; + + PyObject *PyInit__psutil_posix(void) +#else /* PY_MAJOR_VERSION */ + #define INITERR return + + void init_psutil_posix(void) +#endif /* PY_MAJOR_VERSION */ { #if PY_MAJOR_VERSION >= 3 - PyObject *module = PyModule_Create(&moduledef); + PyObject *mod = PyModule_Create(&moduledef); #else - PyObject *module = Py_InitModule("_psutil_posix", PsutilMethods); + PyObject *mod = Py_InitModule("_psutil_posix", mod_methods); #endif + if (mod == NULL) + INITERR; #if defined(PSUTIL_BSD) || defined(PSUTIL_OSX) || defined(PSUTIL_SUNOS) || defined(PSUTIL_AIX) - PyModule_AddIntConstant(module, "AF_LINK", AF_LINK); + if (PyModule_AddIntConstant(mod, "AF_LINK", AF_LINK)) INITERR; #endif - if (module == NULL) - INITERROR; + if (mod == NULL) + INITERR; #if PY_MAJOR_VERSION >= 3 - return module; + return mod; #endif } |