[Tutor] os.spawnlp and os.spawnlpe

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon May 30 05:28:32 CEST 2005



On Sun, 29 May 2005, Jonas Melian wrote:

> which is the difference between os.spawnlp and os.spawnlpe? With an
> example, please. I don't understand it.

Hi Jonas,

The difference is that the one that ends with an 'e' allows us to make the
spawned process use an entirely clean Unix shell environment.


That is, we can define exactly what environmental variables the child
process will be able to see.  Usually, we want to just copy over our own
os.environ and let our child see the same environmental variables that we
see, but in some situations, we want to lock down those environmental
variables.

If it helps to understand the relationship between the two, we can define
something that works like os.spawnlp(), using os.spawnlpe():

### Pseudocode ###
def my_spawnlp(mode, file, *args):
    args_and_env = [args] + [os.environ]
    return os.spawnlpe(mode, file, *args_and_env)
######

(I'm labeling this as pseudocode, because truthfully, I have not tested
this well yet.  *grin*)


As another example:

######
>>> os.spawnlpe(os.P_WAIT, 'which', 'which', 'ls', os.environ)
/bin/ls
0
>>>
>>> os.spawnlpe(os.P_WAIT, 'which', 'which', 'ls', {"PATH":""})
127
######

The second call to the 'which' command fails predictibly, because we
deliberately empty out the PATH in the child's environment.


Best of wishes!



More information about the Tutor mailing list