[Python-Dev] Strange behavior of subprocess.Popen._get_handles under Windows

Alexey Borzenkov snaury at gmail.com
Thu Aug 23 07:41:40 CEST 2007


On 8/23/07, Mark Hammond <mhammond at skippinet.com.au> wrote:
> > Further investigations showed that it seems to be some strange OS
> > quirk/bug,
> I'm not quite with you here - what strange OS bug do you think you have
> found?  I expect that such a bug would be well documented somewhere, even if
> not directly by MS.
[...]
> MSDN documents that without that flag, hStdInput will be "the keyboard
> buffer" while the output handles will default to "the console window's
> buffer" - which sounds significantly different to your expectation that the
> existing handles will be used as the default (unless I misunderstand).
> Sadly, your mail isn't clear enough for me to be sure about what semantics
> you are seeing and exactly what you expect, and I don't have time to
> experiment.  Maybe a clear indication of the OS bug you are referring to,
> and some complete examples which demonstrate the problems you are having
> would help.

Hmm, sorry if I wasn't clear. Here's a better example:

1.c:
    #include <stdio.h>
    #include <windows.h>

    int main(int argc, char** argv)
    {
        HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
        fprintf(stderr, "hStdOut: %08X\n", hStdOut);
        DWORD dwWritten;
        WriteFile(hStdOut, "Something", 9, &dwWritten, NULL);
        return 0;
    }

1.py:
    import subprocess
    subprocess.call(["1.exe"])

in cmd.exe shell:
    C:\>1.exe
    hStdOut: 00000007
    Something

    C:\>1.exe>1.txt
    hStdOut: 000006E0

    (1.txt file now contains string "Something")

    C:\>1.py
    hStdOut: 00000007
    Something

    C:\>1.py>1.txt
    hStdOut: 00000004

    (1.txt file is now completely empty, i.e. hStdOut is invalid)

If what you say was true and stdout would default to console window's
buffer, then I would at least see "Something" in the console. On the
contrary output just stops working at all.

And what I expect (and always expected) is output redirection to be
inherited by child processes. The best example is when I'm writing a
batch file:

    dosomething.exe /something
    dosomething2.exe /somethingmore

and then execute it as batchfile.cmd>somefile.txt I always get this
behavior, i.e. all standard output (except standard error) is
redirected to somefile.txt. I'm sure on *nixes it's all the same as
well. So what documentation for STARTUPINFO specifies is already very
suspicious and uncommon, while in reality it doesn't even work as
specified!

Best regards,
Alexey.


More information about the Python-Dev mailing list