0

I'm using subprocess.call where you just give it an array of argumets and it will build the command line and execute it.

First of all is there any escaping involved? (for example if I pass as argument a path to a file that has spaces in it, /path/my file.txt will this be escaped? "/path/my file.txt")

And is there any way to get this command line that's generated (after escaping and all) before being executed?

As I need to check if the generated command line is not longer than certain amount of characters (to make sure it will not give an error when it gets executed).

daniels
  • 18,416
  • 31
  • 103
  • 173
  • 2
    It looks like `subprocess.list2cmdline` does this, do you have any examples of calls that people can test? – Marius Jul 20 '15 at 23:56

1 Answers1

2

If you're not using shell=True, there isn't really a "command line" involved. subprocess.Popen is just passing your argument list to the underlyingexecve() system call.

Similarly, there's no escaping, because there's no shell involved and hence nothing to interpret special characters and nothing that is going to attempt to tokenize your string.

There isn't a character limit to worry about because the arguments are never concatenated into a single command line. There may be limits on the maximum number of arguments and/or the length of individual arguments.

If you are using shell=True, you have to construct the command line yourself before passing it to subprocess.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • `getconf ARG_MAX` returns 2MiB on my system (1/4 `limit stacksize`). [*"The number of bytes available for the new process' combined argument and environment lists is {ARG_MAX}. It is implementation-defined whether null terminators, pointers, and/or any alignment bytes are included in this total."*](http://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html) – jfs Jul 21 '15 at 05:57
  • I'm using shell=False and I'm on Linux. This means that if I just do something like len(" ".join(["program", "arg1", "arg2", "argn"])) < 65535 I should be in theory safe? – daniels Jul 21 '15 at 07:08