summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2017-11-23 18:24:53 +0100
committerGiampaolo Rodola <g.rodola@gmail.com>2017-11-23 18:24:53 +0100
commitd108baf4a27f0f3b115aae5e6109eaf109302bc3 (patch)
tree78960b0854084e74c931e8f7a54815c80a5118a2
parentecab2b3aa54ec5dbee9d7f7664ad4ed359048cd5 (diff)
downloadpsutil-d108baf4a27f0f3b115aae5e6109eaf109302bc3.tar.gz
be smarter in searching python exe
-rw-r--r--psutil/tests/__init__.py29
1 files changed, 22 insertions, 7 deletions
diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py
index 5b4f6f37..9e8d8596 100644
--- a/psutil/tests/__init__.py
+++ b/psutil/tests/__init__.py
@@ -168,13 +168,28 @@ HAS_SENSORS_TEMPERATURES = hasattr(psutil, "sensors_temperatures")
def _get_py_exe():
- exe = os.path.realpath(sys.executable)
- if not os.path.exists(exe):
- # It seems this only occurs on OSX.
- exe = which("python%s.%s" % sys.version_info[:2])
- if not exe or not os.path.exists(exe):
- ValueError("can't find python exe real abspath")
- return exe
+ def attempt(exe):
+ try:
+ subprocess.check_call(
+ [exe, "-V"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+ except Exception:
+ return None
+ else:
+ return exe
+
+ if OSX:
+ exe = \
+ attempt(sys.executable) or \
+ attempt(os.path.realpath(sys.executable)) or \
+ attempt(which("python%s.%s" % sys.version_info[:2])) or \
+ attempt(psutil.Process().exe())
+ if not exe:
+ raise ValueError("can't find python exe real abspath")
+ return exe
+ else:
+ exe = os.path.realpath(sys.executable)
+ assert os.path.exists(exe), exe
+ return exe
PYTHON_EXE = _get_py_exe()