summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2019-10-17 22:10:41 +0800
committerGiampaolo Rodola <g.rodola@gmail.com>2019-10-17 22:10:41 +0800
commitf37c97b64a81395a8f111bbb5f7d05053be97f71 (patch)
tree50e18ca6e9be906400fa0fb4445cab59cce354bc
parent3227ca99b9fa98d05da46f42a419bbe1be10f13b (diff)
downloadpsutil-f37c97b64a81395a8f111bbb5f7d05053be97f71.tar.gz
port _psutil_osx
-rw-r--r--psutil/_psutil_osx.c143
1 files changed, 70 insertions, 73 deletions
diff --git a/psutil/_psutil_osx.c b/psutil/_psutil_osx.c
index eaf4e514..6e5bd7b6 100644
--- a/psutil/_psutil_osx.c
+++ b/psutil/_psutil_osx.c
@@ -1745,8 +1745,7 @@ error:
/*
* define the psutil C module methods and initialize the module.
*/
-static PyMethodDef
-PsutilMethods[] = {
+static PyMethodDef mod_methods[] = {
// --- per-process functions
{"proc_kinfo_oneshot", psutil_proc_kinfo_oneshot, METH_VARARGS,
@@ -1816,95 +1815,93 @@ PsutilMethods[] = {
};
-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_osx_traverse(PyObject *m, visitproc visit, void *arg) {
- Py_VISIT(GETSTATE(m)->error);
- return 0;
-}
-
-static int
-psutil_osx_clear(PyObject *m) {
- Py_CLEAR(GETSTATE(m)->error);
- return 0;
-}
-
-
-static struct PyModuleDef moduledef = {
- PyModuleDef_HEAD_INIT,
- "psutil_osx",
- NULL,
- sizeof(struct module_state),
- PsutilMethods,
- NULL,
- psutil_osx_traverse,
- psutil_osx_clear,
- NULL
-};
-
-#define INITERROR return NULL
-
-PyMODINIT_FUNC PyInit__psutil_osx(void)
-
-#else
-#define INITERROR return
-
-void
-init_psutil_osx(void)
-#endif
+ #define INITERR return NULL
+
+ static struct PyModuleDef moduledef = {
+ PyModuleDef_HEAD_INIT,
+ "_psutil_osx",
+ NULL,
+ -1,
+ mod_methods,
+ NULL,
+ NULL,
+ NULL,
+ NULL
+ };
+
+ PyObject *PyInit__psutil_osx(void)
+#else /* PY_MAJOR_VERSION */
+ #define INITERR return
+
+ void init_psutil_osx(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_osx", PsutilMethods);
+ PyObject *mod = Py_InitModule("_psutil_osx", mod_methods);
#endif
- if (module == NULL)
- INITERROR;
+ if (mod == NULL)
+ INITERR;
if (psutil_setup() != 0)
- INITERROR;
+ INITERR;
- PyModule_AddIntConstant(module, "version", PSUTIL_VERSION);
+ if (PyModule_AddIntConstant(mod, "version", PSUTIL_VERSION))
+ INITERR;
// process status constants, defined in:
// http://fxr.watson.org/fxr/source/bsd/sys/proc.h?v=xnu-792.6.70#L149
- PyModule_AddIntConstant(module, "SIDL", SIDL);
- PyModule_AddIntConstant(module, "SRUN", SRUN);
- PyModule_AddIntConstant(module, "SSLEEP", SSLEEP);
- PyModule_AddIntConstant(module, "SSTOP", SSTOP);
- PyModule_AddIntConstant(module, "SZOMB", SZOMB);
+ if (PyModule_AddIntConstant(mod, "SIDL", SIDL))
+ INITERR;
+ if (PyModule_AddIntConstant(mod, "SRUN", SRUN))
+ INITERR;
+ if (PyModule_AddIntConstant(mod, "SSLEEP", SSLEEP))
+ INITERR;
+ if (PyModule_AddIntConstant(mod, "SSTOP", SSTOP))
+ INITERR;
+ if (PyModule_AddIntConstant(mod, "SZOMB", SZOMB))
+ INITERR;
// connection status constants
- PyModule_AddIntConstant(module, "TCPS_CLOSED", TCPS_CLOSED);
- PyModule_AddIntConstant(module, "TCPS_CLOSING", TCPS_CLOSING);
- PyModule_AddIntConstant(module, "TCPS_CLOSE_WAIT", TCPS_CLOSE_WAIT);
- PyModule_AddIntConstant(module, "TCPS_LISTEN", TCPS_LISTEN);
- PyModule_AddIntConstant(module, "TCPS_ESTABLISHED", TCPS_ESTABLISHED);
- PyModule_AddIntConstant(module, "TCPS_SYN_SENT", TCPS_SYN_SENT);
- PyModule_AddIntConstant(module, "TCPS_SYN_RECEIVED", TCPS_SYN_RECEIVED);
- PyModule_AddIntConstant(module, "TCPS_FIN_WAIT_1", TCPS_FIN_WAIT_1);
- PyModule_AddIntConstant(module, "TCPS_FIN_WAIT_2", TCPS_FIN_WAIT_2);
- PyModule_AddIntConstant(module, "TCPS_LAST_ACK", TCPS_LAST_ACK);
- PyModule_AddIntConstant(module, "TCPS_TIME_WAIT", TCPS_TIME_WAIT);
- PyModule_AddIntConstant(module, "PSUTIL_CONN_NONE", PSUTIL_CONN_NONE);
+ if (PyModule_AddIntConstant(mod, "TCPS_CLOSED", TCPS_CLOSED))
+ INITERR;
+ if (PyModule_AddIntConstant(mod, "TCPS_CLOSING", TCPS_CLOSING))
+ INITERR;
+ if (PyModule_AddIntConstant(mod, "TCPS_CLOSE_WAIT", TCPS_CLOSE_WAIT))
+ INITERR;
+ if (PyModule_AddIntConstant(mod, "TCPS_LISTEN", TCPS_LISTEN))
+ INITERR;
+ if (PyModule_AddIntConstant(mod, "TCPS_ESTABLISHED", TCPS_ESTABLISHED))
+ INITERR;
+ if (PyModule_AddIntConstant(mod, "TCPS_SYN_SENT", TCPS_SYN_SENT))
+ INITERR;
+ if (PyModule_AddIntConstant(mod, "TCPS_SYN_RECEIVED", TCPS_SYN_RECEIVED))
+ INITERR;
+ if (PyModule_AddIntConstant(mod, "TCPS_FIN_WAIT_1", TCPS_FIN_WAIT_1))
+ INITERR;
+ if (PyModule_AddIntConstant(mod, "TCPS_FIN_WAIT_2", TCPS_FIN_WAIT_2))
+ INITERR;
+ if (PyModule_AddIntConstant(mod, "TCPS_LAST_ACK", TCPS_LAST_ACK))
+ INITERR;
+ if (PyModule_AddIntConstant(mod, "TCPS_TIME_WAIT", TCPS_TIME_WAIT))
+ INITERR;
+ if (PyModule_AddIntConstant(mod, "PSUTIL_CONN_NONE", PSUTIL_CONN_NONE))
+ INITERR;
// Exception.
ZombieProcessError = PyErr_NewException(
"_psutil_osx.ZombieProcessError", NULL, NULL);
+ if (ZombieProcessError == NULL)
+ INITERR;
Py_INCREF(ZombieProcessError);
- PyModule_AddObject(module, "ZombieProcessError", ZombieProcessError);
+ if (PyModule_AddObject(mod, "ZombieProcessError", ZombieProcessError)) {
+ Py_DECREF(ZombieProcessError);
+ INITERR;
+ }
if (module == NULL)
- INITERROR;
+ INITERR;
#if PY_MAJOR_VERSION >= 3
return module;
#endif