summaryrefslogtreecommitdiff
path: root/pexpect
diff options
context:
space:
mode:
authorMartin Packman <martin.packman@canonical.com>2016-05-13 18:12:25 +0000
committerMartin Packman <martin.packman@canonical.com>2016-05-13 18:34:09 +0000
commite5eb99c9bc9e19241c3d42e78fcc91d088b84251 (patch)
tree486821a4bc402b94261b50698b6fde6de5d016c6 /pexpect
parent3ed8d66ffb12863bc4a55a4b5dab7fca7ce9a9cc (diff)
downloadpexpect-e5eb99c9bc9e19241c3d42e78fcc91d088b84251.tar.gz
Use PATH from env argument to pexpect.spawn
Currently pexpect.spawn takes an env argument along the lines of subprocess.Popen but fails to use PATH from that env when finding the binary to run. The result when running a binary not on the PATH of the parent process is pexpect will raise an exception, or worse, find a different binary with the same name. This change passes the env from spawn into the which function so the correct PATH is searched and adds test coverage.
Diffstat (limited to 'pexpect')
-rw-r--r--pexpect/pty_spawn.py2
-rw-r--r--pexpect/utils.py9
2 files changed, 6 insertions, 5 deletions
diff --git a/pexpect/pty_spawn.py b/pexpect/pty_spawn.py
index 3518cc1..e5b5814 100644
--- a/pexpect/pty_spawn.py
+++ b/pexpect/pty_spawn.py
@@ -266,7 +266,7 @@ class spawn(SpawnBase):
self.args.insert(0, command)
self.command = command
- command_with_path = which(self.command)
+ command_with_path = which(self.command, env=self.env)
if command_with_path is None:
raise ExceptionPexpect('The command was not found or was not ' +
'executable: %s.' % self.command)
diff --git a/pexpect/utils.py b/pexpect/utils.py
index 737f0ed..c2763c4 100644
--- a/pexpect/utils.py
+++ b/pexpect/utils.py
@@ -31,7 +31,7 @@ def is_executable_file(path):
return os.access(fpath, os.X_OK)
-def which(filename):
+def which(filename, env=None):
'''This takes a given filename; tries to find it in the environment path;
then checks if it is executable. This returns the full path to the filename
if found and executable. Otherwise this returns None.'''
@@ -39,10 +39,11 @@ def which(filename):
# Special case where filename contains an explicit path.
if os.path.dirname(filename) != '' and is_executable_file(filename):
return filename
- if 'PATH' not in os.environ or os.environ['PATH'] == '':
+ if env is None:
+ env = os.environ
+ p = env.get('PATH')
+ if not p:
p = os.defpath
- else:
- p = os.environ['PATH']
pathlist = p.split(os.pathsep)
for path in pathlist:
ff = os.path.join(path, filename)