[Tutor] 2.7.3 Popen argument issues

eryksun eryksun at gmail.com
Mon Aug 27 14:45:14 CEST 2012


On Mon, Aug 27, 2012 at 3:52 AM, Ray Jones <crawlzone at gmail.com> wrote:
>
> Yes, the Bash call worked (in fact I tried it just prior to sending the
> original message just to be sure).
>
> I guess I'm a bit confused about 'splitting' the arguments. You said
> that Python splits arguments on spaces. What exactly happens to the
> arguments if we split them up into individual strings separated by commas?

I meant that only in comparison to how the shell tokenizes a command.
For example, in the shell if you want spaces to be ignored, you have
to escape them or use quotes, and if you want to include raw
quotes/backslashes you have to escape them too. To tokenize a command
string as the shell would, you can use shlex.split():

http://docs.python.org/library/shlex.html#shlex.split

Otherwise the space character doesn't have any special significance.
What matters is that all of the argument strings are in the list in
the right order. It doesn't matter how they get there. So instead of
using shlex.split you may as well build the list directly.

Popen uses the args list/tuple to call the executable (after forking
and setting up the pipes). This passes through the os module on to the
built-in posix module. There it eventually calls the POSIX system
function execv(path, argv). (FYI, in Windows there's no fork/exec;
Win32 CreateProcess is used instead.)

POSIX is a system standard that's part of the core UNIX standard
(unix.org). It's a collaboration between IEEE and The Open Group.
Here's the documentation for the exec family of calls:

http://pubs.opengroup.org/onlinepubs/009604499/functions/exec.html

Here's their description of the arguments of execv(path, argv):

    * The argument "path" points to a pathname that identifies the new
      process image file.

    * The argument "argv" is an array of character pointers to null-
      terminated strings. The application shall ensure that the last
      member of this array is a null pointer. These strings shall
      constitute the argument list available to the new process image.
      The value in argv[0] should point to a filename that is
      associated with the process being started by one of the exec
      functions.

Most programs expect their arguments to have been tokenized as the
shell would as a matter of convention. So, for example, if vlc gets
"-I" in argv[1] it expects that argv[2] will be a value such as
"dummy". A value of "-I dummy" in argv[1] in principle shouldn't work.
In practice vlc seems pretty flexible and accepts it both ways.


More information about the Tutor mailing list