diff options
author | Brian Curtin <brian.curtin@gmail.com> | 2010-04-24 15:40:11 (GMT) |
---|---|---|
committer | Brian Curtin <brian.curtin@gmail.com> | 2010-04-24 15:40:11 (GMT) |
commit | a2936cfa0989988f3e9f9ce6aec7d5657a2f73ba (patch) | |
tree | df5f23f5cfb46e933746afd5ddec7c6e117e6a38 /Lib/subprocess.py | |
parent | 55841ac1a54423b972884726944ae279cd554c35 (diff) | |
download | cpython-a2936cfa0989988f3e9f9ce6aec7d5657a2f73ba.zip cpython-a2936cfa0989988f3e9f9ce6aec7d5657a2f73ba.tar.gz cpython-a2936cfa0989988f3e9f9ce6aec7d5657a2f73ba.tar.bz2 |
Fix #7838. Add docstrings and privatize _subprocess implementation details.
Since CREATE_NEW_* are used for the creation flags of a subprocess, they
were added to __all__. The rest of the previously exposed attributes are
now qualified by _subprocess.ATTR rather than importing *.
Diffstat (limited to 'Lib/subprocess.py')
-rw-r--r-- | Lib/subprocess.py | 86 |
1 files changed, 39 insertions, 47 deletions
diff --git a/Lib/subprocess.py b/Lib/subprocess.py index e4c843d..8c687c9 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -413,31 +413,18 @@ class CalledProcessError(Exception): if mswindows: + from _subprocess import CREATE_NEW_CONSOLE, CREATE_NEW_PROCESS_GROUP import threading import msvcrt - if 0: # <-- change this to use pywin32 instead of the _subprocess driver - import pywintypes - from win32api import GetStdHandle, STD_INPUT_HANDLE, \ - STD_OUTPUT_HANDLE, STD_ERROR_HANDLE - from win32api import GetCurrentProcess, DuplicateHandle, \ - GetModuleFileName, GetVersion - from win32con import DUPLICATE_SAME_ACCESS, SW_HIDE - from win32pipe import CreatePipe - from win32process import CreateProcess, STARTUPINFO, \ - GetExitCodeProcess, STARTF_USESTDHANDLES, \ - STARTF_USESHOWWINDOW, CREATE_NEW_CONSOLE - from win32process import TerminateProcess - from win32event import WaitForSingleObject, INFINITE, WAIT_OBJECT_0 - else: - from _subprocess import * - class STARTUPINFO: - dwFlags = 0 - hStdInput = None - hStdOutput = None - hStdError = None - wShowWindow = 0 - class pywintypes: - error = IOError + import _subprocess + class STARTUPINFO: + dwFlags = 0 + hStdInput = None + hStdOutput = None + hStdError = None + wShowWindow = 0 + class pywintypes: + error = IOError else: import select _has_poll = hasattr(select, 'poll') @@ -454,6 +441,8 @@ else: __all__ = ["Popen", "PIPE", "STDOUT", "call", "check_call", "check_output", "CalledProcessError"] +if mswindows: + __all__.extend(["CREATE_NEW_CONSOLE", "CREATE_NEW_PROCESS_GROUP"]) try: MAXFD = os.sysconf("SC_OPEN_MAX") except: @@ -771,11 +760,11 @@ class Popen(object): errread, errwrite = None, None if stdin is None: - p2cread = GetStdHandle(STD_INPUT_HANDLE) + p2cread = _subprocess.GetStdHandle(_subprocess.STD_INPUT_HANDLE) if p2cread is None: - p2cread, _ = CreatePipe(None, 0) + p2cread, _ = _subprocess.CreatePipe(None, 0) elif stdin == PIPE: - p2cread, p2cwrite = CreatePipe(None, 0) + p2cread, p2cwrite = _subprocess.CreatePipe(None, 0) elif isinstance(stdin, int): p2cread = msvcrt.get_osfhandle(stdin) else: @@ -784,11 +773,11 @@ class Popen(object): p2cread = self._make_inheritable(p2cread) if stdout is None: - c2pwrite = GetStdHandle(STD_OUTPUT_HANDLE) + c2pwrite = _subprocess.GetStdHandle(_subprocess.STD_OUTPUT_HANDLE) if c2pwrite is None: - _, c2pwrite = CreatePipe(None, 0) + _, c2pwrite = _subprocess.CreatePipe(None, 0) elif stdout == PIPE: - c2pread, c2pwrite = CreatePipe(None, 0) + c2pread, c2pwrite = _subprocess.CreatePipe(None, 0) elif isinstance(stdout, int): c2pwrite = msvcrt.get_osfhandle(stdout) else: @@ -797,11 +786,11 @@ class Popen(object): c2pwrite = self._make_inheritable(c2pwrite) if stderr is None: - errwrite = GetStdHandle(STD_ERROR_HANDLE) + errwrite = _subprocess.GetStdHandle(_subprocess.STD_ERROR_HANDLE) if errwrite is None: - _, errwrite = CreatePipe(None, 0) + _, errwrite = _subprocess.CreatePipe(None, 0) elif stderr == PIPE: - errread, errwrite = CreatePipe(None, 0) + errread, errwrite = _subprocess.CreatePipe(None, 0) elif stderr == STDOUT: errwrite = c2pwrite elif isinstance(stderr, int): @@ -818,14 +807,15 @@ class Popen(object): def _make_inheritable(self, handle): """Return a duplicate of handle, which is inheritable""" - return DuplicateHandle(GetCurrentProcess(), handle, - GetCurrentProcess(), 0, 1, - DUPLICATE_SAME_ACCESS) + return _subprocess.DuplicateHandle(_subprocess.GetCurrentProcess(), + handle, _subprocess.GetCurrentProcess(), 0, 1, + _subprocess.DUPLICATE_SAME_ACCESS) def _find_w9xpopen(self): """Find and return absolut path to w9xpopen.exe""" - w9xpopen = os.path.join(os.path.dirname(GetModuleFileName(0)), + w9xpopen = os.path.join( + os.path.dirname(_subprocess.GetModuleFileName(0)), "w9xpopen.exe") if not os.path.exists(w9xpopen): # Eeek - file-not-found - possibly an embedding @@ -854,17 +844,17 @@ class Popen(object): if startupinfo is None: startupinfo = STARTUPINFO() if None not in (p2cread, c2pwrite, errwrite): - startupinfo.dwFlags |= STARTF_USESTDHANDLES + startupinfo.dwFlags |= _subprocess.STARTF_USESTDHANDLES startupinfo.hStdInput = p2cread startupinfo.hStdOutput = c2pwrite startupinfo.hStdError = errwrite if shell: - startupinfo.dwFlags |= STARTF_USESHOWWINDOW - startupinfo.wShowWindow = SW_HIDE + startupinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW + startupinfo.wShowWindow = _subprocess.SW_HIDE comspec = os.environ.get("COMSPEC", "cmd.exe") args = comspec + " /c " + args - if (GetVersion() >= 0x80000000L or + if (_subprocess.GetVersion() >= 0x80000000L or os.path.basename(comspec).lower() == "command.com"): # Win9x, or using command.com on NT. We need to # use the w9xpopen intermediate program. For more @@ -878,11 +868,11 @@ class Popen(object): # use at xxx" and a hopeful warning about the # stability of your system. Cost is Ctrl+C wont # kill children. - creationflags |= CREATE_NEW_CONSOLE + creationflags |= _subprocess.CREATE_NEW_CONSOLE # Start the process try: - hp, ht, pid, tid = CreateProcess(executable, args, + hp, ht, pid, tid = _subprocess.CreateProcess(executable, args, # no special security None, None, int(not close_fds), @@ -921,8 +911,9 @@ class Popen(object): """Check if child process has terminated. Returns returncode attribute.""" if self.returncode is None: - if WaitForSingleObject(self._handle, 0) == WAIT_OBJECT_0: - self.returncode = GetExitCodeProcess(self._handle) + if(_subprocess.WaitForSingleObject(self._handle, 0) == + _subprocess.WAIT_OBJECT_0): + self.returncode = _subprocess.GetExitCodeProcess(self._handle) return self.returncode @@ -930,8 +921,9 @@ class Popen(object): """Wait for child process to terminate. Returns returncode attribute.""" if self.returncode is None: - WaitForSingleObject(self._handle, INFINITE) - self.returncode = GetExitCodeProcess(self._handle) + _subprocess.WaitForSingleObject(self._handle, + _subprocess.INFINITE) + self.returncode = _subprocess.GetExitCodeProcess(self._handle) return self.returncode @@ -1000,7 +992,7 @@ class Popen(object): def terminate(self): """Terminates the process """ - TerminateProcess(self._handle, 1) + _subprocess.TerminateProcess(self._handle, 1) kill = terminate |