Python Forum
re-open a Popen pipe in non-binary mode
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
re-open a Popen pipe in non-binary mode
#1
i did some experimenting and found that i can exploit open()'s ability to open a file descriptor as a file to change the mode of a pipe from subprocess.Popen() or os.pipe(). here is how i tried it:

#!/usr/bin/env python3

import os
from subprocess import PIPE,Popen

proc = Popen(['bash','-c','exec xz -9 >tryout.xz'],stdin=PIPE)
ofd = proc.stdin.fileno()
prt = open(ofd,'w')
print('foobar',file=prt)
prt.close()
proc.wait()

proc = Popen(['bash','-c','exec xzdec <tryout.xz'],stdout=PIPE)
ifd = proc.stdout.fileno()
look = open(ifd,'r')
lines = look.readlines()
print(repr(lines))
look.close()
proc.wait()
so, i don't need to convert to bytes to output to a pipe, and can input a regular string from a pipe. i assume anything else that comes as a binary mode file can be done the same.
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
You can reuse the already opened Pipe.

Here two different approaches:

import os
import subprocess
import io

# Low Level approach
proc = subprocess.Popen(['cat'], stdin=subprocess.PIPE)
proc.stdin.fileno()
tw = os.fdopen(proc.stdin.fileno(), 'w', encoding='utf8')
tw.write('Hello World\n')
tw.flush()
proc.stdin.write(b'Hello World bytes\n')
proc.stdin.flush()
proc.stdin.close()

# tw is now also closed.

# different approach with io
proc = subprocess.Popen(['cat'], stdin=subprocess.PIPE)
tw2 = io.TextIOWrapper(proc.stdin, encoding='utf8')
tw2.write('Hello ...\n')
tw2.flush()
proc.stdin.write(b'Bin ...\n')
proc.stdin.flush()

tw2.close()
# will close also proc.stdin
The command cat reads from stdin.
It prints the data on the screen, when the pipe stdin was closed.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
the idea is that i wanted to switch the mode from binary to non-binary to be able to write/print regular strings to it or visa-versa. this quest was the result of the uncertainty of bytes vs. str from pickle. but, it is still a broadly usable capability, especially in using pipes i get from Popen since i expect to do that often.
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
  which exception do you get when you os.write() to a pipe that has nothing reading it? Skaperen 7 1,834 Jan-09-2023, 09:45 AM
Last Post: Gribouillis
  pipe(s) in = subprocess.Popen() Skaperen 0 1,754 Feb-13-2021, 11:48 PM
Last Post: Skaperen
  stdin input while copying from a process pipe Skaperen 0 1,510 Jul-18-2019, 12:15 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