Python Forum
strange effect from duplicating a file descriptor
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
strange effect from duplicating a file descriptor
#1
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  question about descriptor akbarza 5 559 Jan-09-2024, 07:09 AM
Last Post: Gribouillis
  Duplicating values 007sonic 2 491 Jul-15-2023, 07:36 PM
Last Post: 007sonic
  Printing effect sizes for variables in an anova eyavuz21 2 940 Feb-01-2023, 02:12 PM
Last Post: eyavuz21
  Help with TypeWriter Effect in Python Rich Extra 0 1,137 May-23-2022, 09:44 PM
Last Post: Extra
  Strange write()/File behavior kaega2 2 1,654 Jan-28-2022, 02:53 AM
Last Post: kaega2
  Duplicating Rows And Put Them In Certain Index Position Query eddywinch82 0 1,255 Nov-25-2020, 05:24 PM
Last Post: eddywinch82
  Rotation Effect on live Webcam Feed Leziiy 0 1,576 Sep-12-2020, 04:25 PM
Last Post: Leziiy
  Type conversion issue while using Descriptor in Python3 mailnsuresh 1 2,815 May-07-2020, 12:26 PM
Last Post: deanhystad
  How to fix statement seems to have no effect hernancrespo89 0 6,742 Jan-23-2020, 09:23 PM
Last Post: hernancrespo89
  Algorithm effect on the CPU Faruk 3 2,571 Dec-19-2018, 08:57 AM
Last Post: Faruk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020