blob: 15833238f16ec2ed46a246fb5e7bc84c070cf8a1 [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 Montanaro269b83b2001-02-06 01:07:02 +000030__all__ = []
31
32def _get_exports_list(module):
33 try:
34 return list(module.__all__)
35 except AttributeError:
36 return [n for n in dir(module) if n[0] != '_']
37
Guido van Rossuma28dab51997-08-29 22:36:47 +000038if 'posix' in _names:
Guido van Rossum61de0ac1997-12-05 21:24:30 +000039 name = 'posix'
Guido van Rossume9387ea1998-05-22 15:26:04 +000040 linesep = '\n'
Guido van Rossum61de0ac1997-12-05 21:24:30 +000041 curdir = '.'; pardir = '..'; sep = '/'; pathsep = ':'
42 defpath = ':/bin:/usr/bin'
43 from posix import *
44 try:
45 from posix import _exit
46 except ImportError:
47 pass
48 import posixpath
49 path = posixpath
50 del posixpath
Skip Montanaro269b83b2001-02-06 01:07:02 +000051
52 import posix
53 __all__.extend(_get_exports_list(posix))
54 del posix
55
Guido van Rossuma28dab51997-08-29 22:36:47 +000056elif 'nt' in _names:
Guido van Rossum61de0ac1997-12-05 21:24:30 +000057 name = 'nt'
Guido van Rossume9387ea1998-05-22 15:26:04 +000058 linesep = '\r\n'
Guido van Rossum61de0ac1997-12-05 21:24:30 +000059 curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';'
60 defpath = '.;C:\\bin'
61 from nt import *
Guido van Rossumfb801e71999-02-22 15:40:34 +000062 for i in ['_exit']:
Guido van Rossum67c65b21999-02-01 23:52:29 +000063 try:
64 exec "from nt import " + i
65 except ImportError:
66 pass
Guido van Rossum61de0ac1997-12-05 21:24:30 +000067 import ntpath
68 path = ntpath
69 del ntpath
Skip Montanaro269b83b2001-02-06 01:07:02 +000070
71 import nt
72 __all__.extend(_get_exports_list(nt))
73 del nt
74
Guido van Rossuma28dab51997-08-29 22:36:47 +000075elif 'dos' in _names:
Guido van Rossum61de0ac1997-12-05 21:24:30 +000076 name = 'dos'
Guido van Rossume9387ea1998-05-22 15:26:04 +000077 linesep = '\r\n'
Guido van Rossum61de0ac1997-12-05 21:24:30 +000078 curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';'
79 defpath = '.;C:\\bin'
80 from dos import *
81 try:
82 from dos import _exit
83 except ImportError:
84 pass
85 import dospath
86 path = dospath
87 del dospath
Skip Montanaro269b83b2001-02-06 01:07:02 +000088
89 import dos
90 __all__.extend(_get_exports_list(dos))
91 del dos
92
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000093elif 'os2' in _names:
Guido van Rossum61de0ac1997-12-05 21:24:30 +000094 name = 'os2'
Guido van Rossume9387ea1998-05-22 15:26:04 +000095 linesep = '\r\n'
Guido van Rossum61de0ac1997-12-05 21:24:30 +000096 curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';'
97 defpath = '.;C:\\bin'
98 from os2 import *
99 try:
100 from os2 import _exit
101 except ImportError:
102 pass
103 import ntpath
104 path = ntpath
105 del ntpath
Skip Montanaro269b83b2001-02-06 01:07:02 +0000106
107 import os2
108 __all__.extend(_get_exports_list(os2))
109 del os2
110
Guido van Rossuma28dab51997-08-29 22:36:47 +0000111elif 'mac' in _names:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000112 name = 'mac'
Guido van Rossume9387ea1998-05-22 15:26:04 +0000113 linesep = '\r'
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000114 curdir = ':'; pardir = '::'; sep = ':'; pathsep = '\n'
115 defpath = ':'
116 from mac import *
117 try:
118 from mac import _exit
119 except ImportError:
120 pass
121 import macpath
122 path = macpath
123 del macpath
Skip Montanaro269b83b2001-02-06 01:07:02 +0000124
125 import mac
126 __all__.extend(_get_exports_list(mac))
127 del mac
128
Guido van Rossum18df5d41999-06-11 01:37:27 +0000129elif 'ce' in _names:
130 name = 'ce'
131 linesep = '\r\n'
132 curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';'
133 defpath = '\\Windows'
134 from ce import *
135 for i in ['_exit']:
136 try:
137 exec "from ce import " + i
138 except ImportError:
139 pass
140 # We can use the standard Windows path.
141 import ntpath
142 path = ntpath
143 del ntpath
Skip Montanaro269b83b2001-02-06 01:07:02 +0000144
145 import ce
146 __all__.extend(_get_exports_list(ce))
147 del ce
148
Guido van Rossum2979b011994-08-01 11:18:30 +0000149else:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000150 raise ImportError, 'no os specific module found'
Guido van Rossume65cce51993-11-08 15:05:21 +0000151
Skip Montanaro269b83b2001-02-06 01:07:02 +0000152__all__.append("path")
153
Guido van Rossuma28dab51997-08-29 22:36:47 +0000154del _names
155
Fred Drake02379091999-01-19 16:05:13 +0000156sys.modules['os.path'] = path
157
Skip Montanaro269b83b2001-02-06 01:07:02 +0000158#'
159
Guido van Rossum4def7de1998-07-24 20:48:03 +0000160# Super directory utilities.
161# (Inspired by Eric Raymond; the doc strings are mostly his)
162
163def makedirs(name, mode=0777):
164 """makedirs(path [, mode=0777]) -> None
165
166 Super-mkdir; create a leaf directory and all intermediate ones.
167 Works like mkdir, except that any intermediate path segment (not
168 just the rightmost) will be created if it does not exist. This is
169 recursive.
170
171 """
172 head, tail = path.split(name)
Fred Drake9f2550f2000-07-25 15:16:40 +0000173 if not tail:
174 head, tail = path.split(head)
Guido van Rossum4def7de1998-07-24 20:48:03 +0000175 if head and tail and not path.exists(head):
176 makedirs(head, mode)
177 mkdir(name, mode)
178
179def removedirs(name):
180 """removedirs(path) -> None
181
182 Super-rmdir; remove a leaf directory and empty all intermediate
183 ones. Works like rmdir except that, if the leaf directory is
184 successfully removed, directories corresponding to rightmost path
185 segments will be pruned way until either the whole path is
186 consumed or an error occurs. Errors during this latter phase are
187 ignored -- they generally mean that a directory was not empty.
188
189 """
190 rmdir(name)
191 head, tail = path.split(name)
Fred Drake9f2550f2000-07-25 15:16:40 +0000192 if not tail:
193 head, tail = path.split(head)
Guido van Rossum4def7de1998-07-24 20:48:03 +0000194 while head and tail:
195 try:
196 rmdir(head)
197 except error:
198 break
199 head, tail = path.split(head)
200
201def renames(old, new):
202 """renames(old, new) -> None
203
204 Super-rename; create directories as necessary and delete any left
205 empty. Works like rename, except creation of any intermediate
206 directories needed to make the new pathname good is attempted
207 first. After the rename, directories corresponding to rightmost
208 path segments of the old name will be pruned way until either the
209 whole path is consumed or a nonempty directory is found.
210
211 Note: this function can fail with the new directory structure made
212 if you lack permissions needed to unlink the leaf directory or
213 file.
214
215 """
216 head, tail = path.split(new)
217 if head and tail and not path.exists(head):
218 makedirs(head)
219 rename(old, new)
220 head, tail = path.split(old)
221 if head and tail:
222 try:
223 removedirs(head)
224 except error:
225 pass
226
Skip Montanaro269b83b2001-02-06 01:07:02 +0000227__all__.extend(["makedirs", "removedirs", "renames"])
228
Guido van Rossuma28dab51997-08-29 22:36:47 +0000229# Make sure os.environ exists, at least
230try:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000231 environ
Guido van Rossuma28dab51997-08-29 22:36:47 +0000232except NameError:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000233 environ = {}
Guido van Rossuma28dab51997-08-29 22:36:47 +0000234
Guido van Rossume65cce51993-11-08 15:05:21 +0000235def execl(file, *args):
Guido van Rossum7da3cc52000-04-25 10:53:22 +0000236 """execl(file, *args)
237
238 Execute the executable file with argument list args, replacing the
239 current process. """
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000240 execv(file, args)
Guido van Rossume65cce51993-11-08 15:05:21 +0000241
242def execle(file, *args):
Guido van Rossum7da3cc52000-04-25 10:53:22 +0000243 """execle(file, *args, env)
244
245 Execute the executable file with argument list args and
246 environment env, replacing the current process. """
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000247 env = args[-1]
248 execve(file, args[:-1], env)
Guido van Rossume65cce51993-11-08 15:05:21 +0000249
250def execlp(file, *args):
Guido van Rossum7da3cc52000-04-25 10:53:22 +0000251 """execlp(file, *args)
252
253 Execute the executable file (which is searched for along $PATH)
254 with argument list args, replacing the current process. """
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000255 execvp(file, args)
Guido van Rossume65cce51993-11-08 15:05:21 +0000256
Guido van Rossum030afb11995-03-14 17:27:18 +0000257def execlpe(file, *args):
Guido van Rossum7da3cc52000-04-25 10:53:22 +0000258 """execlpe(file, *args, env)
259
260 Execute the executable file (which is searched for along $PATH)
261 with argument list args and environment env, replacing the current
Tim Peters2344fae2001-01-15 00:50:52 +0000262 process. """
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000263 env = args[-1]
264 execvpe(file, args[:-1], env)
Guido van Rossum030afb11995-03-14 17:27:18 +0000265
Guido van Rossume65cce51993-11-08 15:05:21 +0000266def execvp(file, args):
Guido van Rossum7da3cc52000-04-25 10:53:22 +0000267 """execp(file, args)
268
269 Execute the executable file (which is searched for along $PATH)
270 with argument list args, replacing the current process.
Thomas Wouters7e474022000-07-16 12:04:32 +0000271 args may be a list or tuple of strings. """
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000272 _execvpe(file, args)
Guido van Rossum030afb11995-03-14 17:27:18 +0000273
274def execvpe(file, args, env):
Guido van Rossum7da3cc52000-04-25 10:53:22 +0000275 """execv(file, args, env)
276
277 Execute the executable file (which is searched for along $PATH)
278 with argument list args and environment env , replacing the
279 current process.
Tim Peters2344fae2001-01-15 00:50:52 +0000280 args may be a list or tuple of strings. """
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000281 _execvpe(file, args, env)
Guido van Rossum030afb11995-03-14 17:27:18 +0000282
Skip Montanaro269b83b2001-02-06 01:07:02 +0000283__all__.extend(["execl","execle","execlp","execlpe","execvp","execvpe"])
284
Guido van Rossum030afb11995-03-14 17:27:18 +0000285_notfound = None
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000286def _execvpe(file, args, env=None):
287 if env is not None:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000288 func = execve
289 argrest = (args, env)
290 else:
291 func = execv
292 argrest = (args,)
293 env = environ
294 global _notfound
295 head, tail = path.split(file)
296 if head:
297 apply(func, (file,) + argrest)
298 return
299 if env.has_key('PATH'):
300 envpath = env['PATH']
301 else:
302 envpath = defpath
Guido van Rossum965fdae2000-04-04 19:50:04 +0000303 PATH = envpath.split(pathsep)
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000304 if not _notfound:
305 import tempfile
306 # Exec a file that is guaranteed not to exist
Guido van Rossum868b50a2000-04-26 20:32:08 +0000307 try: execv(tempfile.mktemp(), ('blah',))
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000308 except error, _notfound: pass
309 exc, arg = error, _notfound
310 for dir in PATH:
311 fullname = path.join(dir, file)
312 try:
313 apply(func, (fullname,) + argrest)
314 except error, (errno, msg):
315 if errno != arg[0]:
316 exc, arg = error, (errno, msg)
317 raise exc, arg
Guido van Rossum2979b011994-08-01 11:18:30 +0000318
Guido van Rossum3b8e20d1996-07-24 00:55:17 +0000319# Change environ to automatically call putenv() if it exists
320try:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000321 # This will fail if there's no putenv
322 putenv
Guido van Rossum3b8e20d1996-07-24 00:55:17 +0000323except NameError:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000324 pass
Guido van Rossuma28dab51997-08-29 22:36:47 +0000325else:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000326 import UserDict
Guido van Rossum3b8e20d1996-07-24 00:55:17 +0000327
Guido van Rossumda4d6da1998-08-04 16:01:23 +0000328 if name in ('os2', 'nt', 'dos'): # Where Env Var Names Must Be UPPERCASE
329 # But we store them as upper case
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000330 class _Environ(UserDict.UserDict):
331 def __init__(self, environ):
332 UserDict.UserDict.__init__(self)
Guido van Rossumda4d6da1998-08-04 16:01:23 +0000333 data = self.data
Guido van Rossumda4d6da1998-08-04 16:01:23 +0000334 for k, v in environ.items():
Guido van Rossum965fdae2000-04-04 19:50:04 +0000335 data[k.upper()] = v
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000336 def __setitem__(self, key, item):
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000337 putenv(key, item)
Guido van Rossum965fdae2000-04-04 19:50:04 +0000338 self.data[key.upper()] = item
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000339 def __getitem__(self, key):
Guido van Rossum965fdae2000-04-04 19:50:04 +0000340 return self.data[key.upper()]
341 def __delitem__(self, key):
342 del self.data[key.upper()]
Guido van Rossumb46413f1999-05-03 15:23:24 +0000343 def has_key(self, key):
Guido van Rossum965fdae2000-04-04 19:50:04 +0000344 return self.data.has_key(key.upper())
345 def get(self, key, failobj=None):
346 return self.data.get(key.upper(), failobj)
347 def update(self, dict):
348 for k, v in dict.items():
349 self[k] = v
Guido van Rossum3b8e20d1996-07-24 00:55:17 +0000350
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000351 else: # Where Env Var Names Can Be Mixed Case
352 class _Environ(UserDict.UserDict):
353 def __init__(self, environ):
354 UserDict.UserDict.__init__(self)
355 self.data = environ
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000356 def __setitem__(self, key, item):
357 putenv(key, item)
358 self.data[key] = item
Guido van Rossum965fdae2000-04-04 19:50:04 +0000359 def update(self, dict):
360 for k, v in dict.items():
361 self[k] = v
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000362
363 environ = _Environ(environ)
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000364
365def getenv(key, default=None):
366 """Get an environment variable, return None if it doesn't exist.
367
Fred Drake20af3172000-09-28 19:10:56 +0000368 The optional second argument can specify an alternate default."""
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000369 return environ.get(key, default)
Skip Montanaro269b83b2001-02-06 01:07:02 +0000370__all__.append("getenv")
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000371
372def _exists(name):
373 try:
374 eval(name)
375 return 1
376 except NameError:
377 return 0
378
379# Supply spawn*() (probably only for Unix)
380if _exists("fork") and not _exists("spawnv") and _exists("execv"):
381
382 P_WAIT = 0
383 P_NOWAIT = P_NOWAITO = 1
384
385 # XXX Should we support P_DETACH? I suppose it could fork()**2
386 # and close the std I/O streams. Also, P_OVERLAY is the same
387 # as execv*()?
388
389 def _spawnvef(mode, file, args, env, func):
390 # Internal helper; func is the exec*() function to use
391 pid = fork()
392 if not pid:
393 # Child
394 try:
395 if env is None:
396 func(file, args)
397 else:
398 func(file, args, env)
399 except:
400 _exit(127)
401 else:
402 # Parent
403 if mode == P_NOWAIT:
404 return pid # Caller is responsible for waiting!
405 while 1:
406 wpid, sts = waitpid(pid, 0)
407 if WIFSTOPPED(sts):
408 continue
409 elif WIFSIGNALED(sts):
410 return -WTERMSIG(sts)
411 elif WIFEXITED(sts):
412 return WEXITSTATUS(sts)
413 else:
414 raise error, "Not stopped, signaled or exited???"
415
416 def spawnv(mode, file, args):
Guido van Rossume0cd2912000-04-21 18:35:36 +0000417 """spawnv(mode, file, args) -> integer
418
419Execute file with arguments from args in a subprocess.
420If mode == P_NOWAIT return the pid of the process.
421If mode == P_WAIT return the process's exit code if it exits normally;
Tim Peters2344fae2001-01-15 00:50:52 +0000422otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000423 return _spawnvef(mode, file, args, None, execv)
424
425 def spawnve(mode, file, args, env):
Guido van Rossume0cd2912000-04-21 18:35:36 +0000426 """spawnve(mode, file, args, env) -> integer
427
428Execute file with arguments from args in a subprocess with the
429specified environment.
430If mode == P_NOWAIT return the pid of the process.
431If mode == P_WAIT return the process's exit code if it exits normally;
432otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000433 return _spawnvef(mode, file, args, env, execve)
434
Guido van Rossumdd7cbbf1999-11-02 20:44:07 +0000435 # Note: spawnvp[e] is't currently supported on Windows
436
437 def spawnvp(mode, file, args):
Guido van Rossume0cd2912000-04-21 18:35:36 +0000438 """spawnvp(mode, file, args) -> integer
439
440Execute file (which is looked for along $PATH) with arguments from
441args in a subprocess.
442If mode == P_NOWAIT return the pid of the process.
443If mode == P_WAIT return the process's exit code if it exits normally;
444otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossumdd7cbbf1999-11-02 20:44:07 +0000445 return _spawnvef(mode, file, args, None, execvp)
446
447 def spawnvpe(mode, file, args, env):
Guido van Rossume0cd2912000-04-21 18:35:36 +0000448 """spawnvpe(mode, file, args, env) -> integer
449
450Execute file (which is looked for along $PATH) with arguments from
451args in a subprocess with the supplied environment.
452If mode == P_NOWAIT return the pid of the process.
453If mode == P_WAIT return the process's exit code if it exits normally;
454otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossumdd7cbbf1999-11-02 20:44:07 +0000455 return _spawnvef(mode, file, args, env, execvpe)
456
457if _exists("spawnv"):
458 # These aren't supplied by the basic Windows code
459 # but can be easily implemented in Python
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000460
461 def spawnl(mode, file, *args):
Guido van Rossume0cd2912000-04-21 18:35:36 +0000462 """spawnl(mode, file, *args) -> integer
463
464Execute file with arguments from args in a subprocess.
465If mode == P_NOWAIT return the pid of the process.
466If mode == P_WAIT return the process's exit code if it exits normally;
467otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000468 return spawnv(mode, file, args)
469
470 def spawnle(mode, file, *args):
Guido van Rossume0cd2912000-04-21 18:35:36 +0000471 """spawnle(mode, file, *args, env) -> integer
472
473Execute file with arguments from args in a subprocess with the
474supplied environment.
475If mode == P_NOWAIT return the pid of the process.
476If mode == P_WAIT return the process's exit code if it exits normally;
477otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000478 env = args[-1]
479 return spawnve(mode, file, args[:-1], env)
480
Guido van Rossumdd7cbbf1999-11-02 20:44:07 +0000481if _exists("spawnvp"):
482 # At the moment, Windows doesn't implement spawnvp[e],
483 # so it won't have spawnlp[e] either.
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000484 def spawnlp(mode, file, *args):
Guido van Rossume0cd2912000-04-21 18:35:36 +0000485 """spawnlp(mode, file, *args, env) -> integer
486
487Execute file (which is looked for along $PATH) with arguments from
488args in a subprocess with the supplied environment.
489If mode == P_NOWAIT return the pid of the process.
490If mode == P_WAIT return the process's exit code if it exits normally;
491otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000492 return spawnvp(mode, file, args)
493
494 def spawnlpe(mode, file, *args):
Guido van Rossume0cd2912000-04-21 18:35:36 +0000495 """spawnlpe(mode, file, *args, env) -> integer
496
497Execute file (which is looked for along $PATH) with arguments from
498args in a subprocess with the supplied environment.
499If mode == P_NOWAIT return the pid of the process.
500If mode == P_WAIT return the process's exit code if it exits normally;
501otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000502 env = args[-1]
503 return spawnvpe(mode, file, args[:-1], env)
Guido van Rossume0cd2912000-04-21 18:35:36 +0000504
505
Skip Montanaro269b83b2001-02-06 01:07:02 +0000506 __all__.extend(["spawnlp","spawnlpe","spawnv", "spawnve","spawnvp",
507 "spawnvpe","spawnl","spawnle",])
508
509
Guido van Rossumd9a8e962000-09-19 03:04:52 +0000510# Supply popen2 etc. (for Unix)
511if _exists("fork"):
512 if not _exists("popen2"):
513 def popen2(cmd, mode="t", bufsize=-1):
Guido van Rossumd9a8e962000-09-19 03:04:52 +0000514 import popen2
515 stdout, stdin = popen2.popen2(cmd, bufsize)
516 return stdin, stdout
Skip Montanaro269b83b2001-02-06 01:07:02 +0000517 __all__.append("popen2")
Fred Drake31f182e2000-08-28 17:20:05 +0000518
Guido van Rossumd9a8e962000-09-19 03:04:52 +0000519 if not _exists("popen3"):
520 def popen3(cmd, mode="t", bufsize=-1):
Guido van Rossumd9a8e962000-09-19 03:04:52 +0000521 import popen2
522 stdout, stdin, stderr = popen2.popen3(cmd, bufsize)
523 return stdin, stdout, stderr
Skip Montanaro269b83b2001-02-06 01:07:02 +0000524 __all__.append("popen3")
Fred Drake20af3172000-09-28 19:10:56 +0000525
526 if not _exists("popen4"):
527 def popen4(cmd, mode="t", bufsize=-1):
528 import popen2
529 stdout, stdin = popen2.popen4(cmd, bufsize)
530 return stdin, stdout
Skip Montanaro269b83b2001-02-06 01:07:02 +0000531 __all__.append("popen4")