blob: d3078d6c6f9b1b93e96ca020a627753db60c052b [file] [log] [blame]
Ka-Ping Yeefeb67192001-03-02 23:31:43 +00001r"""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:
Martin v. Löwis8b10f892002-10-09 17:23:29 +00004 - all functions from posix, nt, os2, mac, or ce, e.g. unlink, stat, etc.
5 - os.path is one of the modules posixpath, ntpath, or macpath
6 - os.name is 'posix', 'nt', 'os2', 'mac', 'ce' or 'riscos'
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 Rossume2ae77b2001-10-24 20:42:55 +000010 - os.extsep is the extension separator ('.' or '/')
Guido van Rossum4b8c6ea2000-02-04 15:39:30 +000011 - os.altsep is the alternate pathname separator (None or '/')
Guido van Rossum54f22ed2000-02-04 15:10:34 +000012 - os.pathsep is the component separator used in $PATH etc
Guido van Rossum4b8c6ea2000-02-04 15:39:30 +000013 - os.linesep is the line separator in text files ('\r' or '\n' or '\r\n')
Guido van Rossum54f22ed2000-02-04 15:10:34 +000014 - os.defpath is the default search path for executables
Guido van Rossum31104f41992-01-14 18:28:36 +000015
Guido van Rossum54f22ed2000-02-04 15:10:34 +000016Programs 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:36 +000022
Skip Montanaro269b83b2001-02-06 01:07:02 +000023#'
24
Guido van Rossum2979b011994-08-01 11:18:30 +000025import sys
Guido van Rossuma28dab51997-08-29 22:36:47 +000026
27_names = sys.builtin_module_names
28
Skip Montanaro6c0a0e12001-02-28 01:00:58 +000029__all__ = ["altsep", "curdir", "pardir", "sep", "pathsep", "linesep",
Skip Montanaro117910d2003-02-14 19:35:31 +000030 "defpath", "name", "path"]
Skip Montanaro269b83b2001-02-06 01:07:02 +000031
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 from posix import *
42 try:
43 from posix import _exit
44 except ImportError:
45 pass
Skip Montanaro117910d2003-02-14 19:35:31 +000046 import posixpath as path
47
Skip Montanaro269b83b2001-02-06 01:07:02 +000048 import posix
49 __all__.extend(_get_exports_list(posix))
50 del posix
51
Guido van Rossuma28dab51997-08-29 22:36:47 +000052elif 'nt' in _names:
Guido van Rossum61de0ac1997-12-05 21:24:30 +000053 name = 'nt'
Guido van Rossume9387ea1998-05-22 15:26:04 +000054 linesep = '\r\n'
Guido van Rossum61de0ac1997-12-05 21:24:30 +000055 from nt import *
Tim Peters6757c1e2003-01-08 21:20:57 +000056 try:
57 from nt import _exit
58 except ImportError:
59 pass
Skip Montanaro117910d2003-02-14 19:35:31 +000060 import ntpath as path
61
Skip Montanaro269b83b2001-02-06 01:07:02 +000062 import nt
63 __all__.extend(_get_exports_list(nt))
64 del nt
65
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000066elif 'os2' in _names:
Guido van Rossum61de0ac1997-12-05 21:24:30 +000067 name = 'os2'
Guido van Rossume9387ea1998-05-22 15:26:04 +000068 linesep = '\r\n'
Guido van Rossum61de0ac1997-12-05 21:24:30 +000069 from os2 import *
70 try:
71 from os2 import _exit
72 except ImportError:
73 pass
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000074 if sys.version.find('EMX GCC') == -1:
Skip Montanaro117910d2003-02-14 19:35:31 +000075 import ntpath as path
Andrew MacIntyre5cef5712002-02-24 05:32:32 +000076 else:
Skip Montanaro117910d2003-02-14 19:35:31 +000077 import os2emxpath as path
78
Skip Montanaro269b83b2001-02-06 01:07:02 +000079 import os2
80 __all__.extend(_get_exports_list(os2))
81 del os2
82
Guido van Rossuma28dab51997-08-29 22:36:47 +000083elif 'mac' in _names:
Guido van Rossum61de0ac1997-12-05 21:24:30 +000084 name = 'mac'
Guido van Rossume9387ea1998-05-22 15:26:04 +000085 linesep = '\r'
Guido van Rossum61de0ac1997-12-05 21:24:30 +000086 from mac import *
87 try:
88 from mac import _exit
89 except ImportError:
90 pass
Skip Montanaro117910d2003-02-14 19:35:31 +000091 import macpath as path
92
Skip Montanaro269b83b2001-02-06 01:07:02 +000093 import mac
94 __all__.extend(_get_exports_list(mac))
95 del mac
96
Guido van Rossum18df5d41999-06-11 01:37:27 +000097elif 'ce' in _names:
98 name = 'ce'
99 linesep = '\r\n'
Guido van Rossum18df5d41999-06-11 01:37:27 +0000100 from ce import *
Tim Peters6757c1e2003-01-08 21:20:57 +0000101 try:
102 from ce import _exit
103 except ImportError:
104 pass
Guido van Rossum18df5d41999-06-11 01:37:27 +0000105 # We can use the standard Windows path.
Skip Montanaro117910d2003-02-14 19:35:31 +0000106 import ntpath as path
107
Skip Montanaro269b83b2001-02-06 01:07:02 +0000108 import ce
109 __all__.extend(_get_exports_list(ce))
110 del ce
111
Guido van Rossumd74fb6b2001-03-02 06:43:49 +0000112elif 'riscos' in _names:
113 name = 'riscos'
114 linesep = '\n'
Guido van Rossumd74fb6b2001-03-02 06:43:49 +0000115 from riscos import *
116 try:
117 from riscos import _exit
118 except ImportError:
119 pass
Skip Montanaro117910d2003-02-14 19:35:31 +0000120 import riscospath as path
121
Guido van Rossumd74fb6b2001-03-02 06:43:49 +0000122 import riscos
123 __all__.extend(_get_exports_list(riscos))
Skip Montanaro81e4b1c2001-03-06 15:26:07 +0000124 del riscos
Guido van Rossumd74fb6b2001-03-02 06:43:49 +0000125
Guido van Rossum2979b011994-08-01 11:18:30 +0000126else:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000127 raise ImportError, 'no os specific module found'
Guido van Rossume65cce51993-11-08 15:05:21 +0000128
Skip Montanaro117910d2003-02-14 19:35:31 +0000129sys.modules['os.path'] = path
130from os.path import curdir, pardir, sep, pathsep, defpath, extsep, altsep
Skip Montanaro269b83b2001-02-06 01:07:02 +0000131
Guido van Rossuma28dab51997-08-29 22:36:47 +0000132del _names
133
Skip Montanaro269b83b2001-02-06 01:07:02 +0000134#'
135
Guido van Rossum4def7de1998-07-24 20:48:03 +0000136# Super directory utilities.
137# (Inspired by Eric Raymond; the doc strings are mostly his)
138
139def makedirs(name, mode=0777):
Fred Drakecadb9eb2002-07-02 21:28:04 +0000140 """makedirs(path [, mode=0777])
Guido van Rossum4def7de1998-07-24 20:48:03 +0000141
142 Super-mkdir; create a leaf directory and all intermediate ones.
143 Works like mkdir, except that any intermediate path segment (not
144 just the rightmost) will be created if it does not exist. This is
145 recursive.
146
147 """
148 head, tail = path.split(name)
Fred Drake9f2550f2000-07-25 15:16:40 +0000149 if not tail:
150 head, tail = path.split(head)
Guido van Rossum4def7de1998-07-24 20:48:03 +0000151 if head and tail and not path.exists(head):
152 makedirs(head, mode)
153 mkdir(name, mode)
154
155def removedirs(name):
Fred Drakecadb9eb2002-07-02 21:28:04 +0000156 """removedirs(path)
Guido van Rossum4def7de1998-07-24 20:48:03 +0000157
158 Super-rmdir; remove a leaf directory and empty all intermediate
159 ones. Works like rmdir except that, if the leaf directory is
160 successfully removed, directories corresponding to rightmost path
161 segments will be pruned way until either the whole path is
162 consumed or an error occurs. Errors during this latter phase are
163 ignored -- they generally mean that a directory was not empty.
164
165 """
166 rmdir(name)
167 head, tail = path.split(name)
Fred Drake9f2550f2000-07-25 15:16:40 +0000168 if not tail:
169 head, tail = path.split(head)
Guido van Rossum4def7de1998-07-24 20:48:03 +0000170 while head and tail:
171 try:
172 rmdir(head)
173 except error:
174 break
175 head, tail = path.split(head)
176
177def renames(old, new):
Fred Drakecadb9eb2002-07-02 21:28:04 +0000178 """renames(old, new)
Guido van Rossum4def7de1998-07-24 20:48:03 +0000179
180 Super-rename; create directories as necessary and delete any left
181 empty. Works like rename, except creation of any intermediate
182 directories needed to make the new pathname good is attempted
183 first. After the rename, directories corresponding to rightmost
184 path segments of the old name will be pruned way until either the
185 whole path is consumed or a nonempty directory is found.
186
187 Note: this function can fail with the new directory structure made
188 if you lack permissions needed to unlink the leaf directory or
189 file.
190
191 """
192 head, tail = path.split(new)
193 if head and tail and not path.exists(head):
194 makedirs(head)
195 rename(old, new)
196 head, tail = path.split(old)
197 if head and tail:
198 try:
199 removedirs(head)
200 except error:
201 pass
202
Skip Montanaro269b83b2001-02-06 01:07:02 +0000203__all__.extend(["makedirs", "removedirs", "renames"])
204
Guido van Rossuma28dab51997-08-29 22:36:47 +0000205# Make sure os.environ exists, at least
206try:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000207 environ
Guido van Rossuma28dab51997-08-29 22:36:47 +0000208except NameError:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000209 environ = {}
Guido van Rossuma28dab51997-08-29 22:36:47 +0000210
Guido van Rossume65cce51993-11-08 15:05:21 +0000211def execl(file, *args):
Guido van Rossum7da3cc52000-04-25 10:53:22 +0000212 """execl(file, *args)
213
214 Execute the executable file with argument list args, replacing the
215 current process. """
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000216 execv(file, args)
Guido van Rossume65cce51993-11-08 15:05:21 +0000217
218def execle(file, *args):
Guido van Rossum7da3cc52000-04-25 10:53:22 +0000219 """execle(file, *args, env)
220
221 Execute the executable file with argument list args and
222 environment env, replacing the current process. """
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000223 env = args[-1]
224 execve(file, args[:-1], env)
Guido van Rossume65cce51993-11-08 15:05:21 +0000225
226def execlp(file, *args):
Guido van Rossum7da3cc52000-04-25 10:53:22 +0000227 """execlp(file, *args)
228
229 Execute the executable file (which is searched for along $PATH)
230 with argument list args, replacing the current process. """
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000231 execvp(file, args)
Guido van Rossume65cce51993-11-08 15:05:21 +0000232
Guido van Rossum030afb11995-03-14 17:27:18 +0000233def execlpe(file, *args):
Guido van Rossum7da3cc52000-04-25 10:53:22 +0000234 """execlpe(file, *args, env)
235
236 Execute the executable file (which is searched for along $PATH)
237 with argument list args and environment env, replacing the current
Tim Peters2344fae2001-01-15 00:50:52 +0000238 process. """
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000239 env = args[-1]
240 execvpe(file, args[:-1], env)
Guido van Rossum030afb11995-03-14 17:27:18 +0000241
Guido van Rossume65cce51993-11-08 15:05:21 +0000242def execvp(file, args):
Guido van Rossum7da3cc52000-04-25 10:53:22 +0000243 """execp(file, args)
244
245 Execute the executable file (which is searched for along $PATH)
246 with argument list args, replacing the current process.
Thomas Wouters7e474022000-07-16 12:04:32 +0000247 args may be a list or tuple of strings. """
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000248 _execvpe(file, args)
Guido van Rossum030afb11995-03-14 17:27:18 +0000249
250def execvpe(file, args, env):
Guido van Rossum683c0fe2002-09-03 16:36:17 +0000251 """execvpe(file, args, env)
Guido van Rossum7da3cc52000-04-25 10:53:22 +0000252
253 Execute the executable file (which is searched for along $PATH)
254 with argument list args and environment env , replacing the
255 current process.
Tim Peters2344fae2001-01-15 00:50:52 +0000256 args may be a list or tuple of strings. """
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000257 _execvpe(file, args, env)
Guido van Rossum030afb11995-03-14 17:27:18 +0000258
Skip Montanaro269b83b2001-02-06 01:07:02 +0000259__all__.extend(["execl","execle","execlp","execlpe","execvp","execvpe"])
260
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000261def _execvpe(file, args, env=None):
Guido van Rossumaed51d82002-08-05 16:13:24 +0000262 from errno import ENOENT, ENOTDIR
263
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000264 if env is not None:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000265 func = execve
266 argrest = (args, env)
267 else:
268 func = execv
269 argrest = (args,)
270 env = environ
Guido van Rossumaed51d82002-08-05 16:13:24 +0000271
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000272 head, tail = path.split(file)
273 if head:
274 apply(func, (file,) + argrest)
275 return
Raymond Hettinger54f02222002-06-01 14:18:47 +0000276 if 'PATH' in env:
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000277 envpath = env['PATH']
278 else:
279 envpath = defpath
Guido van Rossum965fdae2000-04-04 19:50:04 +0000280 PATH = envpath.split(pathsep)
Guido van Rossum683c0fe2002-09-03 16:36:17 +0000281 saved_exc = None
282 saved_tb = None
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000283 for dir in PATH:
284 fullname = path.join(dir, file)
285 try:
286 apply(func, (fullname,) + argrest)
Guido van Rossum683c0fe2002-09-03 16:36:17 +0000287 except error, e:
288 tb = sys.exc_info()[2]
289 if (e.errno != ENOENT and e.errno != ENOTDIR
290 and saved_exc is None):
291 saved_exc = e
292 saved_tb = tb
293 if saved_exc:
294 raise error, saved_exc, saved_tb
295 raise error, e, tb
Guido van Rossumd74fb6b2001-03-02 06:43:49 +0000296
Martin v. Löwisa90f4382001-03-07 09:05:45 +0000297# Change environ to automatically call putenv() if it exists
298try:
299 # This will fail if there's no putenv
300 putenv
301except NameError:
302 pass
303else:
304 import UserDict
Guido van Rossum3b8e20d1996-07-24 00:55:17 +0000305
Guido van Rossumc524d952001-10-19 01:31:59 +0000306 # Fake unsetenv() for Windows
Martin v. Löwis8b10f892002-10-09 17:23:29 +0000307 # not sure about os2 here but
Guido van Rossumc524d952001-10-19 01:31:59 +0000308 # I'm guessing they are the same.
309
Martin v. Löwis8b10f892002-10-09 17:23:29 +0000310 if name in ('os2', 'nt'):
Guido van Rossumc524d952001-10-19 01:31:59 +0000311 def unsetenv(key):
312 putenv(key, "")
313
Martin v. Löwisa90f4382001-03-07 09:05:45 +0000314 if name == "riscos":
315 # On RISC OS, all env access goes through getenv and putenv
316 from riscosenviron import _Environ
Martin v. Löwis8b10f892002-10-09 17:23:29 +0000317 elif name in ('os2', 'nt'): # Where Env Var Names Must Be UPPERCASE
Guido van Rossumda4d6da1998-08-04 16:01:23 +0000318 # But we store them as upper case
Raymond Hettingerca2f5372002-09-06 19:36:31 +0000319 class _Environ(UserDict.IterableUserDict):
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000320 def __init__(self, environ):
321 UserDict.UserDict.__init__(self)
Guido van Rossumda4d6da1998-08-04 16:01:23 +0000322 data = self.data
Guido van Rossumda4d6da1998-08-04 16:01:23 +0000323 for k, v in environ.items():
Guido van Rossum965fdae2000-04-04 19:50:04 +0000324 data[k.upper()] = v
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000325 def __setitem__(self, key, item):
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000326 putenv(key, item)
Guido van Rossum965fdae2000-04-04 19:50:04 +0000327 self.data[key.upper()] = item
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000328 def __getitem__(self, key):
Guido van Rossum965fdae2000-04-04 19:50:04 +0000329 return self.data[key.upper()]
Guido van Rossumc524d952001-10-19 01:31:59 +0000330 try:
331 unsetenv
332 except NameError:
333 def __delitem__(self, key):
334 del self.data[key.upper()]
335 else:
336 def __delitem__(self, key):
337 unsetenv(key)
338 del self.data[key.upper()]
Guido van Rossumb46413f1999-05-03 15:23:24 +0000339 def has_key(self, key):
Raymond Hettinger54f02222002-06-01 14:18:47 +0000340 return key.upper() in self.data
341 def __contains__(self, key):
342 return key.upper() in self.data
Guido van Rossum965fdae2000-04-04 19:50:04 +0000343 def get(self, key, failobj=None):
344 return self.data.get(key.upper(), failobj)
345 def update(self, dict):
346 for k, v in dict.items():
347 self[k] = v
Martin v. Löwisa066f462002-05-02 17:39:19 +0000348 def copy(self):
349 return dict(self)
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
Raymond Hettinger05212fc2002-09-07 04:48:03 +0000352 class _Environ(UserDict.IterableUserDict):
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000353 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 Rossumc524d952001-10-19 01:31:59 +0000362 try:
363 unsetenv
364 except NameError:
365 pass
366 else:
367 def __delitem__(self, key):
368 unsetenv(key)
369 del self.data[key]
Martin v. Löwisa066f462002-05-02 17:39:19 +0000370 def copy(self):
371 return dict(self)
Tim Peters1633a2e2001-10-30 05:56:40 +0000372
Guido van Rossum61de0ac1997-12-05 21:24:30 +0000373
374 environ = _Environ(environ)
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000375
Jack Jansenb11ce9b2003-01-08 16:33:40 +0000376def getenv(key, default=None):
Tim Peters2c60f7a2003-01-29 03:49:43 +0000377 """Get an environment variable, return None if it doesn't exist.
378 The optional second argument can specify an alternate default."""
379 return environ.get(key, default)
Jack Jansenb11ce9b2003-01-08 16:33:40 +0000380__all__.append("getenv")
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000381
382def _exists(name):
383 try:
384 eval(name)
Tim Petersbc0e9102002-04-04 22:55:58 +0000385 return True
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000386 except NameError:
Tim Petersbc0e9102002-04-04 22:55:58 +0000387 return False
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000388
389# Supply spawn*() (probably only for Unix)
390if _exists("fork") and not _exists("spawnv") and _exists("execv"):
391
392 P_WAIT = 0
393 P_NOWAIT = P_NOWAITO = 1
394
395 # XXX Should we support P_DETACH? I suppose it could fork()**2
396 # and close the std I/O streams. Also, P_OVERLAY is the same
397 # as execv*()?
398
399 def _spawnvef(mode, file, args, env, func):
400 # Internal helper; func is the exec*() function to use
401 pid = fork()
402 if not pid:
403 # Child
404 try:
405 if env is None:
406 func(file, args)
407 else:
408 func(file, args, env)
409 except:
410 _exit(127)
411 else:
412 # Parent
413 if mode == P_NOWAIT:
414 return pid # Caller is responsible for waiting!
415 while 1:
416 wpid, sts = waitpid(pid, 0)
417 if WIFSTOPPED(sts):
418 continue
419 elif WIFSIGNALED(sts):
420 return -WTERMSIG(sts)
421 elif WIFEXITED(sts):
422 return WEXITSTATUS(sts)
423 else:
424 raise error, "Not stopped, signaled or exited???"
425
426 def spawnv(mode, file, args):
Guido van Rossume0cd2912000-04-21 18:35:36 +0000427 """spawnv(mode, file, args) -> integer
428
429Execute file with arguments from args in a subprocess.
430If mode == P_NOWAIT return the pid of the process.
431If mode == P_WAIT return the process's exit code if it exits normally;
Tim Peters2344fae2001-01-15 00:50:52 +0000432otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000433 return _spawnvef(mode, file, args, None, execv)
434
435 def spawnve(mode, file, args, env):
Guido van Rossume0cd2912000-04-21 18:35:36 +0000436 """spawnve(mode, file, args, env) -> integer
437
438Execute file with arguments from args in a subprocess with the
439specified environment.
440If mode == P_NOWAIT return the pid of the process.
441If mode == P_WAIT return the process's exit code if it exits normally;
442otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000443 return _spawnvef(mode, file, args, env, execve)
444
Guido van Rossumdd7cbbf1999-11-02 20:44:07 +0000445 # Note: spawnvp[e] is't currently supported on Windows
446
447 def spawnvp(mode, file, args):
Guido van Rossume0cd2912000-04-21 18:35:36 +0000448 """spawnvp(mode, file, args) -> integer
449
450Execute file (which is looked for along $PATH) with arguments from
451args in a subprocess.
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, None, execvp)
456
457 def spawnvpe(mode, file, args, env):
Guido van Rossume0cd2912000-04-21 18:35:36 +0000458 """spawnvpe(mode, file, args, env) -> integer
459
460Execute file (which is looked for along $PATH) with arguments from
461args in a subprocess with the supplied environment.
462If mode == P_NOWAIT return the pid of the process.
463If mode == P_WAIT return the process's exit code if it exits normally;
464otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossumdd7cbbf1999-11-02 20:44:07 +0000465 return _spawnvef(mode, file, args, env, execvpe)
466
467if _exists("spawnv"):
468 # These aren't supplied by the basic Windows code
469 # but can be easily implemented in Python
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000470
471 def spawnl(mode, file, *args):
Guido van Rossume0cd2912000-04-21 18:35:36 +0000472 """spawnl(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;
477otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000478 return spawnv(mode, file, args)
479
480 def spawnle(mode, file, *args):
Guido van Rossume0cd2912000-04-21 18:35:36 +0000481 """spawnle(mode, file, *args, env) -> integer
482
483Execute file with arguments from args in a subprocess with the
484supplied 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:32 +0000488 env = args[-1]
489 return spawnve(mode, file, args[:-1], env)
490
Guido van Rossumdd7cbbf1999-11-02 20:44:07 +0000491if _exists("spawnvp"):
492 # At the moment, Windows doesn't implement spawnvp[e],
493 # so it won't have spawnlp[e] either.
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000494 def spawnlp(mode, file, *args):
Guido van Rossume0cd2912000-04-21 18:35:36 +0000495 """spawnlp(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 return spawnvp(mode, file, args)
503
504 def spawnlpe(mode, file, *args):
Guido van Rossume0cd2912000-04-21 18:35:36 +0000505 """spawnlpe(mode, file, *args, env) -> integer
506
507Execute file (which is looked for along $PATH) with arguments from
508args in a subprocess with the supplied environment.
509If mode == P_NOWAIT return the pid of the process.
510If mode == P_WAIT return the process's exit code if it exits normally;
511otherwise return -SIG, where SIG is the signal that killed it. """
Guido van Rossum5a2ca931999-11-02 13:27:32 +0000512 env = args[-1]
513 return spawnvpe(mode, file, args[:-1], env)
Guido van Rossume0cd2912000-04-21 18:35:36 +0000514
515
Skip Montanaro269b83b2001-02-06 01:07:02 +0000516 __all__.extend(["spawnlp","spawnlpe","spawnv", "spawnve","spawnvp",
517 "spawnvpe","spawnl","spawnle",])
518
519
Guido van Rossumd9a8e962000-09-19 03:04:52 +0000520# Supply popen2 etc. (for Unix)
521if _exists("fork"):
522 if not _exists("popen2"):
523 def popen2(cmd, mode="t", bufsize=-1):
Guido van Rossumd9a8e962000-09-19 03:04:52 +0000524 import popen2
525 stdout, stdin = popen2.popen2(cmd, bufsize)
526 return stdin, stdout
Skip Montanaro269b83b2001-02-06 01:07:02 +0000527 __all__.append("popen2")
Fred Drake31f182e2000-08-28 17:20:05 +0000528
Guido van Rossumd9a8e962000-09-19 03:04:52 +0000529 if not _exists("popen3"):
530 def popen3(cmd, mode="t", bufsize=-1):
Guido van Rossumd9a8e962000-09-19 03:04:52 +0000531 import popen2
532 stdout, stdin, stderr = popen2.popen3(cmd, bufsize)
533 return stdin, stdout, stderr
Skip Montanaro269b83b2001-02-06 01:07:02 +0000534 __all__.append("popen3")
Fred Drake20af3172000-09-28 19:10:56 +0000535
536 if not _exists("popen4"):
537 def popen4(cmd, mode="t", bufsize=-1):
538 import popen2
539 stdout, stdin = popen2.popen4(cmd, bufsize)
540 return stdin, stdout
Skip Montanaro269b83b2001-02-06 01:07:02 +0000541 __all__.append("popen4")
Michael W. Hudson0e025302002-03-06 17:11:18 +0000542
543import copy_reg as _copy_reg
544
545def _make_stat_result(tup, dict):
546 return stat_result(tup, dict)
547
548def _pickle_stat_result(sr):
549 (type, args) = sr.__reduce__()
550 return (_make_stat_result, args)
551
Michael W. Hudsonce00b732002-03-15 10:18:58 +0000552try:
553 _copy_reg.pickle(stat_result, _pickle_stat_result, _make_stat_result)
554except NameError: # stat_result may not exist
555 pass
Michael W. Hudson0e025302002-03-06 17:11:18 +0000556
557def _make_statvfs_result(tup, dict):
558 return statvfs_result(tup, dict)
559
560def _pickle_statvfs_result(sr):
561 (type, args) = sr.__reduce__()
562 return (_make_statvfs_result, args)
563
Michael W. Hudsonce00b732002-03-15 10:18:58 +0000564try:
565 _copy_reg.pickle(statvfs_result, _pickle_statvfs_result,
566 _make_statvfs_result)
Michael W. Hudsone5363b72002-03-15 10:21:59 +0000567except NameError: # statvfs_result may not exist
Michael W. Hudsonce00b732002-03-15 10:18:58 +0000568 pass