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

Tim Peters tim_one@email.msn.com
Tue, 31 Aug 1999 01:44:18 -0400


[Greg Ward]
> ...
> In a previous life, I *did* implement a spawning module for
> a certain other popular scripting language that handles
> redirection and capturing (backticks in the shell and that other
> scripting language).  It was a lot of fun, but pretty hairy.  Took
> three attempts gradually developed over two years to get it right
> in the end.  In fact, it does all the easy stuff that a Unix shell
> does in spawning commands, ie. search the path, fork 'n exec, and
> redirection and capturing.  Doesn't handle the tricky stuff, ie.
> pipelines and job control.
>
> The documentation for this module is 22 pages long; the code
> is 600+ lines of somewhat tricky Perl (1300 lines if you leave
> in comments and blank lines).  That's why the Distutils spawn
> module doesn't do anything with std{out,err,in}.

Note that win/tclWinPipe.c-- which contains the Windows-specific support for
Tcl's "exec" cmd --is about 3,200 lines of C.  It does handle pipelines and
redirection, and even fakes pipes as needed with temp files when it can
identify a pipeline component as belonging to the 16-bit subsystem.  Even so,
the Tcl help page for "exec" bristles with hilarious caveats under the Windows
subsection; e.g.,

    When redirecting from NUL:, some applications may hang, others
    will get an infinite stream of "0x01" bytes, and some will
    actually correctly get an immediate end-of-file; the behavior
    seems to depend upon something compiled into the application
    itself.  When redirecting greater than 4K or so to NUL:, some
    applications will hang.  The above problems do not happen with
    32-bit applications.

Still, people seem very happy with Tcl's exec, and I'm certain no language
tries harder to provide a portable way to "do command lines".

Two points to that:

1) If Python ever wants to do something similar, let's steal the Tcl code (&
unlike stealing Perl's code, stealing Tcl's code actually looks possible --
it's very much better organized and written).

2) For all its heroic efforts to hide platform limitations,

int
Tcl_ExecObjCmd(dummy, interp, objc, objv)
    ClientData dummy;			/* Not used. */
    Tcl_Interp *interp;			/* Current interpreter. */
    int objc;				/* Number of arguments. */
    Tcl_Obj *CONST objv[];		/* Argument objects. */
{
#ifdef MAC_TCL

    Tcl_AppendResult(interp, "exec not implemented under Mac OS",
		(char *)NULL);
    return TCL_ERROR;

#else
...

a-generalized-spawn-is-a-good-start-ly y'rs  - tim