summaryrefslogtreecommitdiff
path: root/Lib/os.py
diff options
context:
space:
mode:
authorGregory P. Smith <greg@mad-scientist.com>2010-02-27 07:22:22 +0000
committerGregory P. Smith <greg@mad-scientist.com>2010-02-27 07:22:22 +0000
commit06f5ad5c7345f42c5ab4be1d1ee350b14d489f65 (patch)
tree5b23c6b8a4ca59e5eebc10f292e54d4b9641273d /Lib/os.py
parented46453710e4eef8e6e2bb1f3a78277d22043333 (diff)
downloadcpython-06f5ad5c7345f42c5ab4be1d1ee350b14d489f65.tar.gz
Add an os.get_exec_path() function to return the list of directories
that launching a subprocess will search for the executable. Refactors some code in os._execvpe().
Diffstat (limited to 'Lib/os.py')
-rw-r--r--Lib/os.py27
1 files changed, 17 insertions, 10 deletions
diff --git a/Lib/os.py b/Lib/os.py
index 1c5b5ce2ea..580b9833cf 100644
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -342,28 +342,23 @@ __all__.extend(["execl","execle","execlp","execlpe","execvp","execvpe"])
def _execvpe(file, args, env=None):
if env is not None:
- func = execve
+ exec_func = execve
argrest = (args, env)
else:
- func = execv
+ exec_func = execv
argrest = (args,)
env = environ
head, tail = path.split(file)
if head:
- func(file, *argrest)
+ exec_func(file, *argrest)
return
- if 'PATH' in env:
- envpath = env['PATH']
- else:
- envpath = defpath
- PATH = envpath.split(pathsep)
last_exc = saved_exc = None
saved_tb = None
- for dir in PATH:
+ for dir in get_exec_path(env):
fullname = path.join(dir, file)
try:
- func(fullname, *argrest)
+ exec_func(fullname, *argrest)
except error as e:
last_exc = e
tb = sys.exc_info()[2]
@@ -376,6 +371,18 @@ def _execvpe(file, args, env=None):
raise last_exc.with_traceback(tb)
+def get_exec_path(env=None):
+ """Returns the sequence of directories that will be searched for the
+ named executable (similar to a shell) when launching a process.
+
+ *env* must be an environment variable dict or None. If *env* is None,
+ os.environ will be used.
+ """
+ if env is None:
+ env = environ
+ return env.get('PATH', defpath).split(pathsep)
+
+
# Change environ to automatically call putenv(), unsetenv if they exist.
from _abcoll import MutableMapping # Can't use collections (bootstrap)