Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does subprocess.Popen() with shell=True work differently on Linux vs Windows?

Tags:

When using subprocess.Popen(args, shell=True) to run "gcc --version" (just as an example), on Windows we get this:

>>> from subprocess import Popen >>> Popen(['gcc', '--version'], shell=True) gcc (GCC) 3.4.5 (mingw-vista special r3) ... 

So it's nicely printing out the version as I expect. But on Linux we get this:

>>> from subprocess import Popen >>> Popen(['gcc', '--version'], shell=True) gcc: no input files 

Because gcc hasn't received the --version option.

The docs don't specify exactly what should happen to the args under Windows, but it does say, on Unix, "If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional shell arguments." IMHO the Windows way is better, because it allows you to treat Popen(arglist) calls the same as Popen(arglist, shell=True) ones.

Why the difference between Windows and Linux here?

like image 970
Ben Hoyt Avatar asked Aug 10 '09 04:08

Ben Hoyt


People also ask

What is the difference between subprocess Popen and OS system?

os. system is equivalent to Unix system command, while subprocess was a helper module created to provide many of the facilities provided by the Popen commands with an easier and controllable interface. Those were designed similar to the Unix Popen command.

What is shell true in subprocess Popen?

Setting the shell argument to a true value causes subprocess to spawn an intermediate shell process, and tell it to run the command. In other words, using an intermediate shell means that variables, glob patterns, and other special shell features in the command string are processed before the command is run.

How can we avoid shell true in subprocess?

From the docs: args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names).

What is shell true in subprocess python?

After reading the docs, I came to know that shell=True means executing the code through the shell. So that means in absence, the process is directly started.


2 Answers

Actually on Windows, it does use cmd.exe when shell=True - it prepends cmd.exe /c (it actually looks up the COMSPEC environment variable but defaults to cmd.exe if not present) to the shell arguments. (On Windows 95/98 it uses the intermediate w9xpopen program to actually launch the command).

So the strange implementation is actually the UNIX one, which does the following (where each space separates a different argument):

/bin/sh -c gcc --version 

It looks like the correct implementation (at least on Linux) would be:

/bin/sh -c "gcc --version" gcc --version 

Since this would set the command string from the quoted parameters, and pass the other parameters successfully.

From the sh man page section for -c:

Read commands from the command_string operand instead of from the standard input. Special parameter 0 will be set from the command_name operand and the positional parameters ($1, $2, etc.) set from the remaining argument operands.

This patch seems to fairly simply do the trick:

--- subprocess.py.orig  2009-04-19 04:43:42.000000000 +0200 +++ subprocess.py       2009-08-10 13:08:48.000000000 +0200 @@ -990,7 +990,7 @@                  args = list(args)               if shell: -                args = ["/bin/sh", "-c"] + args +                args = ["/bin/sh", "-c"] + [" ".join(args)] + args               if executable is None:                  executable = args[0] 
like image 94
David Fraser Avatar answered Oct 12 '22 11:10

David Fraser


From the subprocess.py source:

On UNIX, with shell=True: If args is a string, it specifies the command string to execute through the shell. If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional shell arguments.

On Windows: the Popen class uses CreateProcess() to execute the child program, which operates on strings. If args is a sequence, it will be converted to a string using the list2cmdline method. Please note that not all MS Windows applications interpret the command line the same way: The list2cmdline is designed for applications using the same rules as the MS C runtime.

That doesn't answer why, just clarifies that you are seeing the expected behavior.

The "why" is probably that on UNIX-like systems, command arguments are actually passed through to applications (using the exec* family of calls) as an array of strings. In other words, the calling process decides what goes into EACH command line argument. Whereas when you tell it to use a shell, the calling process actually only gets the chance to pass a single command line argument to the shell to execute: The entire command line that you want executed, executable name and arguments, as a single string.

But on Windows, the entire command line (according to the above documentation) is passed as a single string to the child process. If you look at the CreateProcess API documentation, you will notice that it expects all of the command line arguments to be concatenated together into a big string (hence the call to list2cmdline).

Plus there is the fact that on UNIX-like systems there actually is a shell that can do useful things, so I suspect that the other reason for the difference is that on Windows, shell=True does nothing, which is why it is working the way you are seeing. The only way to make the two systems act identically would be for it to simply drop all of the command line arguments when shell=True on Windows.

like image 39
Adam Batkin Avatar answered Oct 12 '22 12:10

Adam Batkin