• Debugging automatic quotation in subprocess.Popen()

    From c.buhtz@posteo.jp@21:1/5 to All on Fri Oct 7 08:38:40 2022
    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?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Angelico@21:1/5 to c.buhtz@posteo.jp on Sat Oct 8 08:27:57 2022
    On Sat, 8 Oct 2022 at 08:24, <c.buhtz@posteo.jp> 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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Eryk Sun@21:1/5 to c.buhtz@posteo.jp on Fri Oct 7 16:53:34 2022
    On 10/7/22, c.buhtz@posteo.jp <c.buhtz@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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)