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

Attachment: pgpRtXLNrC7BG.pgp
Description: PGP signature

_______________________________________________
Bug-cvs mailing list
Bug-cvs@nongnu.org
http://lists.nongnu.org/mailman/listinfo/bug-cvs

Reply via email to