[Python-Dev] Portable "spawn" module for core?

Guido van Rossum guido@CNRI.Reston.VA.US
Mon, 30 Aug 1999 17:35:45 -0400


> it recently occured to me that the 'spawn' module I wrote for the
> Distutils (and which Perry Stoll extended to handle NT), could fit
> nicely in the core library.  On Unix, it's just a front-end to
> fork-and-exec; on NT, it's a front-end to spawnv().  In either case,
> it's just enough code (and just tricky enough code) that not everybody
> should have to duplicate it for their own uses.
> 
> The basic idea is this:
> 
>   from spawn import spawn
>   ...
>   spawn (['cmd', 'arg1', 'arg2'])
>   # or
>   spawn (['cmd'] + args)
> 
> you get the idea: it takes a *list* representing the command to spawn:
> no strings to parse, no shells to get in the way, no sneaky
> meta-characters ruining your day, draining your efficiency, or
> compromising your security.  (Conversely, no pipelines, redirection,
> etc.)
> 
> The 'spawn()' function just calls '_spawn_posix()' or '_spawn_nt()'
> depending on os.name.  Additionally, it takes a couple of optional
> keyword arguments (all booleans): 'search_path', 'verbose', and
> 'dry_run', which do pretty much what you'd expect.
> 
> The module as it's currently in the Distutils code is attached.  Let me
> know what you think...

I'm not sure that the verbose and dry_run options belong in the
standard library.  When both are given, this does something
semi-useful; for Posix that's basically just printing the arguments,
while for NT it prints the exact command that will be executed.  Not
sure if that's significant though.

Perhaps it's better to extract the code that runs the path to find the
right executable and make that into a separate routine.  (Also, rather
than reversing the path, I would break out of the loop at the first
hit.)

--Guido van Rossum (home page: http://www.python.org/~guido/)