Example #1
0
        def _get_handles(self, stdin, stdout, stderr):
            """
            Construct and return tuple with IO objects:
            p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite
            """
            if stdin is None and stdout is None and stderr is None:
                return (None, None, None, None, None, None)

            p2cread, p2cwrite = None, None
            c2pread, c2pwrite = None, None
            errread, errwrite = None, None

            if stdin is None:
                p2cread = _subprocess.GetStdHandle(_subprocess.STD_INPUT_HANDLE)
                if p2cread is None:
                    p2cread, _ = _subprocess.CreatePipe(None, 0)
            elif stdin == subprocess.PIPE:
                p2cread, p2cwrite = _subprocess.CreatePipe(None, 0)
            elif getattr(stdin, "getsockopt", None):
                p2cread = stdin.fileno()
            elif isinstance(stdin, int):
                p2cread = msvcrt.get_osfhandle(stdin)
            else:
                # Assuming file-like object
                p2cread = msvcrt.get_osfhandle(stdin.fileno())
            p2cread = self._make_inheritable(p2cread)

            if stdout is None:
                c2pwrite = _subprocess.GetStdHandle(_subprocess.STD_OUTPUT_HANDLE)
                if c2pwrite is None:
                    _, c2pwrite = _subprocess.CreatePipe(None, 0)
            elif stdout == subprocess.PIPE:
                c2pread, c2pwrite = _subprocess.CreatePipe(None, 0)
            elif getattr(stdout, "getsockopt", None):
                c2pwrite = stdout.fileno()
            elif isinstance(stdout, int):
                c2pwrite = msvcrt.get_osfhandle(stdout)
            else:
                # Assuming file-like object
                c2pwrite = msvcrt.get_osfhandle(stdout.fileno())
            c2pwrite = self._make_inheritable(c2pwrite)

            if stderr is None:
                errwrite = _subprocess.GetStdHandle(_subprocess.STD_ERROR_HANDLE)
                if errwrite is None:
                    _, errwrite = _subprocess.CreatePipe(None, 0)
            elif stderr == subprocess.PIPE:
                errread, errwrite = _subprocess.CreatePipe(None, 0)
            elif stderr == subprocess.STDOUT:
                errwrite = c2pwrite
            elif isinstance(stderr, int):
                errwrite = msvcrt.get_osfhandle(stderr)
            else:
                # Assuming file-like object
                errwrite = msvcrt.get_osfhandle(stderr.fileno())
            errwrite = self._make_inheritable(errwrite)

            return (p2cread, p2cwrite,
                    c2pread, c2pwrite,
                    errread, errwrite)
Example #2
0
 def _get_handles(self, stdin, stdout, stderr):
     """Construct and return tuple with IO objects:
     p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite
     """
     to_close = set()
     if stdin is None and stdout is None and stderr is None:
         return ((None, None, None, None, None, None), to_close)
     else:
         p2cread, p2cwrite = (None, None)
         c2pread, c2pwrite = (None, None)
         errread, errwrite = (None, None)
         if stdin is None:
             p2cread = _subprocess.GetStdHandle(
                 _subprocess.STD_INPUT_HANDLE)
             if p2cread is None:
                 p2cread, _ = _subprocess.CreatePipe(None, 0)
         elif stdin == PIPE:
             p2cread, p2cwrite = _subprocess.CreatePipe(None, 0)
         elif isinstance(stdin, int):
             p2cread = msvcrt.get_osfhandle(stdin)
         else:
             p2cread = msvcrt.get_osfhandle(stdin.fileno())
         p2cread = self._make_inheritable(p2cread)
         to_close.add(p2cread)
         if stdin == PIPE:
             to_close.add(p2cwrite)
         if stdout is None:
             c2pwrite = _subprocess.GetStdHandle(
                 _subprocess.STD_OUTPUT_HANDLE)
             if c2pwrite is None:
                 _, c2pwrite = _subprocess.CreatePipe(None, 0)
         elif stdout == PIPE:
             c2pread, c2pwrite = _subprocess.CreatePipe(None, 0)
         elif isinstance(stdout, int):
             c2pwrite = msvcrt.get_osfhandle(stdout)
         else:
             c2pwrite = msvcrt.get_osfhandle(stdout.fileno())
         c2pwrite = self._make_inheritable(c2pwrite)
         to_close.add(c2pwrite)
         if stdout == PIPE:
             to_close.add(c2pread)
         if stderr is None:
             errwrite = _subprocess.GetStdHandle(
                 _subprocess.STD_ERROR_HANDLE)
             if errwrite is None:
                 _, errwrite = _subprocess.CreatePipe(None, 0)
         elif stderr == PIPE:
             errread, errwrite = _subprocess.CreatePipe(None, 0)
         elif stderr == STDOUT:
             errwrite = c2pwrite
         elif isinstance(stderr, int):
             errwrite = msvcrt.get_osfhandle(stderr)
         else:
             errwrite = msvcrt.get_osfhandle(stderr.fileno())
         errwrite = self._make_inheritable(errwrite)
         to_close.add(errwrite)
         if stderr == PIPE:
             to_close.add(errread)
         return ((p2cread, p2cwrite, c2pread, c2pwrite, errread,
                  errwrite), to_close)
