blob: ddeb84c212ab8437806f8066b67c1febcea4677d [file] [log] [blame]
Guido van Rossum4b8c6ea2000-02-04 15:39:30 +00001"""OS routines for Mac, DOS, NT, or Posix depending on what system we're on.
Guido van Rossum31104f41992-01-14 18:28:36 +00002
Guido van Rossum54f22ed2000-02-04 15:10:34 +00003This exports:
Guido van Rossum4b8c6ea2000-02-04 15:39:30 +00004 - all functions from posix, nt, dos, os2, mac, or ce, e.g. unlink, stat, etc.
5 - os.path is one of the modules posixpath, ntpath, macpath, or dospath
6 - os.name is 'posix', 'nt', 'dos', 'os2', 'mac', or 'ce'
Guido van Rossum54f22ed2000-02-04 15:10:34 +00007 - os.curdir is a string representing the current directory ('.' or ':')
8 - os.pardir is a string representing the parent directory ('..' or '::')
9 - os.sep is the (or a most common) pathname separator ('/' or ':' or '\\')
Guido van Rossum4b8c6ea2000-02-04 15:39:30 +000010 - os.altsep is the alternate pathname separator (None or '/')
Guido van Rossum54f22ed2000-02-04 15:10:34 +000011 - os.pathsep is the component separator used in $PATH etc
Guido van Rossum4b8c6ea2000-02-04 15:39:30 +000012 - os.linesep is the line separator in text files ('\r' or '\n' or '\r\n')
Guido van Rossum54f22ed2000-02-04 15:10:34 +000013 - os.defpath is the default search path for executables
Guido van Rossum31104f41992-01-14 18:28:36 +000014
Guido van Rossum54f22ed2000-02-04 15:10:34 +000015Programs that import and use 'os' stand a better chance of being
16portable between different platforms. Of course, they must then
17only use functions that are defined by all platforms (e.g., unlink
18and opendir), and leave all pathname manipulation to os.path
19(e.g., split and join).
20"""
Guido van Rossum31104f41992-01-14 18:28:36 +000021
Skip Montanaro269b83b2001-02-06 01:07:02 +000022#'
23
Guido van Rossum2979b011994-08-01 11:18:30 +000024import sys
Guido van Rossuma28dab51997-08-29 22:36:47 +000025
26_names = sys.builtin_module_names
27
28altsep = None
29
Skip Montanaro6c0a0e12001-02-28 01:00:58 +000030__all__ = ["altsep", "curdir", "pardir", "sep", "pathsep", "linesep",
31 "defpath", "name"]
Skip Montanaro269b83b2001-02-06 01:07:02 +000032
33def _get_exports_list(module):
34 try:
35 return list(module.__all__)
36 except AttributeError:
37 return [n for n in dir(module) if n[0] != '_']
38
Guido van Rossuma28dab51997-08-29 22:36:47 +000039if 'posix' in _names:
Guido van Rossum61de0ac1997-12-05 21:24:30 +000040 name = 'posix'
Guido van Rossume9387ea1998-05-22 15:26:04 +000041 linesep = '\n'
Guido van Rossum61de0ac1997-12-05 21:24:30 +000042 curdir = '.'; pardir = '..'; sep = '/'; pathsep = ':'
43 defpath = ':/bin:/usr/bin'
44 from posix import *
45 try:
46 from posix import _exit
47 except ImportError:
48 pass
49 import posixpath
50 path = posixpath
51 del posixpath
Skip Montanaro269b83b2001-02-06 01:07:02 +000052
53 import posix
54 __all__.extend(_get_exports_list(posix))
55 del posix
56
Guido van Rossuma28dab51997-08-29 22:36:47 +000057elif 'nt' in _names:
Guido van Rossum61de0ac1997-12-05 21:24:30 +000058 name = 'nt'
Guido van Rossume9387ea1998-05-22 15:26:04 +000059 linesep = '\r\n'
Guido van Rossum61de0ac1997-12-05 21:24:30 +000060 curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';'
61 defpath = '.;C:\\bin'
62 from nt import *
Guido van Rossumfb801e71999-02-22 15:40:34 +000063 for i in ['_exit']:
Guido van Rossum67c65b21999-02-01 23:52:29 +000064 try:
65 exec "from nt import " + i
66 except ImportError:
67 pass
Guido van Rossum61de0ac1997-12-05 21:24:30 +000068 import ntpath
69 path = ntpath
70 del ntpath
Skip Montanaro269b83b2001-02-06 01:07:02 +000071
72 import nt
73 __all__.extend(_get_exports_list(nt))
74 del nt
75
Guido van Rossuma28dab51997-08-29 22:36:47 +000076elif 'dos' in _names:
Guido van Rossum61de0ac1997-12-05 21:24:30 +000077 name = 'dos'
Guido van Rossume9387ea1998-05-22 15:26:04 +000078 linesep = '\r\n'
Guido van Rossum61de0ac1997-12-05 21:24:30 +000079 curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';'
80 defpath = '.;C:\\bin'
81 from dos import *
82 try:
83 from dos import _exit
84 except ImportError:
85 pass
86 import dospath
87 path = dospath
88 del dospath
Skip Montanaro269b83b2001-02-06 01:07:02 +000089
90 import dos
91 __all__.extend(_get_exports_list(dos))
92 del dos
93
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000094elif 'os2' in _names:
Guido van Rossum61de0ac1997-12-05 21:24:30 +000095 name = 'os2'
Guido van Rossume9387ea1998-05-22 15:26:04 +000096 linesep = '\r\n'
Guido van Rossum61de0ac1997-12-05 21:24:30 +000097 curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';'
98 defpath = '.;C:\\bin'
99 from os2 import *
100 try:
101 from os2 import _exit
102 except ImportError:
103 pass
104 import ntpath
105 path = ntpath
106 del ntpath
Skip Montanaro269b83b2001-02-06 01:07:02 +0000107
108 import os2
109 __all__.extend(_get_exports_list(os2))
110 del os2
111
Guido van Rossuma28dab51997-08-29 22:36:47 +0000112elif 'mac' in _names:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000113 name = 'mac'
Guido van Rossume9387ea1998-05-22 15:26:04 +0000114 linesep = '\r'
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000115 curdir = ':'; pardir = '::'; sep = ':'; pathsep = '\n'
116 defpath = ':'
117 from mac import *
118 try:
119 from mac import _exit
120 except ImportError:
121 pass
122 import macpath
123 path = macpath
124 del macpath
Skip Montanaro269b83b2001-02-06 01:07:02 +0000125
126 import mac
127 __all__.extend(_get_exports_list(mac))
128 del mac
129
Guido van Rossum18df5d41999-06-11 01:37:27 +0000130elif 'ce' in _names:
131 name = 'ce'
132 linesep = '\r\n'
133 curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';'
134 defpath = '\\Windows'
135 from ce import *
136 for i in ['_exit']:
137 try:
138 exec "from ce import " + i
139 except ImportError:
140 pass
141 # We can use the standard Windows path.
142 import ntpath
143 path = ntpath
144 del ntpath
Skip Montanaro269b83b2001-02-06 01:07:02 +0000145
146 import ce
147 __all__.extend(_get_exports_list(ce))
148 del ce
149
Guido van Rossum2979b011994-08-01 11:18:30 +0000150else:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000151 raise ImportError, 'no os specific module found'
Guido van Rossume65cce51993-11-08 15:05:21 +0000152
Skip Montanaro269b83b2001-02-06 01:07:02 +0000153__all__.append("path")
154
Guido van Rossuma28dab51997-08-29 22:36:47 +0000155del _names
156
Fred Drake02379091999-01-19 16:05:13 +0000157sys.modules['os.path'] = path
158
Skip Montanaro269b83b2001-02-06 01:07:02 +0000159#'
160
Guido van Rossum4def7de1998-07-24 20:48:03 +0000161# Super directory utilities.
162# (Inspired by Eric Raymond; the doc strings are mostly his)
163
164def makedirs(name, mode=0777):
165 """makedirs(path [, mode=0777]) -> None
166
167 Super-mkdir; create a leaf directory and all intermediate ones.
168 Works like mkdir, except that any intermediate path segment (not
169 just the rightmost) will be created if it does not exist. This is
170 recursive.
171
172 """
173 head, tail = path.split(name)
Fred Drake9f2550f2000-07-25 15:16:40 +0000174 if not tail:
175 head, tail = path.split(head)
Guido van Rossum4def7de1998-07-24 20:48:03 +0000176 if head and tail and not path.exists(head):
177 makedirs(head, mode)
178 mkdir(name, mode)
179
180def removedirs(name):
181 """removedirs(path) -> None
182
183 Super-rmdir; remove a leaf directory and empty all intermediate
184 ones. Works like rmdir except that, if the leaf directory is
185 successfully removed, directories corresponding to rightmost path
186 segments will be pruned way until either the whole path is
187 consumed or an error occurs. Errors during this latter phase are
188 ignored -- they generally mean that a directory was not empty.
189
190 """
191 rmdir(name)
192 head, tail = path.split(name)
Fred Drake9f2550f2000-07-25 15:16:40 +0000193 if not tail:
194 head, tail = path.split(head)
Guido van Rossum4def7de1998-07-24 20:48:03 +0000195 while head and tail:
196 try:
197 rmdir(head)
198 except error:
199 break
200 head, tail = path.split(head)
201
202def renames(old, new):
203 """renames(old, new) -> None
204
205 Super-rename; create directories as necessary and delete any left
206 empty. Works like rename, except creation of any intermediate
207 directories needed to make the new pathname good is attempted
208 first. After the rename, directories corresponding to rightmost
209 path segments of the old name will be pruned way until either the
210 whole path is consumed or a nonempty directory is found.
211
212 Note: this function can fail with the new directory structure made
213 if you lack permissions needed to unlink the leaf directory or
214 file.
215
216 """
217 head, tail = path.split(new)
218 if head and tail and not path.exists(head):
219 makedirs(head)
220 rename(old, new)
221 head, tail = path.split(old)
222 if head and tail:
223 try:
224 removedirs(head)
225 except error:
226 pass
227
Skip Montanaro269b83b2001-02-06 01:07:02 +0000228__all__.extend(["makedirs", "removedirs", "renames"])
229
Guido van Rossuma28dab51997-08-29 22:36:47 +0000230# Make sure os.environ exists, at least
231try:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000232 environ
Guido van Rossuma28dab51997-08-29 22:36:47 +0000233except NameError:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000234 environ = {}
Guido van Rossuma28dab51997-08-29 22:36:47 +0000235
Guido van Rossume65cce51993-11-08 15:05:21 +0000236def execl(file, *args):
Guido van Rossum7da3cc52000-04-25 10:53:22 +0000237 """execl(file, *args)
238
239 Execute the executable file with argument list args, replacing the
240 current process. """
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000241 execv(file, args)
Guido van Rossume65cce51993-11-08 15:05:21 +0000242
243def execle(file, *args):
Guido van Rossum7da3cc52000-04-25 10:53:22 +0000244 """execle(file, *args, env)
245
246 Execute the executable file with argument list args and
247 environment env, replacing the current process. """
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000248 env = args[-1]
249 execve(file, args[:-1], env)
Guido van Rossume65cce51993-11-08 15:05:21 +0000250
251def execlp(file, *args):
Guido van Rossum7da3cc52000-04-25 10:53:22 +0000252 """execlp(file, *args)
253
254 Execute the executable file (which is searched for along $PATH)
255 with argument list args, replacing the current process. """
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000256 execvp(file, args)
Guido van Rossume65cce51993-11-08 15:05:21 +0000257
Guido van Rossum030afb11995-03-14 17:27:18 +0000258def execlpe(file, *args):
Guido van Rossum7da3cc52000-04-25 10:53:22 +0000259 """execlpe(file, *args, env)
260
261 Execute the executable file (which is searched for along $PATH)
262 with argument list args and environment env, replacing the current
Tim Peters2344fae2001-01-15 00:50:52 +0000263 process. """
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000264 env = args[-1]
265 execvpe(file, args[:-1], env)
Guido van Rossum030afb11995-03-14 17:27:18 +0000266
Guido van Rossume65cce51993-11-08 15:05:21 +0000267def execvp(file, args):
Guido van Rossum7da3cc52000-04-25 10:53:22 +0000268 """execp(file, args)
269
270 Execute the executable file (which is searched for along $PATH)
271 with argument list args, replacing the current process.
Thomas Wouters7e474022000-07-16 12:04:32 +0000272 args may be a list or tuple of strings. """
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000273 _execvpe(file, args)
Guido van Rossum030afb11995-03-14 17:27:18 +0000274
275def execvpe(file, args, env):
Guido van Rossum7da3cc52000-04-25 10:53:22 +0000276 """execv(file, args, env)
277
278 Execute the executable file (which is searched for along $PATH)
279 with argument list args and environment env , replacing the
280 current process.
Tim Peters2344fae2001-01-15 00:50:52 +0000281 args may be a list or tuple of strings. """
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000282 _execvpe(file, args, env)
Guido van Rossum030afb11995-03-14 17:27:18 +0000283
Skip Montanaro269b83b2001-02-06 01:07:02 +0000284__all__.extend(["execl","execle","execlp","execlpe","execvp","execvpe"])
285
Guido van Rossum030afb11995-03-14 17:27:18 +0000286_notfound = None
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000287def _execvpe(file, args, env=None):
288 if env is not None:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000289 func = execve
290 argrest = (args, env)
291 else:
292 func = execv
293 argrest = (args,)
294 env = environ
295 global _notfound
296 head, tail = path.split(file)
297 if head:
298 apply(func, (file,) + argrest)
299 return
300 if env.has_key('PATH'):
301 envpath = env['PATH']
302 else:
303 envpath = defpath
Guido van Rossum965fdae2000-04-04 19:50:04 +0000304 PATH = envpath.split(pathsep)
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000305 if not _notfound:
306 import tempfile
307 # Exec a file that is guaranteed not to exist
Guido van Rossum868b50a2000-04-26 20:32:08 +0000308 try: execv(tempfile.mktemp(), ('blah',))
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000309 except error, _notfound: pass
310 exc, arg = error, _notfound
311 for dir in PATH:
312 fullname = path.join(dir, file)
313 try:
314 apply(func, (fullname,) + argrest)
315 except error, (errno, msg):
316 if errno != arg[0]:
317 exc, arg = error, (errno, msg)
318 raise exc, arg
Guido van Rossum2979b011994-08-01 11:18:30 +0000319
Guido van Rossum3b8e20d1996-07-24 00:55:17 +0000320# Change environ to automatically call putenv() if it exists
321try:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000322 # This will fail if there's no putenv
323 putenv
Guido van Rossum3b8e20d1996-07-24 00:55:17 +0000324except NameError:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000325 pass
Guido van Rossuma28dab51997-08-29 22:36:47 +0000326else:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000327 import UserDict
Guido van Rossum3b8e20d1996-07-24 00:55:17 +0000328
Guido van Rossumda4d6da1998-08-04 16:01:23 +0000329 if name in ('os2', 'nt', 'dos'): # Where Env Var Names Must Be UPPERCASE
330 # But we store them as upper case
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000331 class _Environ(UserDict.UserDict):
332 def __init__(self, environ):
333 UserDict.UserDict.__init__(self)
Guido van Rossumda4d6da1998-08-04 16:01:23 +0000334 data = self.data
Guido van Rossumda4d6da1998-08-04 16:01:23 +0000335 for k, v in environ.items():
Guido van Rossum965fdae2000-04-04 19:50:04 +0000336 data[k.upper()] = v
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000337 def __setitem__(self, key, item):
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000338 putenv(key, item)
Guido van Rossum965fdae2000-04-04 19:50:04 +0000339 self.data[key.upper()] = item
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000340 def __getitem__(self, key):
Guido van Rossum965fdae2000-04-04 19:50:04 +0000341 return self.data[key.upper()]
342 def __delitem__(self, key):
343 del self.data[key.upper()]
Guido van Rossumb46413f1999-05-03 15:23:24 +0000344 def has_key(self, key):
Guido van Rossum965fdae2000-04-04 19:50:04 +0000345 return self.data.has_key(key.upper())
346 def get(self, key, failobj=None):
347 return self.data.get(key.upper(), failobj)
348 def update(self, dict):
349 for k, v in dict.items():
350 self[k] = v
Guido van Rossum3b8e20d1996-07-24 00:55:17 +0000351
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000352 else: # Where Env Var Names Can Be Mixed Case
353 class _Environ(UserDict.UserDict):
354 def __init__(self, environ):
355 UserDict.UserDict.__init__(self)
356 self.data = environ
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000357 def __setitem__(self, key, item):
358 putenv(key, item)
359 self.data[key] = item
Guido van Rossum965fdae2000-04-04 19:50:04 +0000360 def update(self, dict):
361 for k, v in dict.items():
362 self[k] = v
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000363
364 environ = _Environ(environ)
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000365
366def getenv(key, default=None):
367 """Get an environment variable, return None if it doesn't exist.
368
Fred Drake20af3172000-09-28 19:10:56 +0000369 The optional second argument can specify an alternate default."""
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000370 return environ.get(key, default)
Skip Montanaro269b83b2001-02-06 01:07:02 +0000371__all__.append("getenv")
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000372
373def _exists(name):
374 try:
375 eval(name)
376 return 1
377 except NameError:
378 return 0
379
380# Supply spawn*() (probably only for Unix)
381if _exists("fork") and not _exists("spawnv") and _exists("execv"):
382
383 P_WAIT = 0
384 P_NOWAIT = P_NOWAITO = 1
385
386 # XXX Should we support P_DETACH? I suppose it could fork()**2
387 # and close the std I/O streams. Also, P_OVERLAY is the same
388 # as execv*()?
389
390 def _spawnvef(mode, file, args, env, func):
391 # Internal helper; func is the exec*() function to use
392 pid = fork()
393 if not pid:
394 # Child
395 try:
396 if env is None:
397 func(file, args)
398 else:
399 func(file, args, env)
400 except:
401 _exit(127)
402 else:
403 # Parent
404 if mode == P_NOWAIT:
405 return pid # Caller is responsible for waiting!
406 while 1:
407 wpid, sts = waitpid(pid, 0)
408 if WIFSTOPPED(sts):
409 continue
410 elif WIFSIGNALED(sts):
411 return -WTERMSIG(sts)
412 elif WIFEXITED(sts):
413 return WEXITSTATUS(sts)
414 else:
415 raise error, "Not stopped, signaled or exited???"
416
417 def spawnv(mode, file, args):
Guido van Rossume0cd2912000-04-21 18:35:36 +0000418 """spawnv(mode, file, args) -> integer
419
420Execute file with arguments from args in a subprocess.
421If mode == P_NOWAIT return the pid of the process.
422If mode == P_WAIT return the process's exit code if it exits normally;
Tim Peters2344fae2001-01-15 00:50:52 +0000423otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000424 return _spawnvef(mode, file, args, None, execv)
425
426 def spawnve(mode, file, args, env):
Guido van Rossume0cd2912000-04-21 18:35:36 +0000427 """spawnve(mode, file, args, env) -> integer
428
429Execute file with arguments from args in a subprocess with the
430specified environment.
431If mode == P_NOWAIT return the pid of the process.
432If mode == P_WAIT return the process's exit code if it exits normally;
433otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000434 return _spawnvef(mode, file, args, env, execve)
435
Guido van Rossumdd7cbbf1999-11-02 20:44:07 +0000436 # Note: spawnvp[e] is't currently supported on Windows
437
438 def spawnvp(mode, file, args):
Guido van Rossume0cd2912000-04-21 18:35:36 +0000439 """spawnvp(mode, file, args) -> integer
440
441Execute file (which is looked for along $PATH) with arguments from
442args in a subprocess.
443If mode == P_NOWAIT return the pid of the process.
444If mode == P_WAIT return the process's exit code if it exits normally;
445otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossumdd7cbbf1999-11-02 20:44:07 +0000446 return _spawnvef(mode, file, args, None, execvp)
447
448 def spawnvpe(mode, file, args, env):
Guido van Rossume0cd2912000-04-21 18:35:36 +0000449 """spawnvpe(mode, file, args, env) -> integer
450
451Execute file (which is looked for along $PATH) with arguments from
452args in a subprocess with the supplied environment.
453If mode == P_NOWAIT return the pid of the process.
454If mode == P_WAIT return the process's exit code if it exits normally;
455otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossumdd7cbbf1999-11-02 20:44:07 +0000456 return _spawnvef(mode, file, args, env, execvpe)
457
458if _exists("spawnv"):
459 # These aren't supplied by the basic Windows code
460 # but can be easily implemented in Python
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000461
462 def spawnl(mode, file, *args):
Guido van Rossume0cd2912000-04-21 18:35:36 +0000463 """spawnl(mode, file, *args) -> integer
464
465Execute file with arguments from args in a subprocess.
466If mode == P_NOWAIT return the pid of the process.
467If mode == P_WAIT return the process's exit code if it exits normally;
468otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000469 return spawnv(mode, file, args)
470
471 def spawnle(mode, file, *args):
Guido van Rossume0cd2912000-04-21 18:35:36 +0000472 """spawnle(mode, file, *args, env) -> integer
473
474Execute file with arguments from args in a subprocess with the
475supplied environment.
476If mode == P_NOWAIT return the pid of the process.
477If mode == P_WAIT return the process's exit code if it exits normally;
478otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000479 env = args[-1]
480 return spawnve(mode, file, args[:-1], env)
481
Guido van Rossumdd7cbbf1999-11-02 20:44:07 +0000482if _exists("spawnvp"):
483 # At the moment, Windows doesn't implement spawnvp[e],
484 # so it won't have spawnlp[e] either.
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000485 def spawnlp(mode, file, *args):
Guido van Rossume0cd2912000-04-21 18:35:36 +0000486 """spawnlp(mode, file, *args, env) -> integer
487
488Execute file (which is looked for along $PATH) with arguments from
489args in a subprocess with the supplied environment.
490If mode == P_NOWAIT return the pid of the process.
491If mode == P_WAIT return the process's exit code if it exits normally;
492otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000493 return spawnvp(mode, file, args)
494
495 def spawnlpe(mode, file, *args):
Guido van Rossume0cd2912000-04-21 18:35:36 +0000496 """spawnlpe(mode, file, *args, env) -> integer
497
498Execute file (which is looked for along $PATH) with arguments from
499args in a subprocess with the supplied environment.
500If mode == P_NOWAIT return the pid of the process.
501If mode == P_WAIT return the process's exit code if it exits normally;
502otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000503 env = args[-1]
504 return spawnvpe(mode, file, args[:-1], env)
Guido van Rossume0cd2912000-04-21 18:35:36 +0000505
506
Skip Montanaro269b83b2001-02-06 01:07:02 +0000507 __all__.extend(["spawnlp","spawnlpe","spawnv", "spawnve","spawnvp",
508 "spawnvpe","spawnl","spawnle",])
509
510
Guido van Rossumd9a8e962000-09-19 03:04:52 +0000511# Supply popen2 etc. (for Unix)
512if _exists("fork"):
513 if not _exists("popen2"):
514 def popen2(cmd, mode="t", bufsize=-1):
Guido van Rossumd9a8e962000-09-19 03:04:52 +0000515 import popen2
516 stdout, stdin = popen2.popen2(cmd, bufsize)
517 return stdin, stdout
Skip Montanaro269b83b2001-02-06 01:07:02 +0000518 __all__.append("popen2")
Fred Drake31f182e2000-08-28 17:20:05 +0000519
Guido van Rossumd9a8e962000-09-19 03:04:52 +0000520 if not _exists("popen3"):
521 def popen3(cmd, mode="t", bufsize=-1):
Guido van Rossumd9a8e962000-09-19 03:04:52 +0000522 import popen2
523 stdout, stdin, stderr = popen2.popen3(cmd, bufsize)
524 return stdin, stdout, stderr
Skip Montanaro269b83b2001-02-06 01:07:02 +0000525 __all__.append("popen3")
Fred Drake20af3172000-09-28 19:10:56 +0000526
527 if not _exists("popen4"):
528 def popen4(cmd, mode="t", bufsize=-1):
529 import popen2
530 stdout, stdin = popen2.popen4(cmd, bufsize)
531 return stdin, stdout
Skip Montanaro269b83b2001-02-06 01:07:02 +0000532 __all__.append("popen4")