From 0662915f58949a4084f9fb0c278d143708cf3ede Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Sat, 11 Nov 2017 21:35:41 +0100 Subject: [PATCH] get rid of PSUTIL_TESTING env var: it must be necessarily set from cmdline, hence 'python -m psutil.tests' won't work out of the box --- psutil/_psutil_aix.c | 6 ++++-- psutil/_psutil_bsd.c | 6 ++++-- psutil/_psutil_common.c | 18 ++++++++++-------- psutil/_psutil_common.h | 3 ++- psutil/_psutil_linux.c | 6 ++++-- psutil/_psutil_osx.c | 6 ++++-- psutil/_psutil_sunos.c | 6 ++++-- psutil/_psutil_windows.c | 6 ++++-- psutil/tests/__init__.py | 4 ++-- 9 files changed, 38 insertions(+), 23 deletions(-) diff --git a/psutil/_psutil_aix.c b/psutil/_psutil_aix.c index 0834726dd..3b188b85e 100644 --- a/psutil/_psutil_aix.c +++ b/psutil/_psutil_aix.c @@ -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} }; diff --git a/psutil/_psutil_bsd.c b/psutil/_psutil_bsd.c index 3527b6667..7b0f140e9 100644 --- a/psutil/_psutil_bsd.c +++ b/psutil/_psutil_bsd.c @@ -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} }; diff --git a/psutil/_psutil_common.c b/psutil/_psutil_common.c index dace4724b..1fd2344ec 100644 --- a/psutil/_psutil_common.c +++ b/psutil/_psutil_common.c @@ -37,7 +37,7 @@ AccessDenied(void) { } -static int _psutil_testing = -1; +static int _psutil_testing = 0; /* @@ -45,12 +45,6 @@ static int _psutil_testing = -1; */ int psutil_testing(void) { - if (_psutil_testing == -1) { - if (getenv("PSUTIL_TESTING") != NULL) - _psutil_testing = 1; - else - _psutil_testing = 0; - } return _psutil_testing; } @@ -59,7 +53,7 @@ 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); @@ -67,6 +61,14 @@ py_psutil_testing(PyObject *self, PyObject *args) { } +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 diff --git a/psutil/_psutil_common.h b/psutil/_psutil_common.h index 134045327..09999bbaf 100644 --- a/psutil/_psutil_common.h +++ b/psutil/_psutil_common.h @@ -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); diff --git a/psutil/_psutil_linux.c b/psutil/_psutil_linux.c index a15ebe5cf..2391a67a4 100644 --- a/psutil/_psutil_linux.c +++ b/psutil/_psutil_linux.c @@ -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} }; diff --git a/psutil/_psutil_osx.c b/psutil/_psutil_osx.c index 2924aa399..9908d0332 100644 --- a/psutil/_psutil_osx.c +++ b/psutil/_psutil_osx.c @@ -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} }; diff --git a/psutil/_psutil_sunos.c b/psutil/_psutil_sunos.c index 083b78cb1..2abcd8295 100644 --- a/psutil/_psutil_sunos.c +++ b/psutil/_psutil_sunos.c @@ -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} }; diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 1d1fd939a..e1110b538 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -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} }; diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py index b94cfe6c6..9f943d7ad 100644 --- a/psutil/tests/__init__.py +++ b/psutil/tests/__init__.py @@ -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():