diff -r dbe69f54b09c Lib/subprocess.py --- a/Lib/subprocess.py Wed Jun 01 13:54:33 2016 -0700 +++ b/Lib/subprocess.py Sat Jun 04 03:32:19 2016 +0300 @@ -427,6 +427,7 @@ import threading import msvcrt import _winapi + import ctypes class STARTUPINFO: dwFlags = 0 hStdInput = None @@ -463,12 +464,16 @@ # considered an internal implementation detail. issue10838. if _mswindows: - from _winapi import (CREATE_NEW_CONSOLE, CREATE_NEW_PROCESS_GROUP, + from _winapi import (CREATE_NEW_CONSOLE, + CREATE_NO_WINDOW, DETACHED_PROCESS, + CREATE_NEW_PROCESS_GROUP, STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, STD_ERROR_HANDLE, SW_HIDE, STARTF_USESTDHANDLES, STARTF_USESHOWWINDOW) - __all__.extend(["CREATE_NEW_CONSOLE", "CREATE_NEW_PROCESS_GROUP", + __all__.extend(["CREATE_NEW_CONSOLE", + "CREATE_NO_WINDOW", "DETACHED_PROCESS", + "CREATE_NEW_PROCESS_GROUP", "STD_INPUT_HANDLE", "STD_OUTPUT_HANDLE", "STD_ERROR_HANDLE", "SW_HIDE", "STARTF_USESTDHANDLES", "STARTF_USESHOWWINDOW"]) @@ -782,7 +787,7 @@ # Various tools for executing commands and looking at their output and status. # -def getstatusoutput(cmd): +def getstatusoutput(cmd, **kwargs): """ Return (status, output) of executing cmd in a shell. Execute the string 'cmd' in a shell with 'check_output' and @@ -802,7 +807,8 @@ (256, 'sh: /bin/junk: not found') """ try: - data = check_output(cmd, shell=True, universal_newlines=True, stderr=STDOUT) + data = check_output(cmd, shell=True, universal_newlines=True, + stderr=STDOUT, **kwargs) status = 0 except CalledProcessError as ex: data = ex.output @@ -811,7 +817,7 @@ data = data[:-1] return status, data -def getoutput(cmd): +def getoutput(cmd, **kwargs): """Return output (stdout or stderr) of executing cmd in a shell. Like getstatusoutput(), except the exit status is ignored and the return @@ -821,7 +827,7 @@ >>> subprocess.getoutput('ls /bin/ls') '/bin/ls' """ - return getstatusoutput(cmd)[1] + return getstatusoutput(cmd, **kwargs)[1] _PLATFORM_DEFAULT_CLOSE_FDS = object() @@ -835,7 +841,7 @@ stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=_PLATFORM_DEFAULT_CLOSE_FDS, shell=False, cwd=None, env=None, universal_newlines=False, - startupinfo=None, creationflags=0, + startupinfo=None, creationflags=0, encoding=None, errors=None, restore_signals=True, start_new_session=False, pass_fds=()): """Create new Popen instance.""" @@ -921,20 +927,36 @@ c2pread = msvcrt.open_osfhandle(c2pread.Detach(), 0) if errread != -1: errread = msvcrt.open_osfhandle(errread.Detach(), 0) + if encoding is None: + kernel32 = ctypes.WinDLL('kernel32') + if creationflags == DETACHED_PROCESS: + encoding = 'cp' + str(kernel32.GetACP()) + elif (creationflags == CREATE_NEW_CONSOLE or + creationflags == CREATE_NO_WINDOW): + encoding = 'cp' + str(kernel32.GetOEMCP()) + + newline = None if universal_newlines else os.linesep if p2cwrite != -1: self.stdin = io.open(p2cwrite, 'wb', bufsize) - if universal_newlines: - self.stdin = io.TextIOWrapper(self.stdin, write_through=True, - line_buffering=(bufsize == 1)) + if encoding is None and stdin == PIPE: + encoding = os.device_encoding(0) + self.stdin = io.TextIOWrapper(self.stdin, write_through=True, + encoding=encoding, errors=errors, + newline=newline, + line_buffering=(bufsize == 1)) if c2pread != -1: self.stdout = io.open(c2pread, 'rb', bufsize) - if universal_newlines: - self.stdout = io.TextIOWrapper(self.stdout) + if (encoding is None and stdout == PIPE): + encoding = os.device_encoding(1) + self.stdout = io.TextIOWrapper(self.stdout, encoding=encoding, + errors=errors, newline=newline) if errread != -1: self.stderr = io.open(errread, 'rb', bufsize) - if universal_newlines: - self.stderr = io.TextIOWrapper(self.stderr) + if encoding is None and stderr == PIPE: + encoding = os.device_encoding(2) + self.stderr = io.TextIOWrapper(self.stderr, encoding=encoding, + errors=errors, newline=newline) self._closed_child_pipe_fds = False try: diff -r dbe69f54b09c Modules/_winapi.c --- a/Modules/_winapi.c Wed Jun 01 13:54:33 2016 -0700 +++ b/Modules/_winapi.c Sat Jun 04 03:32:19 2016 +0300 @@ -1532,6 +1532,8 @@ /* constants */ WINAPI_CONSTANT(F_DWORD, CREATE_NEW_CONSOLE); + WINAPI_CONSTANT(F_DWORD, CREATE_NO_WINDOW); + WINAPI_CONSTANT(F_DWORD, DETACHED_PROCESS); WINAPI_CONSTANT(F_DWORD, CREATE_NEW_PROCESS_GROUP); WINAPI_CONSTANT(F_DWORD, DUPLICATE_SAME_ACCESS); WINAPI_CONSTANT(F_DWORD, DUPLICATE_CLOSE_SOURCE);