Skip to content

Commit

Permalink
port _psutil_osx
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Oct 17, 2019
1 parent 3227ca9 commit f37c97b
Showing 1 changed file with 70 additions and 73 deletions.
143 changes: 70 additions & 73 deletions psutil/_psutil_osx.c
Original file line number Diff line number Diff line change
Expand Up @@ -1745,8 +1745,7 @@ psutil_sensors_battery(PyObject *self, PyObject *args) {
/*
* 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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f37c97b

Please sign in to comment.