Skip to content

Commit 20c44a9

Browse files
committed
Adapted the pipe() code to python3; API changes and str/byte handling.
- Legacy-Id: 16345
1 parent aad1d0f commit 20c44a9

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

ietf/utils/pipe.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1+
# Copyright The IETF Trust 2010-2019, All Rights Reserved
12
# Simplified interface to os.popen3()
23

3-
def pipe(cmd, str=None):
4-
from popen2 import Popen3 as Popen
4+
def pipe(cmd:bytes, str:bytes=None) -> (int, bytes, bytes):
5+
from subprocess import Popen, PIPE
56
bufsize = 4096
67
MAX = 65536*16
78

89
if str and len(str) > 4096: # XXX: Hardcoded Linux 2.4, 2.6 pipe buffer size
910
bufsize = len(str)
1011

11-
pipe = Popen(cmd, True, bufsize)
12+
pipe = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, bufsize=bufsize, shell=True)
1213
if not str is None:
13-
pipe.tochild.write(str)
14-
pipe.tochild.close()
14+
pipe.stdin.write(str)
15+
pipe.stdin.close()
1516

16-
out = ""
17-
err = ""
17+
out = b""
18+
err = b""
1819
while True:
19-
str = pipe.fromchild.read()
20+
str = pipe.stdout.read()
2021
if str:
2122
out += str
2223
code = pipe.poll()
2324
if code > -1:
24-
err = pipe.childerr.read()
25+
err = pipe.stderr.read()
2526
break
2627
if len(out) >= MAX:
2728
err = "Output exceeds %s bytes and has been truncated" % MAX

0 commit comments

Comments
 (0)