net_helpers: Set process streams to text mode

RootHelperProcess extends Popen from subprocess and sets all
stdin/stdout/stderr descriptors to PIPE. These descriptors use byte
array by default in Python 3. If universal_newlines [1] is set for Popen
object, then those descriptors work in text mode.

[1] https://docs.python.org/3.5/library/subprocess.html#popen-constructor

Change-Id: I3fa2192271aed81fb6da658b8196b365a20fa286
This commit is contained in:
Jakub Libosvar 2017-06-15 12:24:10 +00:00
parent 1a8901a1ed
commit 5d619e54e2
1 changed files with 4 additions and 2 deletions

View File

@ -268,6 +268,8 @@ class RootHelperProcess(subprocess.Popen):
def __init__(self, cmd, *args, **kwargs):
for arg in ('stdin', 'stdout', 'stderr'):
kwargs.setdefault(arg, subprocess.PIPE)
kwargs.setdefault('universal_newlines', True)
self.namespace = kwargs.pop('namespace', None)
self.cmd = cmd
if self.namespace is not None:
@ -283,7 +285,7 @@ class RootHelperProcess(subprocess.Popen):
utils.execute(['kill', '-%d' % sig, pid], run_as_root=True)
def read_stdout(self, timeout=None):
return self._read_stream(self.stdout, timeout).decode('utf-8')
return self._read_stream(self.stdout, timeout)
@staticmethod
def _read_stream(stream, timeout):
@ -294,7 +296,7 @@ class RootHelperProcess(subprocess.Popen):
return stream.readline()
def writeline(self, data):
self.stdin.write((data + os.linesep).encode('utf-8'))
self.stdin.write(data + os.linesep)
self.stdin.flush()
def _wait_for_child_process(self, timeout=CHILD_PROCESS_TIMEOUT,