diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2017-11-12 03:14:12 +0100 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2017-11-12 03:14:12 +0100 |
commit | a8dd6ac992152dc84e0fdf87655355085f465c6f (patch) | |
tree | 524a0f713e86139aab5abdcceab919274b13fd0e | |
parent | 39c40cb155c0dfd463178dbeb26778f4c446c650 (diff) | |
download | psutil-a8dd6ac992152dc84e0fdf87655355085f465c6f.tar.gz |
use a C global variable to figure out whether we're in testing mode
-rw-r--r-- | psutil/_psutil_common.c | 25 | ||||
-rw-r--r-- | psutil/_psutil_common.h | 3 | ||||
-rw-r--r-- | psutil/arch/windows/process_info.c | 5 |
3 files changed, 13 insertions, 20 deletions
diff --git a/psutil/_psutil_common.c b/psutil/_psutil_common.c index 97b1494e..cf9899f1 100644 --- a/psutil/_psutil_common.c +++ b/psutil/_psutil_common.c @@ -10,6 +10,10 @@ #include <stdio.h> +// Global vars. +int PSUTIL_TESTING = 0; + + /* * Backport of unicode FS APIs from Python 3. * On Python 2 we just return a plain byte string @@ -58,27 +62,14 @@ AccessDenied(void) { } -static int _psutil_testing = 0; - - -/* - * Return 1 if PSUTIL_TESTING env var is set or if testing mode was - * enabled with psutil_set_testing. - */ -int -psutil_is_testing(void) { - return _psutil_testing; -} - - /* * Enable testing mode. This has the same effect as setting PSUTIL_TESTING - * env var. The dual method exists because updating os.environ on - * Windows has no effect. + * env var. This dual method exists because updating os.environ on + * Windows has no effect. Called on unit tests setup. */ PyObject * psutil_set_testing(PyObject *self, PyObject *args) { - _psutil_testing = 1; + PSUTIL_TESTING = 1; Py_INCREF(Py_None); return Py_None; } @@ -90,5 +81,5 @@ psutil_set_testing(PyObject *self, PyObject *args) { void psutil_setup(void) { if (getenv("PSUTIL_TESTING") != NULL) - _psutil_testing = 1; + PSUTIL_TESTING = 1; } diff --git a/psutil/_psutil_common.h b/psutil/_psutil_common.h index 2b71caa4..a609b886 100644 --- a/psutil/_psutil_common.h +++ b/psutil/_psutil_common.h @@ -6,6 +6,8 @@ #include <Python.h> +extern int PSUTIL_TESTING; + // a signaler for connections without an actual status static const int PSUTIL_CONN_NONE = 128; @@ -17,6 +19,5 @@ PyObject* PyUnicode_DecodeFSDefaultAndSize(char *s, Py_ssize_t size); PyObject* AccessDenied(void); PyObject* NoSuchProcess(void); -int psutil_is_testing(void); PyObject* psutil_set_testing(PyObject *self, PyObject *args); void psutil_setup(void); diff --git a/psutil/arch/windows/process_info.c b/psutil/arch/windows/process_info.c index c7410a18..92095fe5 100644 --- a/psutil/arch/windows/process_info.c +++ b/psutil/arch/windows/process_info.c @@ -337,7 +337,8 @@ psutil_get_pids(DWORD *numberOfReturnedPIDs) { int psutil_assert_pid_exists(DWORD pid, char *err) { - if (psutil_is_testing()) { + if (PSUTIL_TESTING) { + printf("testing\n"); if (psutil_pid_in_pids(pid) == 0) { PyErr_SetString(PyExc_AssertionError, err); return 0; @@ -349,7 +350,7 @@ psutil_assert_pid_exists(DWORD pid, char *err) { int psutil_assert_pid_not_exists(DWORD pid, char *err) { - if (psutil_is_testing()) { + if (PSUTIL_TESTING) { if (psutil_pid_in_pids(pid) == 1) { PyErr_SetString(PyExc_AssertionError, err); return 0; |