Adapted the pipe() code to python3; API changes and str/byte handling.

- Legacy-Id: 16345
This commit is contained in:
Henrik Levkowetz 2019-06-30 21:01:44 +00:00
parent aad1d0f1e5
commit 20c44a9e1a

View file

@ -1,27 +1,28 @@
# Copyright The IETF Trust 2010-2019, All Rights Reserved
# Simplified interface to os.popen3()
def pipe(cmd, str=None):
from popen2 import Popen3 as Popen
def pipe(cmd:bytes, str:bytes=None) -> (int, bytes, bytes):
from subprocess import Popen, PIPE
bufsize = 4096
MAX = 65536*16
if str and len(str) > 4096: # XXX: Hardcoded Linux 2.4, 2.6 pipe buffer size
bufsize = len(str)
pipe = Popen(cmd, True, bufsize)
pipe = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, bufsize=bufsize, shell=True)
if not str is None:
pipe.tochild.write(str)
pipe.tochild.close()
pipe.stdin.write(str)
pipe.stdin.close()
out = ""
err = ""
out = b""
err = b""
while True:
str = pipe.fromchild.read()
str = pipe.stdout.read()
if str:
out += str
code = pipe.poll()
if code > -1:
err = pipe.childerr.read()
err = pipe.stderr.read()
break
if len(out) >= MAX:
err = "Output exceeds %s bytes and has been truncated" % MAX