Sometimes sys.std* have been monkeypatched to something other than real
filehandles, and the fileno() method may be missing or return a
misleading value (eg: during test cases).
This change just hard codes the real 0/1/2 file descriptor numbers
rather than trying to find another more stable symbolic name - the
values are well known and as portable as the os.dup2() call being used.
Strongly related to this, this change also moves the relevant flush()
calls before any fork/dup2 funny business, to ensure python buffers are
flushed and only the expected data hits the fds.