diff options
Diffstat (limited to 'psutil/tests/__init__.py')
-rw-r--r-- | psutil/tests/__init__.py | 35 |
1 files changed, 21 insertions, 14 deletions
diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py index 61a06e21..e3766eea 100644 --- a/psutil/tests/__init__.py +++ b/psutil/tests/__init__.py @@ -41,7 +41,6 @@ from socket import SOCK_STREAM import psutil from psutil import AIX -from psutil import FREEBSD from psutil import LINUX from psutil import MACOS from psutil import POSIX @@ -80,8 +79,8 @@ if POSIX: __all__ = [ # constants 'APPVEYOR', 'DEVNULL', 'GLOBAL_TIMEOUT', 'TOLERANCE_SYS_MEM', 'NO_RETRIES', - 'PYPY', 'PYTHON_EXE', 'ROOT_DIR', 'SCRIPTS_DIR', 'TESTFN_PREFIX', - 'UNICODE_SUFFIX', 'INVALID_UNICODE_SUFFIX', + 'PYPY', 'PYTHON_EXE', 'PYTHON_EXE_ENV', 'ROOT_DIR', 'SCRIPTS_DIR', + 'TESTFN_PREFIX', 'UNICODE_SUFFIX', 'INVALID_UNICODE_SUFFIX', 'CI_TESTING', 'VALID_PROC_STATUSES', 'TOLERANCE_DISK_USAGE', 'IS_64BIT', "HAS_CPU_AFFINITY", "HAS_CPU_FREQ", "HAS_ENVIRON", "HAS_PROC_IO_COUNTERS", "HAS_IONICE", "HAS_MEMORY_MAPS", "HAS_PROC_CPU_NUM", "HAS_RLIMIT", @@ -240,13 +239,21 @@ def _get_py_exe(): else: return exe - if GITHUB_ACTIONS: - if PYPY: - return which("pypy3") if PY3 else which("pypy") - elif FREEBSD: - return os.path.realpath(sys.executable) - else: - return which('python') + env = os.environ.copy() + + # On Windows, starting with python 3.7, virtual environments use a + # venv launcher startup process. This does not play well when + # counting spawned processes, or when relying on the PID of the + # spawned process to do some checks, e.g. connections check per PID. + # Let's use the base python in this case. + base = getattr(sys, "_base_executable", None) + if WINDOWS and sys.version_info >= (3, 7) and base is not None: + # We need to set __PYVENV_LAUNCHER__ to sys.executable for the + # base python executable to know about the environment. + env["__PYVENV_LAUNCHER__"] = sys.executable + return base, env + elif GITHUB_ACTIONS: + return sys.executable, env elif MACOS: exe = \ attempt(sys.executable) or \ @@ -255,14 +262,14 @@ def _get_py_exe(): attempt(psutil.Process().exe()) if not exe: raise ValueError("can't find python exe real abspath") - return exe + return exe, env else: exe = os.path.realpath(sys.executable) assert os.path.exists(exe), exe - return exe + return exe, env -PYTHON_EXE = _get_py_exe() +PYTHON_EXE, PYTHON_EXE_ENV = _get_py_exe() DEVNULL = open(os.devnull, 'r+') atexit.register(DEVNULL.close) @@ -351,7 +358,7 @@ def spawn_testproc(cmd=None, **kwds): kwds.setdefault("stdin", DEVNULL) kwds.setdefault("stdout", DEVNULL) kwds.setdefault("cwd", os.getcwd()) - kwds.setdefault("env", os.environ) + kwds.setdefault("env", PYTHON_EXE_ENV) if WINDOWS: # Prevents the subprocess to open error dialogs. This will also # cause stderr to be suppressed, which is suboptimal in order |