Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
file descriptors
#1
this will be run in linux.  i have a function that reads from and writes to file descriptors ... the things that are used by os.read() and os.write().  i want this function to read from stdin and write to stdout.  the problem is that the function returns two file descriptors that it created, one to write to, and one to read from (they happen to be pipes like from os.pipe()).  how can i get this function to read from stdin and write to stdout?
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
import sys

stdout_bak = sys.stdout
stdin_bak = sys.stdin

with open('outfile', 'w') as out_f:
    sys.stdout = out_f
    # now print() will print  to a out_f 
    sys.stdout = stdout_bak
def openAnything(source):
    if source == "-":
        import sys
        return sys.stdin
Honestly, I didn't play with this at all. I only know the idea. Both are from 'Dive into Python':

http://www.diveintopython.net/scripts_an...tderr.html
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Simple to add stdin to:

import sys
 
class ToggleStdout:
   def __init__(self, filename=None):
       self.savestdout = None
       self.stdio_redirected = False
       if filename:
           self.savefile = filename
 
   def toggle_stdout(self):
       if self.stdio_redirected:
           sys.stdout = self.savestdout
           self.savestdout = None
           self.stdio_redirected = False
       else:
           self.savestdout = sys.stdout
           sys.stdout = open(self.savefile, 'w')
           self.stdio_redirected = True
Reply
#4
the function is reading from and writing to ... file descriptors ... as if it is doing os.read() and/or os.write().  but it makes the file descriptors (like perhaps 6 and 7) and returns them (so this is not a case of simply passing 0 and 1 to the function).  so i think i have set up some kind of concurrent descriptor-to-descriptor or sys.stdin-to-descriptor or descriptor-to-sys.stdout copying, like maybe launch 2 more child processes. that is, unless they is some other form of connecting file descriptors.  i do know that C does not have such.  but C wouldn't as it is a low layer just short of assembly and machine code.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
You have to backup stdin, stdout before that as in my previous post

sys.stdin = os.fdopen(fd_in_obj)
sys.stdout = os.fdopen(fd_out_obj)
May be?

os.fdopen creates/wraps a file descriptor in a file like object and I think you can just do the above

You may see this. It's doing basically the same
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
so, let's say i have a file descriptor and it is an int with the value of 14.  now what?

this is not about how to "wrap" a file descriptor as sys.stdin or sys.stdout or such.  if the function returns fd 17 for output then i could read from it to get the functions output but what if i want that data to go out on stdout?  what if it willbe reading fd 15 and i want to have read stdin (to get the data fed to my script as stdin)?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
My bad  Confused

I misled you.
os.fdopen(fd) parameter is integer. 

In [5]: os.fdopen??
Signature: os.fdopen(fd, *args, **kwargs)
Source:   
def fdopen(fd, *args, **kwargs):
    if not isinstance(fd, int):
        raise TypeError("invalid fd type (%s, expected integer)" % type(fd))
    import io
    return io.open(fd, *args, **kwargs)
File:      /usr/lib/python3.5/os.py
Type:      function
I've learned what kind of animal is fd because of you
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
these fd things are basically a index into a pointer list of open files each process has.  unix system layer thing.  very early versions of unix had fun when programs did (in C) stuff like write(12345,"x",1).  of course, now days, there is a solid check of fd validity.
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
  problem descriptors in Python akbarza 8 1,013 Dec-27-2023, 09:01 PM
Last Post: deanhystad
  does open() still take file descriptors in py3 Skaperen 2 3,327 Jan-25-2017, 02:30 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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