Skip to content

Commit

Permalink
get rid of PSUTIL_TESTING env var: it must be necessarily set from cm…
Browse files Browse the repository at this point in the history
…dline, hence 'python -m psutil.tests' won't work out of the box
  • Loading branch information
giampaolo committed Nov 11, 2017
1 parent ffde326 commit 0662915
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 23 deletions.
6 changes: 4 additions & 2 deletions psutil/_psutil_aix.c
Original file line number Diff line number Diff line change
Expand Up @@ -899,8 +899,10 @@ PsutilMethods[] =
"Return CPU statistics"},

// --- others
{"py_psutil_testing", py_psutil_testing, METH_VARARGS,
"Return True if PSUTIL_TESTING env var is set"},
{"py_psutil_is_testing", py_psutil_is_testing, METH_VARARGS,
"Return True if psutil is in testing mode"},
{"py_psutil_set_testing", py_psutil_set_testing, METH_VARARGS,
"Set psutil in testing mode"},

{NULL, NULL, 0, NULL}
};
Expand Down
6 changes: 4 additions & 2 deletions psutil/_psutil_bsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -984,8 +984,10 @@ PsutilMethods[] = {
#endif

// --- others
{"py_psutil_testing", py_psutil_testing, METH_VARARGS,
"Return True if PSUTIL_TESTING env var is set"},
{"py_psutil_is_testing", py_psutil_is_testing, METH_VARARGS,
"Return True if psutil is in testing mode"},
{"py_psutil_set_testing", py_psutil_set_testing, METH_VARARGS,
"Set psutil in testing mode"},

{NULL, NULL, 0, NULL}
};
Expand Down
18 changes: 10 additions & 8 deletions psutil/_psutil_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,14 @@ AccessDenied(void) {
}


static int _psutil_testing = -1;
static int _psutil_testing = 0;


/*
* Return 1 if PSUTIL_TESTING env var is set else 0.
*/
int
psutil_testing(void) {
if (_psutil_testing == -1) {
if (getenv("PSUTIL_TESTING") != NULL)
_psutil_testing = 1;
else
_psutil_testing = 0;
}
return _psutil_testing;
}

Expand All @@ -59,14 +53,22 @@ psutil_testing(void) {
* Return True if PSUTIL_TESTING env var is set else False.
*/
PyObject *
py_psutil_testing(PyObject *self, PyObject *args) {
py_psutil_is_testing(PyObject *self, PyObject *args) {
PyObject *res;
res = psutil_testing() ? Py_True : Py_False;
Py_INCREF(res);
return res;
}


PyObject *
py_psutil_set_testing(PyObject *self, PyObject *args) {
_psutil_testing = 1;
Py_INCREF(Py_None);
return Py_None;
}


/*
* Backport of unicode FS APIs from Python 3.
* On Python 2 we just return a plain byte string
Expand Down
3 changes: 2 additions & 1 deletion psutil/_psutil_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ static const int PSUTIL_CONN_NONE = 128;
PyObject* AccessDenied(void);
PyObject* NoSuchProcess(void);
int psutil_testing(void);
PyObject* py_psutil_testing(PyObject *self, PyObject *args);
PyObject* py_psutil_is_testing(PyObject *self, PyObject *args);
PyObject* py_psutil_set_testing(PyObject *self, PyObject *args);
#if PY_MAJOR_VERSION < 3
PyObject* PyUnicode_DecodeFSDefault(char *s);
PyObject* PyUnicode_DecodeFSDefaultAndSize(char *s, Py_ssize_t size);
Expand Down
6 changes: 4 additions & 2 deletions psutil/_psutil_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -607,8 +607,10 @@ PsutilMethods[] = {
#endif

// --- others
{"py_psutil_testing", py_psutil_testing, METH_VARARGS,
"Return True if PSUTIL_TESTING env var is set"},
{"py_psutil_is_testing", py_psutil_is_testing, METH_VARARGS,
"Return True if psutil is in testing mode"},
{"py_psutil_set_testing", py_psutil_set_testing, METH_VARARGS,
"Set psutil in testing mode"},

{NULL, NULL, 0, NULL}
};
Expand Down
6 changes: 4 additions & 2 deletions psutil/_psutil_osx.c
Original file line number Diff line number Diff line change
Expand Up @@ -1845,8 +1845,10 @@ PsutilMethods[] = {
"Return CPU statistics"},

// --- others
{"py_psutil_testing", py_psutil_testing, METH_VARARGS,
"Return True if PSUTIL_TESTING env var is set"},
{"py_psutil_is_testing", py_psutil_is_testing, METH_VARARGS,
"Return True if psutil is in testing mode"},
{"py_psutil_set_testing", py_psutil_set_testing, METH_VARARGS,
"Set psutil in testing mode"},

{NULL, NULL, 0, NULL}
};
Expand Down
6 changes: 4 additions & 2 deletions psutil/_psutil_sunos.c
Original file line number Diff line number Diff line change
Expand Up @@ -1592,8 +1592,10 @@ PsutilMethods[] = {
"Return CPU statistics"},

// --- others
{"py_psutil_testing", py_psutil_testing, METH_VARARGS,
"Return True if PSUTIL_TESTING env var is set"},
{"py_psutil_is_testing", py_psutil_is_testing, METH_VARARGS,
"Return True if psutil is in testing mode"},
{"py_psutil_set_testing", py_psutil_set_testing, METH_VARARGS,
"Set psutil in testing mode"},

{NULL, NULL, 0, NULL}
};
Expand Down
6 changes: 4 additions & 2 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -3624,8 +3624,10 @@ PsutilMethods[] = {
"QueryDosDevice binding"},

// --- others
{"py_psutil_testing", py_psutil_testing, METH_VARARGS,
"Return True if PSUTIL_TESTING env var is set"},
{"py_psutil_is_testing", py_psutil_is_testing, METH_VARARGS,
"Return True if psutil is in testing mode"},
{"py_psutil_set_testing", py_psutil_set_testing, METH_VARARGS,
"Set psutil in testing mode"},

{NULL, NULL, 0, NULL}
};
Expand Down
4 changes: 2 additions & 2 deletions psutil/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,8 +755,8 @@ def __str__(self):
def _setup_tests():
if 'PSUTIL_TESTING' not in os.environ:
os.environ['PSUTIL_TESTING'] = '1' # not guaranteed to work
if not psutil._psplatform.cext.py_psutil_testing():
raise AssertionError('PSUTIL_TESTING env var is not set')
psutil._psplatform.cext.py_psutil_set_testing()
assert psutil._psplatform.cext.py_psutil_is_testing()


def get_suite():
Expand Down

0 comments on commit 0662915

Please sign in to comment.