diff -r dbe69f54b09c Lib/encodings/aliases.py --- a/Lib/encodings/aliases.py Wed Jun 01 13:54:33 2016 -0700 +++ b/Lib/encodings/aliases.py Thu Jun 09 02:41:32 2016 +0300 @@ -547,3 +547,11 @@ 'x_mac_simp_chinese' : 'gb2312', 'x_mac_trad_chinese' : 'big5', } + +import sys + +if sys.platform.startswith("win"): + import _locale + aliases['ansi'] = _locale._getansiencoding() + aliases['oem'] = _locale._getoemencoding() + diff -r dbe69f54b09c Lib/subprocess.py --- a/Lib/subprocess.py Wed Jun 01 13:54:33 2016 -0700 +++ b/Lib/subprocess.py Thu Jun 09 02:41:32 2016 +0300 @@ -463,12 +463,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 +786,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 +806,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 +816,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 +826,7 @@ >>> subprocess.getoutput('ls /bin/ls') '/bin/ls' """ - return getstatusoutput(cmd)[1] + return getstatusoutput(cmd, **kwargs)[1] _PLATFORM_DEFAULT_CLOSE_FDS = object() @@ -835,7 +840,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 +926,35 @@ c2pread = msvcrt.open_osfhandle(c2pread.Detach(), 0) if errread != -1: errread = msvcrt.open_osfhandle(errread.Detach(), 0) + if encoding is None: + if creationflags == DETACHED_PROCESS: + encoding = 'ansi' + elif (creationflags == CREATE_NEW_CONSOLE or + creationflags == CREATE_NO_WINDOW): + encoding = 'oem' + + 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/_localemodule.c --- a/Modules/_localemodule.c Wed Jun 01 13:54:33 2016 -0700 +++ b/Modules/_localemodule.c Thu Jun 09 02:41:32 2016 +0300 @@ -282,12 +282,27 @@ #if defined(MS_WINDOWS) static PyObject* +PyLocale_getansiencoding(PyObject* self) +{ + char encoding[50]; + PyOS_snprintf(encoding, sizeof(encoding), "cp%d", GetACP()); + return Py_BuildValue("s", encoding); +} + +static PyObject* +PyLocale_getoemencoding(PyObject* self) +{ + char encoding[50]; + PyOS_snprintf(encoding, sizeof(encoding), "cp%d", GetOEMCP()); + return Py_BuildValue("s", encoding); +} + +static PyObject* PyLocale_getdefaultlocale(PyObject* self) { - char encoding[100]; char locale[100]; - PyOS_snprintf(encoding, sizeof(encoding), "cp%d", GetACP()); + PyObject *encoding = PyLocale_getansiencoding(self); if (GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SISO639LANGNAME, @@ -297,7 +312,7 @@ if (GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SISO3166CTRYNAME, locale+i, (int)(sizeof(locale)-i))) - return Py_BuildValue("ss", locale, encoding); + return Py_BuildValue("sN", locale, encoding); } /* If we end up here, this windows version didn't know about @@ -308,12 +323,12 @@ locale[1] = 'x'; if (GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDEFAULTLANGUAGE, locale+2, sizeof(locale)-2)) { - return Py_BuildValue("ss", locale, encoding); + return Py_BuildValue("sN", locale, encoding); } /* cannot determine the language code (very unlikely) */ Py_INCREF(Py_None); - return Py_BuildValue("Os", Py_None, encoding); + return Py_BuildValue("ON", Py_None, encoding); } #endif @@ -580,6 +595,8 @@ METH_VARARGS, strxfrm__doc__}, #endif #if defined(MS_WINDOWS) + {"_getansiencoding", (PyCFunction) PyLocale_getansiencoding, METH_NOARGS}, + {"_getoemencoding", (PyCFunction) PyLocale_getoemencoding, METH_NOARGS}, {"_getdefaultlocale", (PyCFunction) PyLocale_getdefaultlocale, METH_NOARGS}, #endif #ifdef HAVE_LANGINFO_H diff -r dbe69f54b09c Modules/_winapi.c --- a/Modules/_winapi.c Wed Jun 01 13:54:33 2016 -0700 +++ b/Modules/_winapi.c Thu Jun 09 02:41:32 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); diff -r dbe69f54b09c PCbuild/pcbuild.sln --- a/PCbuild/pcbuild.sln Wed Jun 01 13:54:33 2016 -0700 +++ b/PCbuild/pcbuild.sln Thu Jun 09 02:41:32 2016 +0300 @@ -1,6 +1,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 14 -VisualStudioVersion = 14.0.22823.1 +VisualStudioVersion = 14.0.25123.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{553EC33E-9816-4996-A660-5D6186A0B0B3}" ProjectSection(SolutionItems) = preProject diff -r dbe69f54b09c Python/fileutils.c --- a/Python/fileutils.c Wed Jun 01 13:54:33 2016 -0700 +++ b/Python/fileutils.c Thu Jun 09 02:41:32 2016 +0300 @@ -44,7 +44,19 @@ #endif int valid; _Py_BEGIN_SUPPRESS_IPH - valid = _PyVerify_fd(fd) && isatty(fd); + valid = _PyVerify_fd(fd); + if (valid) { + valid = isatty(fd); +#if defined(MS_WINDOWS) + if (!valid) { // isatty returns 0 for non-console + HANDLE handle = (HANDLE)_get_osfhandle(fd); + if (handle != INVALID_HANDLE_VALUE) { + // we also use console's code page for pipes + valid = GetFileType(handle) & FILE_TYPE_PIPE; + } + } +#endif + } _Py_END_SUPPRESS_IPH if (!valid) Py_RETURN_NONE;