Example #3
0
 def _checkFileObjInheritable(cls, fileobj, handle_name):
     """Check if a given file-like object (or whatever else subprocess.Popen
     takes as a handle/stream) can be correctly inherited by a child process.
     This just duplicates the code in subprocess.Popen._get_handles to make
     sure we go down the correct code path; this to catch some non-standard
     corner cases."""
     import _subprocess
     import ctypes
     import msvcrt
     new_handle = None
     try:
         if fileobj is None:
             handle = _subprocess.GetStdHandle(
                 getattr(_subprocess, handle_name))
             if handle is None:
                 return True  # No need to check things we create
         elif fileobj == subprocess.PIPE:
             return True  # No need to check things we create
         elif isinstance(fileobj, int):
             handle = msvcrt.get_osfhandle(fileobj)
         else:
             # Assuming file-like object
             handle = msvcrt.get_osfhandle(fileobj.fileno())
         new_handle = self._make_inheritable(handle)
         return True
     except:
         return False
     finally:
         CloseHandle = ctypes.windll.kernel32.CloseHandle
         if new_handle is not None:
             CloseHandle(new_handle)
Example #4
0
    def _isFileObjInheritable(cls, fileobj, stream_name):
        """Check if a given file-like object (or whatever else subprocess.Popen
        takes as a handle/stream) can be correctly inherited by a child process.
        This just duplicates the code in subprocess.Popen._get_handles to make
        sure we go down the correct code path; this to catch some non-standard
        corner cases.

        @param fileobj The object being used as a fd/handle/whatever
        @param stream_name The name of the stream, "stdin", "stdout", or "stderr"
        """
        import _subprocess
        import ctypes
        import msvcrt
        new_handle = None

        # Things that we know how to reset (i.e. not custom fds)
        valid_list = {
            "stdin": (sys.stdin, sys.__stdin__, 0),
            "stdout": (sys.stdout, sys.__stdout__, 1),
            "stderr": (sys.stderr, sys.__stderr__, 2),
        }[stream_name]

        try:
            if fileobj is None:
                std_handle = {
                    "stdin": _subprocess.STD_INPUT_HANDLE,
                    "stdout": _subprocess.STD_OUTPUT_HANDLE,
                    "stderr": _subprocess.STD_ERROR_HANDLE,
                }[stream_name]
                handle = _subprocess.GetStdHandle(std_handle)
                if handle is None:
                    # subprocess.Popen._get_handles creates a new pipe here
                    # we don't have to worry about things we create
                    return True
            elif fileobj == subprocess.PIPE:
                # We're creating a new pipe; newly created things are inheritable
                return True
            elif fileobj not in valid_list:
                # We are trying to use a custom fd; don't do anything fancy here,
                # we don't want to actually use subprocess.PIPE
                return True
            elif isinstance(fileobj, int):
                handle = msvcrt.get_osfhandle(fileobj)
            else:
                # Assuming file-like object
                handle = msvcrt.get_osfhandle(fileobj.fileno())
            new_handle = self._make_inheritable(handle)
            return True
        except:
            return False
        finally:
            CloseHandle = ctypes.windll.kernel32.CloseHandle
            if new_handle is not None:
                CloseHandle(new_handle)
