Re: Debugging automatic quotation in subprocess.Popen()

2022-10-07 Thread Eryk Sun
On 10/7/22, c.bu...@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.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Debugging automatic quotation in subprocess.Popen()

2022-10-07 Thread Chris Angelico
On Sat, 8 Oct 2022 at 08:24,  wrote:
>
> 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?

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
-- 
https://mail.python.org/mailman/listinfo/python-list


Debugging automatic quotation in subprocess.Popen()

2022-10-07 Thread c.buhtz
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?
-- 
https://mail.python.org/mailman/listinfo/python-list