Do not use universal newlines on old Python versions.

This commit is contained in:
Jussi Pakkanen 2017-09-16 18:05:49 +03:00
parent 076f3c53bd
commit df18c1ac98
1 changed files with 22 additions and 0 deletions

View File

@ -14,6 +14,7 @@
"""A library of random helper functionality.""" """A library of random helper functionality."""
import sys
import stat import stat
import time import time
import platform, subprocess, operator, os, shutil, re import platform, subprocess, operator, os, shutil, re
@ -509,6 +510,8 @@ def expand_arguments(args):
return expended_args return expended_args
def Popen_safe(args, write=None, stderr=subprocess.PIPE, **kwargs): def Popen_safe(args, write=None, stderr=subprocess.PIPE, **kwargs):
if sys.version_info < (3, 6) or not sys.stdout.encoding:
return Popen_safe_legacy(args, write=write, stderr=stderr, **kwargs)
p = subprocess.Popen(args, universal_newlines=True, p = subprocess.Popen(args, universal_newlines=True,
close_fds=False, close_fds=False,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
@ -516,6 +519,25 @@ def Popen_safe(args, write=None, stderr=subprocess.PIPE, **kwargs):
o, e = p.communicate(write) o, e = p.communicate(write)
return p, o, e return p, o, e
def Popen_safe_legacy(args, write=None, stderr=subprocess.PIPE, **kwargs):
p = subprocess.Popen(args, universal_newlines=False,
stdout=subprocess.PIPE,
stderr=stderr, **kwargs)
if write is not None:
write = write.encode('utf-8')
o, e = p.communicate(write)
if o is not None:
if sys.stdout.encoding:
o = o.decode(encoding=sys.stdout.encoding, errors='replace').replace('\r\n', '\n')
else:
o = o.decode(errors='replace').replace('\r\n', '\n')
if e is not None:
if sys.stderr.encoding:
e = e.decode(encoding=sys.stderr.encoding, errors='replace').replace('\r\n', '\n')
else:
e = e.decode(errors='replace').replace('\r\n', '\n')
return p, o, e
def commonpath(paths): def commonpath(paths):
''' '''
For use on Python 3.4 where os.path.commonpath is not available. For use on Python 3.4 where os.path.commonpath is not available.