Example #5
0
        def _get_handles(self, stdin, stdout, stderr):
            """Construct and return tuple with IO objects:
            p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite
            """
            to_close = set()
            if stdin is None and stdout is None and stderr is None:
                return (None, None, None, None, None, None), to_close

            p2cread, p2cwrite = None, None
            c2pread, c2pwrite = None, None
            errread, errwrite = None, None

            if stdin is None:
                p2cread = _subprocess.GetStdHandle(_subprocess.STD_INPUT_HANDLE)
                if p2cread is None:
                    p2cread, _ = _subprocess.CreatePipe(None, 0)
            elif stdin == PIPE:
                p2cread, p2cwrite = _subprocess.CreatePipe(None, 0)
            elif isinstance(stdin, (int, long)):
                p2cread = msvcrt.get_osfhandle(stdin)
            else:
                # Assuming file-like object
                p2cread = msvcrt.get_osfhandle(stdin.fileno())
            p2cread = self._make_inheritable(p2cread)
            # We just duplicated the handle, it has to be closed at the end
            to_close.add(p2cread)
            if stdin == PIPE:
                to_close.add(p2cwrite)

            if stdout is None:
                c2pwrite = _subprocess.GetStdHandle(_subprocess.STD_OUTPUT_HANDLE)
                if c2pwrite is None:
                    _, c2pwrite = _subprocess.CreatePipe(None, 0)
            elif stdout == PIPE:
                c2pread, c2pwrite = _subprocess.CreatePipe(None, 0)
            elif isinstance(stdout, (int, long)):
                c2pwrite = msvcrt.get_osfhandle(stdout)
            else:
                # Assuming file-like object
                c2pwrite = msvcrt.get_osfhandle(stdout.fileno())
            c2pwrite = self._make_inheritable(c2pwrite)
            # We just duplicated the handle, it has to be closed at the end
            to_close.add(c2pwrite)
            if stdout == PIPE:
                to_close.add(c2pread)

            if stderr is None:
                errwrite = _subprocess.GetStdHandle(_subprocess.STD_ERROR_HANDLE)
                if errwrite is None:
                    _, errwrite = _subprocess.CreatePipe(None, 0)
            elif stderr == PIPE:
                errread, errwrite = _subprocess.CreatePipe(None, 0)
            elif stderr == STDOUT:
                errwrite = c2pwrite
            elif isinstance(stderr, (int, long)):
                errwrite = msvcrt.get_osfhandle(stderr)
            else:
                # Assuming file-like object
                errwrite = msvcrt.get_osfhandle(stderr.fileno())
            errwrite = self._make_inheritable(errwrite)
            # We just duplicated the handle, it has to be closed at the end
            to_close.add(errwrite)
            if stderr == PIPE:
                to_close.add(errread)

            return (p2cread, p2cwrite,
                    c2pread, c2pwrite,
                    errread, errwrite), to_close
Example #6
0
	"""Return a duplicate of handle, which is inheritable"""
	return _subprocess.DuplicateHandle(_subprocess.GetCurrentProcess(),
						handle, _subprocess.GetCurrentProcess(), 0, 1,
						_subprocess.DUPLICATE_SAME_ACCESS)

	
bufsize = 0			
#创建输入接口
pipein1,pipein1w = _subprocess.CreatePipe(None, 0)
pipein1 = _make_inheritable(pipein1)
#接管进程输入口
startupinfo32 = subprocess.STARTUPINFO()
startupinfo32.dwFlags = subprocess.STARTF_USESHOWWINDOW | subprocess.STARTF_USESTDHANDLES
startupinfo32.wShowWindow = 1
startupinfo32.hStdInput = pipein1
startupinfo32.hStdOut = _make_inheritable(_subprocess.GetStdHandle(_subprocess.STD_OUTPUT_HANDLE))
#显示窗口方式打开新进程
p1 = subprocess.Popen('webrtc-cpp-sample.exe',startupinfo = startupinfo32,creationflags=subprocess.CREATE_NEW_CONSOLE)

#关闭句柄
if p1.stdin:
	p1.stdin.close()
if p1.stdout:
	p1.stdout.close()
if p1.stderr:
	p1.stderr.close()

#关联进程stdin
pipein1w = msvcrt.open_osfhandle(pipein1w.Detach(), 0)
p1.stdin = os.fdopen(pipein1w, 'wb', bufsize)