Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Debugging automatic quotation in subprocess.Popen()

179 views
Skip to first unread message

c.b...@posteo.jp

unread,
Oct 7, 2022, 5:23:55 PM10/7/22
to
Hello,

I need to improve my understanding about how subprocess.Popen() does
quote arguments. I have special case here.

Simple example:
Popen(['ls', '-l']) results on a shell in "ls -l" without quotation.

Quotes are added if they are needed:
Popen(['ls', 'folder with blank']) results on a shell in
"ls 'folder with blank'".

Am I right so far with the basics?

Is there a way to be sure and to debug how Popen() give it to the shell?

Chris Angelico

unread,
Oct 7, 2022, 5:28:31 PM10/7/22
to
That's kinda looking at it backwards; the shell first splits the
command into a list of arguments, and then runs it. Python has a
module that is capable of doing similar sorts of work:

https://docs.python.org/3/library/shlex.html

That may be helpful if you want to give the user something to copy and paste.

ChrisA

Eryk Sun

unread,
Oct 7, 2022, 5:54:14 PM10/7/22
to
On 10/7/22, c.b...@posteo.jp <c.b...@posteo.jp> wrote:
>
> I need to improve my understanding about how subprocess.Popen() does
> quote arguments. I have special case here.
>
> Simple example:
> Popen(['ls', '-l']) results on a shell in "ls -l" without quotation.

The shell is only used if Popen is instantiated with `shell=True`. The
above example does not use the shell. It runs the "ls" executable
directly. On POSIX systems, fork() and exec() are called to create the
child process. The argument list is passed to exec() and becomes the
argv array of the application's main() entry point function.

On Windows systems, CreateProcessW() is called to created the child
process. It requires a command-line string instead of an argument
array. The argument list gets joined into a command-line string via
subprocess.list2cmdline(), which is based on the rules that are used
by the Windows C runtime library when it parses a command line into
the argv array of an application's [w]main() entry point. That said, a
Windows application is free to parse its command line however it
wants, so listcmdline() is little more than a best guess. On Windows,
Popen() may have to be called directly with a command-line string in
some cases.
0 new messages