@@ -320,8 +320,11 @@ def check_output(*popenargs, timeout=None, **kwargs):
320320 ... input=b"when in the course of fooman events\n")
321321 b'when in the course of barman events\n'
322322
323- If universal_newlines=True is passed, the "input" argument must be a
324- string and the return value will be a string rather than bytes.
323+ By default, all communication is in bytes, and therefore any "input"
324+ should be bytes, and the return value wil be bytes. If in text mode,
325+ any "input" should be a string, and the return value will be a string
326+ decoded according to locale encoding, or by "encoding" if set. Text mode
327+ is triggered by setting any of text, encoding, errors or universal_newlines.
325328 """
326329 if 'stdout' in kwargs :
327330 raise ValueError ('stdout argument not allowed, it will be overridden.' )
@@ -384,15 +387,17 @@ def run(*popenargs, input=None, timeout=None, check=False, **kwargs):
384387 exception will be raised.
385388
386389 There is an optional argument "input", allowing you to
387- pass a string to the subprocess's stdin. If you use this argument
390+ pass bytes or a string to the subprocess's stdin. If you use this argument
388391 you may not also use the Popen constructor's "stdin" argument, as
389392 it will be used internally.
390393
391- The other arguments are the same as for the Popen constructor.
394+ By default, all communication is in bytes, and therefore any "input" should
395+ be bytes, and the stdout and stderr will be bytes. If in text mode, any
396+ "input" should be a string, and stdout and stderr will be strings decoded
397+ according to locale encoding, or by "encoding" if set. Text mode is
398+ triggered by setting any of text, encoding, errors or universal_newlines.
392399
393- If universal_newlines=True is passed, the "input" argument must be a
394- string and stdout/stderr in the returned object will be strings rather than
395- bytes.
400+ The other arguments are the same as for the Popen constructor.
396401 """
397402 if input is not None :
398403 if 'stdin' in kwargs :
@@ -513,7 +518,7 @@ def getstatusoutput(cmd):
513518 (-15, '')
514519 """
515520 try :
516- data = check_output (cmd , shell = True , universal_newlines = True , stderr = STDOUT )
521+ data = check_output (cmd , shell = True , text = True , stderr = STDOUT )
517522 exitcode = 0
518523 except CalledProcessError as ex :
519524 data = ex .output
@@ -565,8 +570,10 @@ class Popen(object):
565570
566571 env: Defines the environment variables for the new process.
567572
568- universal_newlines: If true, use universal line endings for file
569- objects stdin, stdout and stderr.
573+ text: If true, decode stdin, stdout and stderr using the given encoding
574+ (if set) or the system default otherwise.
575+
576+ universal_newlines: Alias of text, provided for backwards compatibility.
570577
571578 startupinfo and creationflags (Windows only)
572579
@@ -587,10 +594,10 @@ class Popen(object):
587594 def __init__ (self , args , bufsize = - 1 , executable = None ,
588595 stdin = None , stdout = None , stderr = None ,
589596 preexec_fn = None , close_fds = _PLATFORM_DEFAULT_CLOSE_FDS ,
590- shell = False , cwd = None , env = None , universal_newlines = False ,
597+ shell = False , cwd = None , env = None , universal_newlines = None ,
591598 startupinfo = None , creationflags = 0 ,
592599 restore_signals = True , start_new_session = False ,
593- pass_fds = (), * , encoding = None , errors = None ):
600+ pass_fds = (), * , encoding = None , errors = None , text = None ):
594601 """Create new Popen instance."""
595602 _cleanup ()
596603 # Held while anything is calling waitpid before returncode has been
@@ -642,10 +649,16 @@ def __init__(self, args, bufsize=-1, executable=None,
642649 self .stderr = None
643650 self .pid = None
644651 self .returncode = None
645- self .universal_newlines = universal_newlines
646652 self .encoding = encoding
647653 self .errors = errors
648654
655+ # Validate the combinations of text and universal_newlines
656+ if (text is not None and universal_newlines is not None
657+ and bool (universal_newlines ) != bool (text )):
658+ raise SubprocessError ('Cannot disambiguate when both text '
659+ 'and universal_newlines are supplied but '
660+ 'different. Pass one or the other.' )
661+
649662 # Input and output objects. The general principle is like
650663 # this:
651664 #
@@ -677,25 +690,25 @@ def __init__(self, args, bufsize=-1, executable=None,
677690 if errread != - 1 :
678691 errread = msvcrt .open_osfhandle (errread .Detach (), 0 )
679692
680- text_mode = encoding or errors or universal_newlines
693+ self . text_mode = encoding or errors or text or universal_newlines
681694
682695 self ._closed_child_pipe_fds = False
683696
684697 try :
685698 if p2cwrite != - 1 :
686699 self .stdin = io .open (p2cwrite , 'wb' , bufsize )
687- if text_mode :
700+ if self . text_mode :
688701 self .stdin = io .TextIOWrapper (self .stdin , write_through = True ,
689702 line_buffering = (bufsize == 1 ),
690703 encoding = encoding , errors = errors )
691704 if c2pread != - 1 :
692705 self .stdout = io .open (c2pread , 'rb' , bufsize )
693- if text_mode :
706+ if self . text_mode :
694707 self .stdout = io .TextIOWrapper (self .stdout ,
695708 encoding = encoding , errors = errors )
696709 if errread != - 1 :
697710 self .stderr = io .open (errread , 'rb' , bufsize )
698- if text_mode :
711+ if self . text_mode :
699712 self .stderr = io .TextIOWrapper (self .stderr ,
700713 encoding = encoding , errors = errors )
701714
@@ -735,6 +748,16 @@ def __init__(self, args, bufsize=-1, executable=None,
735748
736749 raise
737750
751+ @property
752+ def universal_newlines (self ):
753+ # universal_newlines as retained as an alias of text_mode for API
754+ # compatability. bpo-31756
755+ return self .text_mode
756+
757+ @universal_newlines .setter
758+ def universal_newlines (self , universal_newlines ):
759+ self .text_mode = bool (universal_newlines )
760+
738761 def _translate_newlines (self , data , encoding , errors ):
739762 data = data .decode (encoding , errors )
740763 return data .replace ("\r \n " , "\n " ).replace ("\r " , "\n " )
@@ -805,12 +828,16 @@ def communicate(self, input=None, timeout=None):
805828 reached. Wait for process to terminate.
806829
807830 The optional "input" argument should be data to be sent to the
808- child process (if self.universal_newlines is True, this should
809- be a string; if it is False, "input" should be bytes), or
810- None, if no data should be sent to the child.
811-
812- communicate() returns a tuple (stdout, stderr). These will be
813- bytes or, if self.universal_newlines was True, a string.
831+ child process, or None, if no data should be sent to the child.
832+ communicate() returns a tuple (stdout, stderr).
833+
834+ By default, all communication is in bytes, and therefore any
835+ "input" should be bytes, and the (stdout, stderr) will be bytes.
836+ If in text mode (indicated by self.text_mode), any "input" should
837+ be a string, and (stdout, stderr) will be strings decoded
838+ according to locale encoding, or by "encoding" if set. Text mode
839+ is triggered by setting any of text, encoding, errors or
840+ universal_newlines.
814841 """
815842
816843 if self ._communication_started and input :
@@ -1533,7 +1560,7 @@ def _communicate(self, input, endtime, orig_timeout):
15331560
15341561 # Translate newlines, if requested.
15351562 # This also turns bytes into strings.
1536- if self .encoding or self . errors or self . universal_newlines :
1563+ if self .text_mode :
15371564 if stdout is not None :
15381565 stdout = self ._translate_newlines (stdout ,
15391566 self .stdout .encoding ,
@@ -1553,8 +1580,7 @@ def _save_input(self, input):
15531580 if self .stdin and self ._input is None :
15541581 self ._input_offset = 0
15551582 self ._input = input
1556- if input is not None and (
1557- self .encoding or self .errors or self .universal_newlines ):
1583+ if input is not None and self .text_mode :
15581584 self ._input = self ._input .encode (self .stdin .encoding ,
15591585 self .stdin .errors )
15601586
0 commit comments