Python Forum
strange effect from duplicating a file descriptor - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: strange effect from duplicating a file descriptor (/thread-16195.html)



strange effect from duplicating a file descriptor - Skaperen - Feb-18-2019

many of my programs, when run with redirected I/O, and during a special part of running (the last phase) need access to the original standard file descriptors. so what i have been doing is duplicating 0,1,2 to 3,4,5 in the redirector script. now, i am starting to convert these from bash to python. the first one is basically a simple thing that redirects stdout to a file. there are other that i will get to next that do many other things. these ultimately get run by the very same programs that will be run by the redirectors.

here is the first one i an trying to convert ... in bash:
#!/bin/bash
[[ "$#" -gt 1 ]] || exit 3
o="$1"
shift
if [[ "$#" -lt 1 ]];then
    exec cat >"$o"
elif [[ -e "$o" && ! -w "$o" ]];then
    echo "o: file '$o' not writable" 1>&2
    exit 6
fi
exec 3<&0
exec 4>&1
exec 5>&2
exec "$@" 1>"$o"
here is what i first put together in python:
#!/usr/bin/env python3
"""o: set stdout to file and run commad."""
from os import dup2
from subprocess import Popen
from sys import argv,stdout
argv.pop(0)
if not argv:
    exit(1)
n=argv.pop(0)
if not argv:
    argv=['cat']
try:
    with open(n,'w') as f:
        dup2(0,3)
        dup2(1,4)
        dup2(2,5)
        print('woot',file=f)
        r=Popen(argv,stdout=f).wait()
except:
    r=1
exit(r)
line 17 is just there for debugging. i am expecting the output of the command starting in atgument 2 to be written the file named in argument 1. but it is coming out to the stdout given to it by the shell it is being run from (and nothing is written to the file). if i comment out line 14 then it works as expected, except that stdin wouldn't be duplicated to fd 3.

the output of line 17 always goes the same way as the command output (line 18).

how could having os.dup2(0,3) in there cause stdout to fail to be redirected to the file?

i have many others of these things, each doing many different kinds of redirection, most also doing the 0,1,2 -> 3,4,5 fd duplicating, some in bash, some in C, and even a few in ksh, and they all work. i intend to eventually convert them all to python3.

i am running python version 3.5.2.


RE: strange effect from duplicating a file descriptor - Skaperen - Feb-18-2019

i found my bug. the dup2(0,3) was happening after the open() which got fd 3 so dup2(0,3) was replacing it with stdin, which was the tty. so i moved the 3 dup2() calls's to before the open and all work well. i expect that fd 6 will now be the underlying fd for the file.