blob: ff6589d00e4a610a0dee4c5d40257e342291c9a3 [file] [log] [blame]
Ka-Ping Yeefeb67192001-03-02 23:31:431r"""OS routines for Mac, DOS, NT, or Posix depending on what system we're on.
Guido van Rossum31104f41992-01-14 18:28:362
Guido van Rossum54f22ed2000-02-04 15:10:343This exports:
Guido van Rossum4b8c6ea2000-02-04 15:39:304 - 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
Guido van Rossumd74fb6b2001-03-02 06:43:496 - os.name is 'posix', 'nt', 'dos', 'os2', 'mac', 'ce' or 'riscos'
Guido van Rossum54f22ed2000-02-04 15:10:347 - 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 Rossume2ae77b2001-10-24 20:42:5510 - os.extsep is the extension separator ('.' or '/')
Guido van Rossum4b8c6ea2000-02-04 15:39:3011 - os.altsep is the alternate pathname separator (None or '/')
Guido van Rossum54f22ed2000-02-04 15:10:3412 - os.pathsep is the component separator used in $PATH etc
Guido van Rossum4b8c6ea2000-02-04 15:39:3013 - os.linesep is the line separator in text files ('\r' or '\n' or '\r\n')
Guido van Rossum54f22ed2000-02-04 15:10:3414 - os.defpath is the default search path for executables
Guido van Rossum31104f41992-01-14 18:28:3615
Guido van Rossum54f22ed2000-02-04 15:10:3416Programs that import and use 'os' stand a better chance of being
17portable between different platforms. Of course, they must then
18only use functions that are defined by all platforms (e.g., unlink
19and opendir), and leave all pathname manipulation to os.path
20(e.g., split and join).
21"""
Guido van Rossum31104f41992-01-14 18:28:3622
Skip Montanaro269b83b2001-02-06 01:07:0223#'
24
Guido van Rossum2979b011994-08-01 11:18:3025import sys
Guido van Rossuma28dab51997-08-29 22:36:4726
27_names = sys.builtin_module_names
28
29altsep = None
30
Skip Montanaro6c0a0e12001-02-28 01:00:5831__all__ = ["altsep", "curdir", "pardir", "sep", "pathsep", "linesep",
32 "defpath", "name"]
Skip Montanaro269b83b2001-02-06 01:07:0233
34def _get_exports_list(module):
35 try:
36 return list(module.__all__)
37 except AttributeError:
38 return [n for n in dir(module) if n[0] != '_']
39
Guido van Rossuma28dab51997-08-29 22:36:4740if 'posix' in _names:
Guido van Rossum61de0ac1997-12-05 21:24:3041 name = 'posix'
Guido van Rossume9387ea1998-05-22 15:26:0442 linesep = '\n'
Guido van Rossum61de0ac1997-12-05 21:24:3043 curdir = '.'; pardir = '..'; sep = '/'; pathsep = ':'
44 defpath = ':/bin:/usr/bin'
45 from posix import *
46 try:
47 from posix import _exit
48 except ImportError:
49 pass
50 import posixpath
51 path = posixpath
52 del posixpath
Skip Montanaro269b83b2001-02-06 01:07:0253
54 import posix
55 __all__.extend(_get_exports_list(posix))
56 del posix
57
Guido van Rossuma28dab51997-08-29 22:36:4758elif 'nt' in _names:
Guido van Rossum61de0ac1997-12-05 21:24:3059 name = 'nt'
Guido van Rossume9387ea1998-05-22 15:26:0460 linesep = '\r\n'
Guido van Rossum61de0ac1997-12-05 21:24:3061 curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';'
62 defpath = '.;C:\\bin'
Skip Montanaro4c9fefc2003-03-28 22:31:4163 altsep = '/'
Guido van Rossum61de0ac1997-12-05 21:24:3064 from nt import *
Guido van Rossumfb801e71999-02-22 15:40:3465 for i in ['_exit']:
Guido van Rossum67c65b21999-02-01 23:52:2966 try:
67 exec "from nt import " + i
68 except ImportError:
69 pass
Guido van Rossum61de0ac1997-12-05 21:24:3070 import ntpath
71 path = ntpath
72 del ntpath
Skip Montanaro269b83b2001-02-06 01:07:0273
74 import nt
75 __all__.extend(_get_exports_list(nt))
76 del nt
77
Guido van Rossuma28dab51997-08-29 22:36:4778elif 'dos' in _names:
Guido van Rossum61de0ac1997-12-05 21:24:3079 name = 'dos'
Guido van Rossume9387ea1998-05-22 15:26:0480 linesep = '\r\n'
Guido van Rossum61de0ac1997-12-05 21:24:3081 curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';'
82 defpath = '.;C:\\bin'
83 from dos import *
84 try:
85 from dos import _exit
86 except ImportError:
87 pass
88 import dospath
89 path = dospath
90 del dospath
Skip Montanaro269b83b2001-02-06 01:07:0291
92 import dos
93 __all__.extend(_get_exports_list(dos))
94 del dos
95
Guido van Rossum8e9ebfd1997-11-22 21:53:4896elif 'os2' in _names:
Guido van Rossum61de0ac1997-12-05 21:24:3097 name = 'os2'
Guido van Rossume9387ea1998-05-22 15:26:0498 linesep = '\r\n'
Guido van Rossum61de0ac1997-12-05 21:24:3099 curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';'
100 defpath = '.;C:\\bin'
101 from os2 import *
102 try:
103 from os2 import _exit
104 except ImportError:
105 pass
106 import ntpath
107 path = ntpath
108 del ntpath
Skip Montanaro269b83b2001-02-06 01:07:02109
110 import os2
111 __all__.extend(_get_exports_list(os2))
112 del os2
113
Guido van Rossuma28dab51997-08-29 22:36:47114elif 'mac' in _names:
Guido van Rossum61de0ac1997-12-05 21:24:30115 name = 'mac'
Guido van Rossume9387ea1998-05-22 15:26:04116 linesep = '\r'
Guido van Rossum61de0ac1997-12-05 21:24:30117 curdir = ':'; pardir = '::'; sep = ':'; pathsep = '\n'
118 defpath = ':'
119 from mac import *
120 try:
121 from mac import _exit
122 except ImportError:
123 pass
124 import macpath
125 path = macpath
126 del macpath
Skip Montanaro269b83b2001-02-06 01:07:02127
128 import mac
129 __all__.extend(_get_exports_list(mac))
130 del mac
131
Guido van Rossum18df5d41999-06-11 01:37:27132elif 'ce' in _names:
133 name = 'ce'
134 linesep = '\r\n'
135 curdir = '.'; pardir = '..'; sep = '\\'; pathsep = ';'
136 defpath = '\\Windows'
137 from ce import *
138 for i in ['_exit']:
139 try:
140 exec "from ce import " + i
141 except ImportError:
142 pass
143 # We can use the standard Windows path.
144 import ntpath
145 path = ntpath
146 del ntpath
Skip Montanaro269b83b2001-02-06 01:07:02147
148 import ce
149 __all__.extend(_get_exports_list(ce))
150 del ce
151
Guido van Rossumd74fb6b2001-03-02 06:43:49152elif 'riscos' in _names:
153 name = 'riscos'
154 linesep = '\n'
155 curdir = '@'; pardir = '^'; sep = '.'; pathsep = ','
156 defpath = '<Run$Dir>'
157 from riscos import *
158 try:
159 from riscos import _exit
160 except ImportError:
161 pass
162 import riscospath
163 path = riscospath
164 del riscospath
Guido van Rossumd74fb6b2001-03-02 06:43:49165
166 import riscos
167 __all__.extend(_get_exports_list(riscos))
Skip Montanaro81e4b1c2001-03-06 15:26:07168 del riscos
Guido van Rossumd74fb6b2001-03-02 06:43:49169
Guido van Rossum2979b011994-08-01 11:18:30170else:
Guido van Rossum61de0ac1997-12-05 21:24:30171 raise ImportError, 'no os specific module found'
Guido van Rossume65cce51993-11-08 15:05:21172
Guido van Rossume2ae77b2001-10-24 20:42:55173
174if sep=='.':
175 extsep = '/'
176else:
177 extsep = '.'
178
Skip Montanaro269b83b2001-02-06 01:07:02179__all__.append("path")
180
Guido van Rossuma28dab51997-08-29 22:36:47181del _names
182
Fred Drake02379091999-01-19 16:05:13183sys.modules['os.path'] = path
184
Skip Montanaro269b83b2001-02-06 01:07:02185#'
186
Guido van Rossum4def7de1998-07-24 20:48:03187# Super directory utilities.
188# (Inspired by Eric Raymond; the doc strings are mostly his)
189
190def makedirs(name, mode=0777):
191 """makedirs(path [, mode=0777]) -> None
192
193 Super-mkdir; create a leaf directory and all intermediate ones.
194 Works like mkdir, except that any intermediate path segment (not
195 just the rightmost) will be created if it does not exist. This is
196 recursive.
197
198 """
199 head, tail = path.split(name)
Fred Drake9f2550f2000-07-25 15:16:40200 if not tail:
201 head, tail = path.split(head)
Guido van Rossum4def7de1998-07-24 20:48:03202 if head and tail and not path.exists(head):
203 makedirs(head, mode)
204 mkdir(name, mode)
205
206def removedirs(name):
207 """removedirs(path) -> None
208
209 Super-rmdir; remove a leaf directory and empty all intermediate
210 ones. Works like rmdir except that, if the leaf directory is
211 successfully removed, directories corresponding to rightmost path
212 segments will be pruned way until either the whole path is
213 consumed or an error occurs. Errors during this latter phase are
214 ignored -- they generally mean that a directory was not empty.
215
216 """
217 rmdir(name)
218 head, tail = path.split(name)
Fred Drake9f2550f2000-07-25 15:16:40219 if not tail:
220 head, tail = path.split(head)
Guido van Rossum4def7de1998-07-24 20:48:03221 while head and tail:
222 try:
223 rmdir(head)
224 except error:
225 break
226 head, tail = path.split(head)
227
228def renames(old, new):
229 """renames(old, new) -> None
230
231 Super-rename; create directories as necessary and delete any left
232 empty. Works like rename, except creation of any intermediate
233 directories needed to make the new pathname good is attempted
234 first. After the rename, directories corresponding to rightmost
235 path segments of the old name will be pruned way until either the
236 whole path is consumed or a nonempty directory is found.
237
238 Note: this function can fail with the new directory structure made
239 if you lack permissions needed to unlink the leaf directory or
240 file.
241
242 """
243 head, tail = path.split(new)
244 if head and tail and not path.exists(head):
245 makedirs(head)
246 rename(old, new)
247 head, tail = path.split(old)
248 if head and tail:
249 try:
250 removedirs(head)
251 except error:
252 pass
253
Skip Montanaro269b83b2001-02-06 01:07:02254__all__.extend(["makedirs", "removedirs", "renames"])
255
Guido van Rossuma28dab51997-08-29 22:36:47256# Make sure os.environ exists, at least
257try:
Guido van Rossum61de0ac1997-12-05 21:24:30258 environ
Guido van Rossuma28dab51997-08-29 22:36:47259except NameError:
Guido van Rossum61de0ac1997-12-05 21:24:30260 environ = {}
Guido van Rossuma28dab51997-08-29 22:36:47261
Guido van Rossume65cce51993-11-08 15:05:21262def execl(file, *args):
Guido van Rossum7da3cc52000-04-25 10:53:22263 """execl(file, *args)
264
265 Execute the executable file with argument list args, replacing the
266 current process. """
Guido van Rossum61de0ac1997-12-05 21:24:30267 execv(file, args)
Guido van Rossume65cce51993-11-08 15:05:21268
269def execle(file, *args):
Guido van Rossum7da3cc52000-04-25 10:53:22270 """execle(file, *args, env)
271
272 Execute the executable file with argument list args and
273 environment env, replacing the current process. """
Guido van Rossum61de0ac1997-12-05 21:24:30274 env = args[-1]
275 execve(file, args[:-1], env)
Guido van Rossume65cce51993-11-08 15:05:21276
277def execlp(file, *args):
Guido van Rossum7da3cc52000-04-25 10:53:22278 """execlp(file, *args)
279
280 Execute the executable file (which is searched for along $PATH)
281 with argument list args, replacing the current process. """
Guido van Rossum61de0ac1997-12-05 21:24:30282 execvp(file, args)
Guido van Rossume65cce51993-11-08 15:05:21283
Guido van Rossum030afb11995-03-14 17:27:18284def execlpe(file, *args):
Guido van Rossum7da3cc52000-04-25 10:53:22285 """execlpe(file, *args, env)
286
287 Execute the executable file (which is searched for along $PATH)
288 with argument list args and environment env, replacing the current
Tim Peters2344fae2001-01-15 00:50:52289 process. """
Guido van Rossum61de0ac1997-12-05 21:24:30290 env = args[-1]
291 execvpe(file, args[:-1], env)
Guido van Rossum030afb11995-03-14 17:27:18292
Guido van Rossume65cce51993-11-08 15:05:21293def execvp(file, args):
Guido van Rossum7da3cc52000-04-25 10:53:22294 """execp(file, args)
295
296 Execute the executable file (which is searched for along $PATH)
297 with argument list args, replacing the current process.
Thomas Wouters7e474022000-07-16 12:04:32298 args may be a list or tuple of strings. """
Guido van Rossum61de0ac1997-12-05 21:24:30299 _execvpe(file, args)
Guido van Rossum030afb11995-03-14 17:27:18300
301def execvpe(file, args, env):
Guido van Rossum10c7ab72002-09-03 16:36:59302 """execvpe(file, args, env)
Guido van Rossum7da3cc52000-04-25 10:53:22303
304 Execute the executable file (which is searched for along $PATH)
305 with argument list args and environment env , replacing the
306 current process.
Tim Peters2344fae2001-01-15 00:50:52307 args may be a list or tuple of strings. """
Guido van Rossum61de0ac1997-12-05 21:24:30308 _execvpe(file, args, env)
Guido van Rossum030afb11995-03-14 17:27:18309
Skip Montanaro269b83b2001-02-06 01:07:02310__all__.extend(["execl","execle","execlp","execlpe","execvp","execvpe"])
311
Guido van Rossum5a2ca931999-11-02 13:27:32312def _execvpe(file, args, env=None):
Guido van Rossumf1045ad2002-08-08 19:35:53313 from errno import ENOENT, ENOTDIR
314
Guido van Rossum5a2ca931999-11-02 13:27:32315 if env is not None:
Guido van Rossum61de0ac1997-12-05 21:24:30316 func = execve
317 argrest = (args, env)
318 else:
319 func = execv
320 argrest = (args,)
321 env = environ
Guido van Rossumf1045ad2002-08-08 19:35:53322
Guido van Rossum61de0ac1997-12-05 21:24:30323 head, tail = path.split(file)
324 if head:
325 apply(func, (file,) + argrest)
326 return
327 if env.has_key('PATH'):
328 envpath = env['PATH']
329 else:
330 envpath = defpath
Guido van Rossum965fdae2000-04-04 19:50:04331 PATH = envpath.split(pathsep)
Guido van Rossum10c7ab72002-09-03 16:36:59332 saved_exc = None
333 saved_tb = None
Guido van Rossum61de0ac1997-12-05 21:24:30334 for dir in PATH:
335 fullname = path.join(dir, file)
336 try:
337 apply(func, (fullname,) + argrest)
Guido van Rossum10c7ab72002-09-03 16:36:59338 except error, e:
339 tb = sys.exc_info()[2]
340 if (e.errno != ENOENT and e.errno != ENOTDIR
341 and saved_exc is None):
342 saved_exc = e
343 saved_tb = tb
344 if saved_exc:
345 raise error, saved_exc, saved_tb
346 raise error, e, tb
Guido van Rossumd74fb6b2001-03-02 06:43:49347
Martin v. Löwisa90f4382001-03-07 09:05:45348# Change environ to automatically call putenv() if it exists
349try:
350 # This will fail if there's no putenv
351 putenv
352except NameError:
353 pass
354else:
355 import UserDict
Guido van Rossum3b8e20d1996-07-24 00:55:17356
Guido van Rossumc524d952001-10-19 01:31:59357 # Fake unsetenv() for Windows
Tim Peters1633a2e2001-10-30 05:56:40358 # not sure about os2 and dos here but
Guido van Rossumc524d952001-10-19 01:31:59359 # I'm guessing they are the same.
360
361 if name in ('os2', 'nt', 'dos'):
362 def unsetenv(key):
363 putenv(key, "")
364
Martin v. Löwisa90f4382001-03-07 09:05:45365 if name == "riscos":
366 # On RISC OS, all env access goes through getenv and putenv
367 from riscosenviron import _Environ
368 elif name in ('os2', 'nt', 'dos'): # Where Env Var Names Must Be UPPERCASE
Guido van Rossumda4d6da1998-08-04 16:01:23369 # But we store them as upper case
Raymond Hettingeraa36d872002-09-06 19:35:22370 class _Environ(UserDict.IterableUserDict):
Guido van Rossum61de0ac1997-12-05 21:24:30371 def __init__(self, environ):
372 UserDict.UserDict.__init__(self)
Guido van Rossumda4d6da1998-08-04 16:01:23373 data = self.data
Guido van Rossumda4d6da1998-08-04 16:01:23374 for k, v in environ.items():
Guido van Rossum965fdae2000-04-04 19:50:04375 data[k.upper()] = v
Guido van Rossum61de0ac1997-12-05 21:24:30376 def __setitem__(self, key, item):
Guido van Rossum61de0ac1997-12-05 21:24:30377 putenv(key, item)
Guido van Rossum965fdae2000-04-04 19:50:04378 self.data[key.upper()] = item
Guido van Rossum61de0ac1997-12-05 21:24:30379 def __getitem__(self, key):
Guido van Rossum965fdae2000-04-04 19:50:04380 return self.data[key.upper()]
Guido van Rossumc524d952001-10-19 01:31:59381 try:
382 unsetenv
383 except NameError:
384 def __delitem__(self, key):
385 del self.data[key.upper()]
386 else:
387 def __delitem__(self, key):
388 unsetenv(key)
389 del self.data[key.upper()]
Guido van Rossumb46413f1999-05-03 15:23:24390 def has_key(self, key):
Guido van Rossum965fdae2000-04-04 19:50:04391 return self.data.has_key(key.upper())
392 def get(self, key, failobj=None):
393 return self.data.get(key.upper(), failobj)
394 def update(self, dict):
395 for k, v in dict.items():
396 self[k] = v
Guido van Rossum3b8e20d1996-07-24 00:55:17397
Guido van Rossum61de0ac1997-12-05 21:24:30398 else: # Where Env Var Names Can Be Mixed Case
Raymond Hettinger9c1d3e42002-09-07 04:49:09399 class _Environ(UserDict.IterableUserDict):
Guido van Rossum61de0ac1997-12-05 21:24:30400 def __init__(self, environ):
401 UserDict.UserDict.__init__(self)
402 self.data = environ
Guido van Rossum61de0ac1997-12-05 21:24:30403 def __setitem__(self, key, item):
404 putenv(key, item)
405 self.data[key] = item
Guido van Rossum965fdae2000-04-04 19:50:04406 def update(self, dict):
407 for k, v in dict.items():
408 self[k] = v
Guido van Rossumc524d952001-10-19 01:31:59409 try:
410 unsetenv
411 except NameError:
412 pass
413 else:
414 def __delitem__(self, key):
415 unsetenv(key)
416 del self.data[key]
Tim Peters1633a2e2001-10-30 05:56:40417
Guido van Rossum61de0ac1997-12-05 21:24:30418
419 environ = _Environ(environ)
Guido van Rossum5a2ca931999-11-02 13:27:32420
Guido van Rossumd74fb6b2001-03-02 06:43:49421 def getenv(key, default=None):
422 """Get an environment variable, return None if it doesn't exist.
423 The optional second argument can specify an alternate default."""
424 return environ.get(key, default)
425 __all__.append("getenv")
Guido van Rossum5a2ca931999-11-02 13:27:32426
427def _exists(name):
428 try:
429 eval(name)
430 return 1
431 except NameError:
432 return 0
433
434# Supply spawn*() (probably only for Unix)
435if _exists("fork") and not _exists("spawnv") and _exists("execv"):
436
437 P_WAIT = 0
438 P_NOWAIT = P_NOWAITO = 1
439
440 # XXX Should we support P_DETACH? I suppose it could fork()**2
441 # and close the std I/O streams. Also, P_OVERLAY is the same
442 # as execv*()?
443
444 def _spawnvef(mode, file, args, env, func):
445 # Internal helper; func is the exec*() function to use
446 pid = fork()
447 if not pid:
448 # Child
449 try:
450 if env is None:
451 func(file, args)
452 else:
453 func(file, args, env)
454 except:
455 _exit(127)
456 else:
457 # Parent
458 if mode == P_NOWAIT:
459 return pid # Caller is responsible for waiting!
460 while 1:
461 wpid, sts = waitpid(pid, 0)
462 if WIFSTOPPED(sts):
463 continue
464 elif WIFSIGNALED(sts):
465 return -WTERMSIG(sts)
466 elif WIFEXITED(sts):
467 return WEXITSTATUS(sts)
468 else:
469 raise error, "Not stopped, signaled or exited???"
470
471 def spawnv(mode, file, args):
Guido van Rossume0cd2912000-04-21 18:35:36472 """spawnv(mode, file, args) -> integer
473
474Execute file with arguments from args in a subprocess.
475If mode == P_NOWAIT return the pid of the process.
476If mode == P_WAIT return the process's exit code if it exits normally;
Tim Peters2344fae2001-01-15 00:50:52477otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossum5a2ca931999-11-02 13:27:32478 return _spawnvef(mode, file, args, None, execv)
479
480 def spawnve(mode, file, args, env):
Guido van Rossume0cd2912000-04-21 18:35:36481 """spawnve(mode, file, args, env) -> integer
482
483Execute file with arguments from args in a subprocess with the
484specified environment.
485If mode == P_NOWAIT return the pid of the process.
486If mode == P_WAIT return the process's exit code if it exits normally;
487otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossum5a2ca931999-11-02 13:27:32488 return _spawnvef(mode, file, args, env, execve)
489
Guido van Rossumdd7cbbf1999-11-02 20:44:07490 # Note: spawnvp[e] is't currently supported on Windows
491
492 def spawnvp(mode, file, args):
Guido van Rossume0cd2912000-04-21 18:35:36493 """spawnvp(mode, file, args) -> integer
494
495Execute file (which is looked for along $PATH) with arguments from
496args in a subprocess.
497If mode == P_NOWAIT return the pid of the process.
498If mode == P_WAIT return the process's exit code if it exits normally;
499otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossumdd7cbbf1999-11-02 20:44:07500 return _spawnvef(mode, file, args, None, execvp)
501
502 def spawnvpe(mode, file, args, env):
Guido van Rossume0cd2912000-04-21 18:35:36503 """spawnvpe(mode, file, args, env) -> integer
504
505Execute file (which is looked for along $PATH) with arguments from
506args in a subprocess with the supplied environment.
507If mode == P_NOWAIT return the pid of the process.
508If mode == P_WAIT return the process's exit code if it exits normally;
509otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossumdd7cbbf1999-11-02 20:44:07510 return _spawnvef(mode, file, args, env, execvpe)
511
512if _exists("spawnv"):
513 # These aren't supplied by the basic Windows code
514 # but can be easily implemented in Python
Guido van Rossum5a2ca931999-11-02 13:27:32515
516 def spawnl(mode, file, *args):
Guido van Rossume0cd2912000-04-21 18:35:36517 """spawnl(mode, file, *args) -> integer
518
519Execute file with arguments from args in a subprocess.
520If mode == P_NOWAIT return the pid of the process.
521If mode == P_WAIT return the process's exit code if it exits normally;
522otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossum5a2ca931999-11-02 13:27:32523 return spawnv(mode, file, args)
524
525 def spawnle(mode, file, *args):
Guido van Rossume0cd2912000-04-21 18:35:36526 """spawnle(mode, file, *args, env) -> integer
527
528Execute file with arguments from args in a subprocess with the
529supplied environment.
530If mode == P_NOWAIT return the pid of the process.
531If mode == P_WAIT return the process's exit code if it exits normally;
532otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossum5a2ca931999-11-02 13:27:32533 env = args[-1]
534 return spawnve(mode, file, args[:-1], env)
535
Guido van Rossumdd7cbbf1999-11-02 20:44:07536if _exists("spawnvp"):
537 # At the moment, Windows doesn't implement spawnvp[e],
538 # so it won't have spawnlp[e] either.
Guido van Rossum5a2ca931999-11-02 13:27:32539 def spawnlp(mode, file, *args):
Neal Norwitz0a6f7da2003-07-02 02:51:15540 """spawnlp(mode, file, *args) -> integer
Guido van Rossume0cd2912000-04-21 18:35:36541
542Execute file (which is looked for along $PATH) with arguments from
543args in a subprocess with the supplied environment.
544If mode == P_NOWAIT return the pid of the process.
545If mode == P_WAIT return the process's exit code if it exits normally;
546otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossum5a2ca931999-11-02 13:27:32547 return spawnvp(mode, file, args)
548
549 def spawnlpe(mode, file, *args):
Guido van Rossume0cd2912000-04-21 18:35:36550 """spawnlpe(mode, file, *args, env) -> integer
551
552Execute file (which is looked for along $PATH) with arguments from
553args in a subprocess with the supplied environment.
554If mode == P_NOWAIT return the pid of the process.
555If mode == P_WAIT return the process's exit code if it exits normally;
556otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossum5a2ca931999-11-02 13:27:32557 env = args[-1]
558 return spawnvpe(mode, file, args[:-1], env)
Guido van Rossume0cd2912000-04-21 18:35:36559
560
Skip Montanaro269b83b2001-02-06 01:07:02561 __all__.extend(["spawnlp","spawnlpe","spawnv", "spawnve","spawnvp",
562 "spawnvpe","spawnl","spawnle",])
563
564
Guido van Rossumd9a8e962000-09-19 03:04:52565# Supply popen2 etc. (for Unix)
566if _exists("fork"):
567 if not _exists("popen2"):
568 def popen2(cmd, mode="t", bufsize=-1):
Guido van Rossumd9a8e962000-09-19 03:04:52569 import popen2
570 stdout, stdin = popen2.popen2(cmd, bufsize)
571 return stdin, stdout
Skip Montanaro269b83b2001-02-06 01:07:02572 __all__.append("popen2")
Fred Drake31f182e2000-08-28 17:20:05573
Guido van Rossumd9a8e962000-09-19 03:04:52574 if not _exists("popen3"):
575 def popen3(cmd, mode="t", bufsize=-1):
Guido van Rossumd9a8e962000-09-19 03:04:52576 import popen2
577 stdout, stdin, stderr = popen2.popen3(cmd, bufsize)
578 return stdin, stdout, stderr
Skip Montanaro269b83b2001-02-06 01:07:02579 __all__.append("popen3")
Fred Drake20af3172000-09-28 19:10:56580
581 if not _exists("popen4"):
582 def popen4(cmd, mode="t", bufsize=-1):
583 import popen2
584 stdout, stdin = popen2.popen4(cmd, bufsize)
585 return stdin, stdout
Skip Montanaro269b83b2001-02-06 01:07:02586 __all__.append("popen4")
Michael W. Hudson2b85b372002-03-07 10:12:11587
588import copy_reg as _copy_reg
589
590def _make_stat_result(tup, dict):
591 return stat_result(tup, dict)
592
593def _pickle_stat_result(sr):
594 (type, args) = sr.__reduce__()
595 return (_make_stat_result, args)
596
Michael W. Hudson09a5bd82002-03-16 18:02:20597try:
598 _copy_reg.pickle(stat_result, _pickle_stat_result, _make_stat_result)
599except NameError: # stat_result may not exist
600 pass
Michael W. Hudson2b85b372002-03-07 10:12:11601
602def _make_statvfs_result(tup, dict):
603 return statvfs_result(tup, dict)
604
605def _pickle_statvfs_result(sr):
606 (type, args) = sr.__reduce__()
607 return (_make_statvfs_result, args)
608
Michael W. Hudson09a5bd82002-03-16 18:02:20609try:
610 _copy_reg.pickle(statvfs_result, _pickle_statvfs_result,
611 _make_statvfs_result)
612except NameError: # statvfs_result may not exist
613 pass