Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5a2ca93

Browse files
committed
Checking in a bunch of spawn functions. These are only defined if we
have fork and execv (and friends) but not spawnv. They operate exactly like the spawn functions on Windows. A limited set of needed constants is also defined (P_WAIT, P_NOWAIT etc.). Also add getenv() as a familiar alias for environ.get().
1 parent 66e5385 commit 5a2ca93

1 file changed

Lines changed: 80 additions & 2 deletions

File tree

Lib/os.py

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ def execvpe(file, args, env):
201201
_execvpe(file, args, env)
202202

203203
_notfound = None
204-
def _execvpe(file, args, env = None):
205-
if env:
204+
def _execvpe(file, args, env=None):
205+
if env is not None:
206206
func = execve
207207
argrest = (args, env)
208208
else:
@@ -273,3 +273,81 @@ def __setitem__(self, key, item):
273273
self.data[key] = item
274274

275275
environ = _Environ(environ)
276+
277+
def getenv(key, default=None):
278+
"""Get an environment variable, return None if it doesn't exist.
279+
280+
The optional second argument can specify an alternative default."""
281+
return environ.get(key, default)
282+
283+
def _exists(name):
284+
try:
285+
eval(name)
286+
return 1
287+
except NameError:
288+
return 0
289+
290+
# Supply spawn*() (probably only for Unix)
291+
if _exists("fork") and not _exists("spawnv") and _exists("execv"):
292+
293+
P_WAIT = 0
294+
P_NOWAIT = P_NOWAITO = 1
295+
296+
# XXX Should we support P_DETACH? I suppose it could fork()**2
297+
# and close the std I/O streams. Also, P_OVERLAY is the same
298+
# as execv*()?
299+
300+
def _spawnvef(mode, file, args, env, func):
301+
# Internal helper; func is the exec*() function to use
302+
pid = fork()
303+
if not pid:
304+
# Child
305+
try:
306+
if env is None:
307+
func(file, args)
308+
else:
309+
func(file, args, env)
310+
except:
311+
_exit(127)
312+
else:
313+
# Parent
314+
if mode == P_NOWAIT:
315+
return pid # Caller is responsible for waiting!
316+
while 1:
317+
wpid, sts = waitpid(pid, 0)
318+
if WIFSTOPPED(sts):
319+
continue
320+
elif WIFSIGNALED(sts):
321+
return -WTERMSIG(sts)
322+
elif WIFEXITED(sts):
323+
return WEXITSTATUS(sts)
324+
else:
325+
raise error, "Not stopped, signaled or exited???"
326+
327+
def spawnv(mode, file, args):
328+
return _spawnvef(mode, file, args, None, execv)
329+
330+
def spawnve(mode, file, args, env):
331+
return _spawnvef(mode, file, args, env, execve)
332+
333+
# Supply the various other variants
334+
335+
def spawnl(mode, file, *args):
336+
return spawnv(mode, file, args)
337+
338+
def spawnle(mode, file, *args):
339+
env = args[-1]
340+
return spawnve(mode, file, args[:-1], env)
341+
342+
def spawnlp(mode, file, *args):
343+
return spawnvp(mode, file, args)
344+
345+
def spawnlpe(mode, file, *args):
346+
env = args[-1]
347+
return spawnvpe(mode, file, args[:-1], env)
348+
349+
def spawnvp(mode, file, args):
350+
return _spawnvef(mode, file, args, None, execvp)
351+
352+
def spawnvpe(mode, file, args, env):
353+
return _spawnvef(mode, file, args, env, execvpe)

0 commit comments

Comments
 (0)