[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: CVS, Python's subprocess.Ppen() and STDOUT redirection
From: |
Petr Pisar |
Subject: |
Re: CVS, Python's subprocess.Ppen() and STDOUT redirection |
Date: |
Wed, 25 Aug 2010 10:44:40 +0200 |
User-agent: |
Mutt/1.5.20 (2009-12-10) |
On Tue, Aug 24, 2010 at 10:54:52PM +0200, Michał Górny wrote:
> #include <stdio.h>
>
> int main(void) {
> fprintf(stdout, "foo\n");
> fclose(stdout);
> fprintf(stderr, "bar\n");
> }
>
> As I've expected, calling it with Popen(..., stdout=sys.stderr) causes
> only 'foo' to be output.
>
> I'm not sure if the problem is rather in CVS or Python, but I
> personally think it's not a good idea to close STDOUT anyway.
>
I'm not sure what Python's Popen() does, but simple dup2(2) does not exhibit
the issue and prints both strings:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
close(1);
if (-1 == dup2(2, 1)) {
perror("Stdout to stderr dupplication failed");
exit(EXIT_FAILURE);
}
fprintf(stdout, "foo\n");
fclose(stdout);
fprintf(stderr, "bar\n");
}
And this is strace output:
close(1) = 0
dup2(2, 1) = 1
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 8), ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) =
0x7f3b05fd8000
write(1, "foo\n", 4foo
) = 4
close(1) = 0
munmap(0x7f3b05fd8000, 4096) = 0
write(2, "bar\n", 4bar
) = 4
exit_group(4) = ?
As you can see, it closes stdout, duplicates it to stderr (the
stdout=sys.stderr construct, I guess) and write to stderr succeeds even if
stdout has been closed before.
-- Petr
pgppf4sR920fH.pgp
Description: PGP signature