[Tutor] os.system vs subprocess.Popen args problems

eryksun eryksun at gmail.com
Tue Sep 3 15:48:08 CEST 2013


On Tue, Sep 3, 2013 at 5:54 AM, Oscar Benjamin
<oscar.j.benjamin at gmail.com> wrote:
> On 3 September 2013 05:49, eryksun <eryksun at gmail.com> wrote:
>
> I've previously tried to find documentation that explains how MSVCRT
> handles this. I didn't find anything as explicit as the explanation in
> the subprocess docs. For example:
> http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx

It's in the CRT startup code:

http://msdn.microsoft.com/en-us/library/a1y7w461(v=vs.90)

Visual Studio includes the CRT source in the subdirectory VC\crt\src.
The startup code calls parse_cmdline() in stdargv.c.

For the Windows API, shell32 has CommandLineToArgvW:

http://msdn.microsoft.com/en-us/library/bb776391

>>     import sys
>>     from ctypes import *
>>
>>     windll.kernel32.GetCommandLineW.restype = c_wchar_p
>>
>>     print(windll.kernel32.GetCommandLineW())
>
> I'm pretty sure Python 2.x uses CreateProcessA and GetCommandLineA. No
> idea about ffmpeg though (and probably makes no difference here)...

2.x _subprocess calls CreateProcessA, but internally Windows routes
the call to CreateProcessW, after first decoding the A(NSI) strings.
The process itself stores the command line as a UNICODE_STRING:

PEB:
http://msdn.microsoft.com/en-us/library/aa813706

RTL_USER_PROCESS_PARAMETERS
http://msdn.microsoft.com/en-us/library/aa813741

> It occurs to me that another possibility is if ffmpeg isn't really an
> .exe on PATH but rather a .bat file or something. In that case
> os.system or subprocess shell=True would pick it up but subprocess
> shell=False might not. I say "might" because at least on some version
> of Windows CreateProcess can run .bat files directly but I've never
> seen this documented anywhere.

cmd tries each PATHEXT extension, but CreateProcess only tries .exe,
and finds ffmpeg.exe.

As to batch files, "Windows Internals" (Microsoft Press) documents
that CreateProcess starts cmd.exe to run .bat and .cmd files.


More information about the Tutor mailing list